Skip to content

Commit c1a1ce5

Browse files
committed
Fix a race condition
1 parent 4c4a67d commit c1a1ce5

File tree

2 files changed

+6
-5
lines changed

2 files changed

+6
-5
lines changed

kotlinx-coroutines-test/common/src/TestCoroutineScheduler.kt

+3-1
Original file line numberDiff line numberDiff line change
@@ -62,12 +62,14 @@ public class TestCoroutineScheduler: AbstractCoroutineContextElement(TestCorouti
6262
isCancelled : (T) -> Boolean
6363
): DisposableHandle {
6464
require(timeDeltaMillis >= 0) { "Attempted scheduling an event earlier in time (with the time delta $timeDeltaMillis)" }
65-
sendDispatchEvent()
6665
val count = count.getAndIncrement()
6766
return synchronized(lock) {
6867
val time = addClamping(currentTime, timeDeltaMillis)
6968
val event = TestDispatchEvent(dispatcher, count, time, marker as Any) { isCancelled(marker) }
7069
events.addLast(event)
70+
/** can't be moved above: otherwise, [onDispatchEvent] could consume the token sent here before there's
71+
* actually anything in the event queue. */
72+
sendDispatchEvent()
7173
DisposableHandle {
7274
synchronized(lock) {
7375
events.remove(event)

kotlinx-coroutines-test/jvm/test/MultithreadingTest.kt

+3-4
Original file line numberDiff line numberDiff line change
@@ -4,12 +4,11 @@
44

55
import kotlinx.coroutines.*
66
import kotlinx.coroutines.test.*
7-
import java.util.concurrent.*
87
import kotlin.concurrent.*
98
import kotlin.coroutines.*
109
import kotlin.test.*
1110

12-
class MultithreadingTest : TestBase() {
11+
class MultithreadingTest {
1312

1413
@Test
1514
fun incorrectlyCalledRunBlocking_doesNotHaveSameInterceptor() = runBlockingTest {
@@ -24,7 +23,7 @@ class MultithreadingTest : TestBase() {
2423
}
2524

2625
@Test
27-
fun testSingleThreadExecutor() = runTest {
26+
fun testSingleThreadExecutor() = runBlocking {
2827
val mainThread = Thread.currentThread()
2928
Dispatchers.setMain(Dispatchers.Unconfined)
3029
newSingleThreadContext("testSingleThread").use { threadPool ->
@@ -98,4 +97,4 @@ class MultithreadingTest : TestBase() {
9897
}
9998
}
10099
}
101-
}
100+
}

0 commit comments

Comments
 (0)