blob: 5090e7c06c60fae3173965d9d5a153385b7141a6 (
plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
|
/*
* Copyright 2016-2018 JetBrains s.r.o. Use of this source code is governed by the Apache 2.0 license.
*/
package kotlinx.coroutines
import org.junit.*
import org.junit.Test
import java.util.concurrent.*
import kotlin.test.*
class JoinStressTest : TestBase() {
private val iterations = 50_000 * stressTestMultiplier
private val pool = newFixedThreadPoolContext(3, "JoinStressTest")
@After
fun tearDown() {
pool.close()
}
@Test
fun testExceptionalJoinWithCancellation() = runBlocking {
val results = IntArray(2)
repeat(iterations) {
val barrier = CyclicBarrier(3)
val exceptionalJob = async(pool + NonCancellable) {
barrier.await()
throw TestException()
}
val awaiterJob = async(pool) {
barrier.await()
try {
exceptionalJob.await()
} catch (e: TestException) {
0
} catch (e: CancellationException) {
1
}
}
barrier.await()
exceptionalJob.cancel()
++results[awaiterJob.await()]
}
// Check that concurrent cancellation of job which throws TestException without suspends doesn't suppress TestException
assertEquals(iterations, results[0], results.toList().toString())
assertEquals(0, results[1], results.toList().toString())
}
@Test
fun testExceptionalJoinWithMultipleCancellations() = runBlocking {
val results = IntArray(2)
repeat(iterations) {
val barrier = CyclicBarrier(4)
val exceptionalJob = async<Unit>(pool + NonCancellable) {
barrier.await()
throw TestException()
}
val awaiterJob = async(pool) {
barrier.await()
try {
exceptionalJob.await()
2
} catch (e: TestException) {
0
} catch (e: TestException1) {
1
}
}
val canceller = async(pool + NonCancellable) {
barrier.await()
// cast for test purposes only
(exceptionalJob as AbstractCoroutine<*>).cancelInternal(TestException1())
}
barrier.await()
val awaiterResult = awaiterJob.await()
canceller.await()
++results[awaiterResult]
}
assertTrue(results[0] > 0, results.toList().toString())
assertTrue(results[1] > 0, results.toList().toString())
}
}
|