Skip to content

Commit 9b6e311

Browse files
committed
Moved ChannelAtomicCancelStressTest to newFixedThreadPoolContext
Hopefully, this is going to make this test less flaky (more stable) due to more predictably scheduling of two stressed tasks onto two different core and, at the time, increases quality of the test by stressing multi-threaded channel operation more.
1 parent 310f8b7 commit 9b6e311

File tree

1 file changed

+12
-4
lines changed

1 file changed

+12
-4
lines changed

kotlinx-coroutines-core/jvm/test/channels/ChannelAtomicCancelStressTest.kt

+12-4
Original file line numberDiff line numberDiff line change
@@ -24,9 +24,12 @@ class ChannelAtomicCancelStressTest(private val kind: TestChannelKind) : TestBas
2424
fun params(): Collection<Array<Any>> = TestChannelKind.values().map { arrayOf<Any>(it) }
2525
}
2626

27-
private val TEST_DURATION = 3000L * stressTestMultiplier
27+
private val TEST_DURATION = 1000L * stressTestMultiplier
2828

29-
val channel = kind.create()
29+
private val dispatcher = newFixedThreadPoolContext(2, "ChannelAtomicCancelStressTest")
30+
private val scope = CoroutineScope(dispatcher)
31+
32+
private val channel = kind.create()
3033
private val senderDone = Channel<Boolean>(1)
3134
private val receiverDone = Channel<Boolean>(1)
3235

@@ -44,6 +47,11 @@ class ChannelAtomicCancelStressTest(private val kind: TestChannelKind) : TestBas
4447
lateinit var sender: Job
4548
lateinit var receiver: Job
4649

50+
@After
51+
fun tearDown() {
52+
dispatcher.close()
53+
}
54+
4755
fun fail(e: Throwable) = failed.compareAndSet(null, e)
4856

4957
private inline fun cancellable(done: Channel<Boolean>, block: () -> Unit) {
@@ -95,7 +103,7 @@ class ChannelAtomicCancelStressTest(private val kind: TestChannelKind) : TestBas
95103
}
96104

97105
private fun launchSender() {
98-
sender = GlobalScope.launch(start = CoroutineStart.ATOMIC) {
106+
sender = scope.launch(start = CoroutineStart.ATOMIC) {
99107
val rnd = Random()
100108
cancellable(senderDone) {
101109
var counter = 0
@@ -120,7 +128,7 @@ class ChannelAtomicCancelStressTest(private val kind: TestChannelKind) : TestBas
120128
}
121129

122130
private fun launchReceiver() {
123-
receiver = GlobalScope.launch(start = CoroutineStart.ATOMIC) {
131+
receiver = scope.launch(start = CoroutineStart.ATOMIC) {
124132
val rnd = Random()
125133
cancellable(receiverDone) {
126134
while (true) {

0 commit comments

Comments
 (0)