1
1
/*
2
2
* Copyright 2016-2021 JetBrains s.r.o. Use of this source code is governed by the Apache 2.0 license.
3
3
*/
4
-
5
4
package kotlinx.coroutines
6
5
6
+ import kotlinx.coroutines.exceptions.*
7
7
import kotlin.coroutines.*
8
8
import kotlin.test.*
9
9
@@ -59,19 +59,19 @@ class RunBlockingTest : TestBase() {
59
59
runBlocking(thread) {
60
60
expect(2 )
61
61
assertSame(coroutineContext[ContinuationInterceptor ], thread)
62
- assertTrue(Thread .currentThread().name .contains(name))
62
+ assertTrue(currentThreadName() .contains(name))
63
63
yield () // should work
64
64
expect(3 )
65
65
}
66
66
finish(4 )
67
67
thread.close()
68
68
}
69
69
70
-
71
70
@Test
72
- fun testCancellation () = newFixedThreadPoolContext(2 , " testCancellation" ).use {
73
- val job = GlobalScope .launch(it) {
74
- runBlocking(coroutineContext) {
71
+ fun testCancellation () {
72
+ val ctx = newSingleThreadContext(" testCancellation" )
73
+ val job = GlobalScope .launch {
74
+ runBlocking(coroutineContext + ctx) {
75
75
while (true ) {
76
76
yield ()
77
77
}
@@ -81,6 +81,7 @@ class RunBlockingTest : TestBase() {
81
81
runBlocking {
82
82
job.cancelAndJoin()
83
83
}
84
+ ctx.close()
84
85
}
85
86
86
87
@Test
@@ -104,40 +105,44 @@ class RunBlockingTest : TestBase() {
104
105
}
105
106
}
106
107
107
- @Test(expected = CancellationException ::class )
108
- fun testDispatchOnShutdown () = runBlocking<Unit > {
109
- expect(1 )
110
- val job = launch(NonCancellable ) {
111
- try {
112
- expect(2 )
113
- delay(Long .MAX_VALUE )
114
- } finally {
115
- finish(4 )
108
+ @Test
109
+ fun testDispatchOnShutdown (): Unit = assertFailsWith<CancellationException > {
110
+ runBlocking<Unit > {
111
+ expect(1 )
112
+ val job = launch(NonCancellable ) {
113
+ try {
114
+ expect(2 )
115
+ delay(Long .MAX_VALUE )
116
+ } finally {
117
+ finish(4 )
118
+ }
116
119
}
117
- }
118
120
119
- yield ()
120
- expect(3 )
121
- coroutineContext.cancel()
122
- job.cancel()
123
- }
121
+ yield ()
122
+ expect(3 )
123
+ coroutineContext.cancel()
124
+ job.cancel()
125
+ }
126
+ }.let { }
124
127
125
- @Test(expected = CancellationException ::class )
126
- fun testDispatchOnShutdown2 () = runBlocking<Unit > {
127
- coroutineContext.cancel()
128
- expect(1 )
129
- val job = launch(NonCancellable , start = CoroutineStart .UNDISPATCHED ) {
130
- try {
131
- expect(2 )
132
- delay(Long .MAX_VALUE )
133
- } finally {
134
- finish(4 )
128
+ @Test
129
+ fun testDispatchOnShutdown2 (): Unit = assertFailsWith<CancellationException > {
130
+ runBlocking<Unit > {
131
+ coroutineContext.cancel()
132
+ expect(1 )
133
+ val job = launch(NonCancellable , start = CoroutineStart .UNDISPATCHED ) {
134
+ try {
135
+ expect(2 )
136
+ delay(Long .MAX_VALUE )
137
+ } finally {
138
+ finish(4 )
139
+ }
135
140
}
136
- }
137
141
138
- expect(3 )
139
- job.cancel()
140
- }
142
+ expect(3 )
143
+ job.cancel()
144
+ }
145
+ }.let { }
141
146
142
147
@Test
143
148
fun testNestedRunBlocking () = runBlocking {
@@ -157,21 +162,12 @@ class RunBlockingTest : TestBase() {
157
162
fun testIncompleteState () {
158
163
val handle = runBlocking {
159
164
// See #835
160
- coroutineContext[Job ]!! .invokeOnCompletion { }
165
+ coroutineContext[Job ]!! .invokeOnCompletion { }
161
166
}
162
167
163
168
handle.dispose()
164
169
}
165
170
166
- @Test
167
- fun testContract () {
168
- val rb: Int
169
- runBlocking {
170
- rb = 42
171
- }
172
- rb.hashCode() // unused
173
- }
174
-
175
171
@Test
176
172
fun testCancelledParent () {
177
173
val job = Job ()
0 commit comments