-
Notifications
You must be signed in to change notification settings - Fork 1.9k
/
Copy pathReusableCancellableContinuationInvariantStressTest.kt
53 lines (42 loc) · 1.72 KB
/
ReusableCancellableContinuationInvariantStressTest.kt
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
/*
* Copyright 2016-2021 JetBrains s.r.o. Use of this source code is governed by the Apache 2.0 license.
*/
package kotlinx.coroutines
import org.junit.Test
import java.util.concurrent.CountDownLatch
import java.util.concurrent.atomic.AtomicReference
import kotlin.coroutines.*
// Stresses scenario from #3613
class ReusableCancellableContinuationInvariantStressTest : TestBase() {
// Tests have a timeout 10 sec because the bug they catch leads to an infinite spin-loop
@Test(timeout = 10_000)
fun testExceptionFromSuspendReusable() = doTest { /* nothing */ }
@Test(timeout = 10_000)
fun testExceptionFromCancelledSuspendReusable() = doTest { it.cancel() }
@Suppress("SuspendFunctionOnCoroutineScope")
private inline fun doTest(crossinline block: (Job) -> Unit) {
runTest {
repeat(10_000) {
val latch = CountDownLatch(1)
val continuationToResume = AtomicReference<Continuation<Unit>?>(null)
val j1 = launch(Dispatchers.Default) {
latch.await()
suspendCancellableCoroutineReusable {
continuationToResume.set(it)
block(coroutineContext.job)
throw CancellationException() // Don't let getResult() chance to execute
}
}
val j2 = launch(Dispatchers.Default) {
latch.await()
while (continuationToResume.get() == null) {
// spin
}
continuationToResume.get()!!.resume(Unit)
}
latch.countDown()
joinAll(j1, j2)
}
}
}
}