-
Notifications
You must be signed in to change notification settings - Fork 1.9k
/
Copy pathRunInterruptibleStressTest.kt
58 lines (48 loc) · 1.84 KB
/
RunInterruptibleStressTest.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
54
55
56
57
58
/*
* Copyright 2016-2018 JetBrains s.r.o. Use of this source code is governed by the Apache 2.0 license.
*/
package kotlinx.coroutines
import java.util.concurrent.atomic.*
import kotlin.test.*
class RunInterruptibleStressTest : TestBase() {
private val dispatcher = Dispatchers.IO
private val REPEAT_TIMES = 1000 * stressTestMultiplier
@Test
fun testStress() = runBlocking {
val interruptLeak = AtomicBoolean(false)
val enterCount = AtomicInteger(0)
val interruptedCount = AtomicInteger(0)
val otherExceptionCount = AtomicInteger(0)
repeat(REPEAT_TIMES) { repeat ->
val job = launch(dispatcher, start = CoroutineStart.LAZY) {
try {
runInterruptible {
enterCount.incrementAndGet()
try {
Thread.sleep(Long.MAX_VALUE)
} catch (e: InterruptedException) {
interruptedCount.incrementAndGet()
throw e
}
}
} catch (e: CancellationException) {
} catch (e: Throwable) {
otherExceptionCount.incrementAndGet()
} finally {
interruptLeak.set(interruptLeak.get() || Thread.currentThread().isInterrupted)
}
}
val cancelJob = launch(dispatcher, start = CoroutineStart.LAZY) {
job.cancel()
}
job.start()
val canceller = launch(dispatcher) {
cancelJob.start()
}
joinAll(job, cancelJob, canceller)
}
assertFalse(interruptLeak.get())
assertEquals(enterCount.get(), interruptedCount.get())
assertEquals(0, otherExceptionCount.get())
}
}