4
4
5
5
package kotlinx.coroutines
6
6
7
+ import org.junit.*
8
+ import org.junit.Test
7
9
import java.util.concurrent.atomic.*
8
10
import kotlin.test.*
9
11
10
12
class RunInterruptibleStressTest : TestBase () {
11
13
12
- private val dispatcher = Dispatchers .IO
14
+ @get:Rule
15
+ val dispatcher = ExecutorRule (4 )
13
16
private val REPEAT_TIMES = 1000 * stressTestMultiplier
14
17
15
18
@Test
16
19
fun testStress () = runBlocking {
17
20
val interruptLeak = AtomicBoolean (false )
18
21
val enterCount = AtomicInteger (0 )
19
22
val interruptedCount = AtomicInteger (0 )
20
- val otherExceptionCount = AtomicInteger (0 )
21
23
22
- repeat(REPEAT_TIMES ) { repeat ->
23
- val job = launch(dispatcher, start = CoroutineStart . LAZY ) {
24
+ repeat(REPEAT_TIMES ) {
25
+ val job = launch(dispatcher) {
24
26
try {
25
27
runInterruptible {
26
28
enterCount.incrementAndGet()
@@ -32,27 +34,21 @@ class RunInterruptibleStressTest : TestBase() {
32
34
}
33
35
}
34
36
} catch (e: CancellationException ) {
35
- } catch (e: Throwable ) {
36
- otherExceptionCount.incrementAndGet()
37
+ // Expected
37
38
} finally {
38
39
interruptLeak.set(interruptLeak.get() || Thread .currentThread().isInterrupted)
39
40
}
40
41
}
41
-
42
- val cancelJob = launch(dispatcher, start = CoroutineStart . LAZY ) {
42
+ // Add dispatch delay
43
+ val cancelJob = launch(dispatcher) {
43
44
job.cancel()
44
45
}
45
46
46
47
job.start()
47
- val canceller = launch(dispatcher) {
48
- cancelJob.start()
49
- }
50
-
51
- joinAll(job, cancelJob, canceller)
48
+ joinAll(job, cancelJob)
52
49
}
53
50
54
51
assertFalse(interruptLeak.get())
55
52
assertEquals(enterCount.get(), interruptedCount.get())
56
- assertEquals(0 , otherExceptionCount.get())
57
53
}
58
54
}
0 commit comments