@@ -10,10 +10,11 @@ import org.junit.Test
10
10
import java.util.concurrent.*
11
11
import kotlin.coroutines.*
12
12
import kotlin.test.*
13
+ import kotlin.time.Duration.Companion.minutes
13
14
14
15
class WithContextCancellationStressTest : TestBase () {
15
16
16
- private val iterations = 15_000 * stressTestMultiplier
17
+ private val timeoutAfter = 1 .minutes
17
18
private val pool = newFixedThreadPoolContext(3 , " WithContextCancellationStressTest" )
18
19
19
20
@After
@@ -28,56 +29,54 @@ class WithContextCancellationStressTest : TestBase() {
28
29
var e1Cnt = 0
29
30
var e2Cnt = 0
30
31
31
- repeat(iterations) {
32
- val barrier = CyclicBarrier (4 )
33
- val ctx = pool + NonCancellable
34
- var e1 = false
35
- var e2 = false
36
- val jobWithContext = async(ctx) {
37
- withContext(wrapperDispatcher(coroutineContext)) {
38
- launch {
39
- barrier.await()
40
- e1 = true
41
- throw TestException1 ()
42
- }
32
+ withTimeout(timeoutAfter) {
33
+ while (eCnt == 0 || e1Cnt == 0 || e2Cnt == 0 ) {
34
+ val barrier = CyclicBarrier (4 )
35
+ val ctx = pool + NonCancellable
36
+ var e1 = false
37
+ var e2 = false
38
+ val jobWithContext = async(ctx) {
39
+ withContext(wrapperDispatcher(coroutineContext)) {
40
+ launch {
41
+ barrier.await()
42
+ e1 = true
43
+ throw TestException1 ()
44
+ }
45
+
46
+ launch {
47
+ barrier.await()
48
+ e2 = true
49
+ throw TestException2 ()
50
+ }
43
51
44
- launch {
45
52
barrier.await()
46
- e2 = true
47
- throw TestException2 ()
53
+ throw TestException ()
48
54
}
49
-
50
- barrier.await()
51
- throw TestException ()
52
55
}
53
- }
54
56
55
- barrier.await()
57
+ barrier.await()
56
58
57
- try {
58
- jobWithContext.await()
59
- } catch (e: Throwable ) {
60
- when (e) {
61
- is TestException -> {
62
- eCnt++
63
- e.checkSuppressed(e1 = e1, e2 = e2)
64
- }
65
- is TestException1 -> {
66
- e1Cnt++
67
- e.checkSuppressed(ex = true , e2 = e2)
59
+ try {
60
+ jobWithContext.await()
61
+ } catch (e: Throwable ) {
62
+ when (e) {
63
+ is TestException -> {
64
+ eCnt++
65
+ e.checkSuppressed(e1 = e1, e2 = e2)
66
+ }
67
+ is TestException1 -> {
68
+ e1Cnt++
69
+ e.checkSuppressed(ex = true , e2 = e2)
70
+ }
71
+ is TestException2 -> {
72
+ e2Cnt++
73
+ e.checkSuppressed(ex = true , e1 = e1)
74
+ }
75
+ else -> error(" Unexpected exception $e " )
68
76
}
69
- is TestException2 -> {
70
- e2Cnt++
71
- e.checkSuppressed(ex = true , e1 = e1)
72
- }
73
- else -> error(" Unexpected exception $e " )
74
77
}
75
78
}
76
79
}
77
-
78
- require(eCnt > 0 ) { " At least one TestException expected" }
79
- require(e1Cnt > 0 ) { " At least one TestException1 expected" }
80
- require(e2Cnt > 0 ) { " At least one TestException2 expected" }
81
80
}
82
81
83
82
private fun wrapperDispatcher (context : CoroutineContext ): CoroutineContext {
0 commit comments