Skip to content

Commit 41f3755

Browse files
committed
Make RunInterruptibleStressTest more straightforward, get rid of Dispatchers.IO
1 parent 0bf172b commit 41f3755

File tree

1 file changed

+10
-14
lines changed

1 file changed

+10
-14
lines changed

kotlinx-coroutines-core/jvm/test/RunInterruptibleStressTest.kt

Lines changed: 10 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -4,23 +4,25 @@
44

55
package kotlinx.coroutines
66

7+
import org.junit.*
8+
import org.junit.Test
79
import java.util.concurrent.atomic.*
810
import kotlin.test.*
911

1012
class RunInterruptibleStressTest : TestBase() {
1113

12-
private val dispatcher = Dispatchers.IO
14+
@get:Rule
15+
val dispatcher = ExecutorRule(4)
1316
private val REPEAT_TIMES = 1000 * stressTestMultiplier
1417

1518
@Test
1619
fun testStress() = runBlocking {
1720
val interruptLeak = AtomicBoolean(false)
1821
val enterCount = AtomicInteger(0)
1922
val interruptedCount = AtomicInteger(0)
20-
val otherExceptionCount = AtomicInteger(0)
2123

22-
repeat(REPEAT_TIMES) { repeat ->
23-
val job = launch(dispatcher, start = CoroutineStart.LAZY) {
24+
repeat(REPEAT_TIMES) {
25+
val job = launch(dispatcher) {
2426
try {
2527
runInterruptible {
2628
enterCount.incrementAndGet()
@@ -32,27 +34,21 @@ class RunInterruptibleStressTest : TestBase() {
3234
}
3335
}
3436
} catch (e: CancellationException) {
35-
} catch (e: Throwable) {
36-
otherExceptionCount.incrementAndGet()
37+
// Expected
3738
} finally {
3839
interruptLeak.set(interruptLeak.get() || Thread.currentThread().isInterrupted)
3940
}
4041
}
41-
42-
val cancelJob = launch(dispatcher, start = CoroutineStart.LAZY) {
42+
// Add dispatch delay
43+
val cancelJob = launch(dispatcher) {
4344
job.cancel()
4445
}
4546

4647
job.start()
47-
val canceller = launch(dispatcher) {
48-
cancelJob.start()
49-
}
50-
51-
joinAll(job, cancelJob, canceller)
48+
joinAll(job, cancelJob)
5249
}
5350

5451
assertFalse(interruptLeak.get())
5552
assertEquals(enterCount.get(), interruptedCount.get())
56-
assertEquals(0, otherExceptionCount.get())
5753
}
5854
}

0 commit comments

Comments
 (0)