@@ -11,75 +11,105 @@ import kotlin.test.*
11
11
class CancelledParentAttachTest : TestBase () {
12
12
13
13
@Test
14
- fun testAsync () = CoroutineStart .values().forEach(::testAsyncCancelledParent)
14
+ fun testAsync () = runTest {
15
+ CoroutineStart .values().forEach { testAsyncCancelledParent(it) }
16
+ }
15
17
16
- private fun testAsyncCancelledParent (start : CoroutineStart ) =
17
- runTest({ it is CancellationException }) {
18
- cancel()
19
- expect(1 )
20
- val d = async<Int >(start = start) { 42 }
21
- expect(2 )
22
- d.invokeOnCompletion {
23
- finish(3 )
24
- reset()
18
+ private suspend fun testAsyncCancelledParent (start : CoroutineStart ) {
19
+ try {
20
+ withContext(Job ()) {
21
+ cancel()
22
+ expect(1 )
23
+ val d = async<Int >(start = start) { 42 }
24
+ expect(2 )
25
+ d.invokeOnCompletion {
26
+ finish(3 )
27
+ reset()
28
+ }
25
29
}
30
+ expectUnreached()
31
+ } catch (e: CancellationException ) {
32
+ // Expected
26
33
}
34
+ }
27
35
28
36
@Test
29
- fun testLaunch () = CoroutineStart .values().forEach(::testLaunchCancelledParent)
37
+ fun testLaunch () = runTest {
38
+ CoroutineStart .values().forEach { testLaunchCancelledParent(it) }
39
+ }
30
40
31
- private fun testLaunchCancelledParent (start : CoroutineStart ) =
32
- runTest({ it is CancellationException }) {
33
- cancel()
34
- expect(1 )
35
- val d = launch(start = start) { }
36
- expect(2 )
37
- d.invokeOnCompletion {
38
- finish(3 )
39
- reset()
41
+ private suspend fun testLaunchCancelledParent (start : CoroutineStart ) {
42
+ try {
43
+ withContext(Job ()) {
44
+ cancel()
45
+ expect(1 )
46
+ val d = launch(start = start) { }
47
+ expect(2 )
48
+ d.invokeOnCompletion {
49
+ finish(3 )
50
+ reset()
51
+ }
40
52
}
53
+ expectUnreached()
54
+ } catch (e: CancellationException ) {
55
+ // Expected
41
56
}
57
+ }
42
58
43
59
@Test
44
- fun testProduce () =
45
- runTest({ it is CancellationException }) {
46
- cancel()
47
- expect(1 )
48
- val d = produce<Int > { }
49
- expect(2 )
50
- (d as Job ).invokeOnCompletion {
51
- finish(3 )
52
- reset()
53
- }
60
+ fun testProduce () = runTest({ it is CancellationException }) {
61
+ cancel()
62
+ expect(1 )
63
+ val d = produce<Int > { }
64
+ expect(2 )
65
+ (d as Job ).invokeOnCompletion {
66
+ finish(3 )
67
+ reset()
54
68
}
69
+ }
55
70
56
71
@Test
57
- fun testBroadcast () = CoroutineStart .values().forEach(::testBroadcastCancelledParent)
72
+ fun testBroadcast () = runTest {
73
+ CoroutineStart .values().forEach { testBroadcastCancelledParent(it) }
74
+ }
58
75
59
- private fun testBroadcastCancelledParent (start : CoroutineStart ) =
60
- runTest({ it is CancellationException }) {
61
- cancel()
62
- expect(1 )
63
- val bc = broadcast<Int >(start = start) {}
64
- expect(2 )
65
- (bc as Job ).invokeOnCompletion {
66
- finish(3 )
67
- reset()
76
+ private suspend fun testBroadcastCancelledParent (start : CoroutineStart ) {
77
+ try {
78
+ withContext(Job ()) {
79
+ cancel()
80
+ expect(1 )
81
+ val bc = broadcast<Int >(start = start) {}
82
+ expect(2 )
83
+ (bc as Job ).invokeOnCompletion {
84
+ finish(3 )
85
+ reset()
86
+ }
68
87
}
88
+ expectUnreached()
89
+ } catch (e: CancellationException ) {
90
+ // Expected
69
91
}
92
+ }
70
93
71
94
@Test
72
- fun testScopes () {
73
- testScope { coroutineScope { } }
74
- testScope { supervisorScope { } }
75
- testScope { flowScope { } }
76
- testScope { withTimeout(Long .MAX_VALUE ) { } }
77
- testScope { withContext(Job ()) { } }
78
- testScope { withContext(CoroutineName (" " )) { } }
95
+ fun testScopes () = runTest {
96
+ testScope { coroutineScope { } }
97
+ testScope { supervisorScope { } }
98
+ testScope { flowScope { } }
99
+ testScope { withTimeout(Long .MAX_VALUE ) { } }
100
+ testScope { withContext(Job ()) { } }
101
+ testScope { withContext(CoroutineName (" " )) { } }
79
102
}
80
103
81
- private inline fun testScope (crossinline block : suspend () -> Unit ) = runTest({ it is CancellationException }) {
82
- cancel()
83
- block()
104
+ private suspend inline fun testScope (crossinline block : suspend () -> Unit ) {
105
+ try {
106
+ withContext(Job ()) {
107
+ cancel()
108
+ block()
109
+ }
110
+ expectUnreached()
111
+ } catch (e: CancellationException ) {
112
+ // Expected
113
+ }
84
114
}
85
115
}
0 commit comments