• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright 2016-2018 JetBrains s.r.o. Use of this source code is governed by the Apache 2.0 license.
3  */
4 
5 package kotlinx.coroutines
6 
7 import kotlin.test.*
8 
9 class NonCancellableTest : TestBase() {
10     @Test
<lambda>null11     fun testNonCancellable() = runTest {
12         expect(1)
13         val job = async {
14             withContext(NonCancellable) {
15                 expect(2)
16                 yield()
17                 expect(4)
18             }
19 
20             expect(5)
21             yield()
22             expectUnreached()
23         }
24 
25         yield()
26         job.cancel()
27         expect(3)
28         assertTrue(job.isCancelled)
29         try {
30             job.await()
31             expectUnreached()
32         } catch (e: JobCancellationException) {
33             if (RECOVER_STACK_TRACES) {
34                 val cause = e.cause as JobCancellationException // shall be recovered JCE
35                 assertNull(cause.cause)
36             } else {
37                 assertNull(e.cause)
38             }
39             finish(6)
40         }
41     }
42 
43     @Test
<lambda>null44     fun testNonCancellableWithException() = runTest {
45         expect(1)
46         val deferred = async(NonCancellable) {
47             withContext(NonCancellable) {
48                 expect(2)
49                 yield()
50                 expect(4)
51             }
52 
53             expect(5)
54             yield()
55             expectUnreached()
56         }
57 
58         yield()
59         deferred.cancel(TestCancellationException("TEST"))
60         expect(3)
61         assertTrue(deferred.isCancelled)
62         try {
63             deferred.await()
64             expectUnreached()
65         } catch (e: TestCancellationException) {
66             assertEquals("TEST", e.message)
67             finish(6)
68         }
69     }
70 
71     @Test
<lambda>null72     fun testNonCancellableFinally() = runTest {
73         expect(1)
74         val job = async {
75             try {
76                 expect(2)
77                 yield()
78                 expectUnreached()
79             } finally {
80                 withContext(NonCancellable) {
81                     expect(4)
82                     yield()
83                     expect(5)
84                 }
85             }
86 
87             expectUnreached()
88         }
89 
90         yield()
91         job.cancel()
92         expect(3)
93         assertTrue(job.isCancelled)
94 
95         try {
96             job.await()
97             expectUnreached()
98         } catch (e: CancellationException) {
99             finish(6)
100         }
101     }
102 
103     @Test
<lambda>null104     fun testNonCancellableTwice() = runTest {
105         expect(1)
106         val job = async {
107             withContext(NonCancellable) {
108                 expect(2)
109                 yield()
110                 expect(4)
111             }
112 
113             withContext(NonCancellable) {
114                 expect(5)
115                 yield()
116                 expect(6)
117             }
118         }
119 
120         yield()
121         job.cancel()
122         expect(3)
123         assertTrue(job.isCancelled)
124         try {
125             job.await()
126             expectUnreached()
127         } catch (e: JobCancellationException) {
128             if (RECOVER_STACK_TRACES) {
129                 val cause = e.cause as JobCancellationException // shall be recovered JCE
130                 assertNull(cause.cause)
131             } else {
132                 assertNull(e.cause)
133             }
134             finish(7)
135         }
136     }
137 }
138