Skip to content

Commit 2397ffc

Browse files
authored
Fix race condition in testFutureIsDoneAfterChildrenCompleted (#4165)
The `foo` coroutine could be cancelled before it's started, which would immediately trigger the `expect(3)` in `foo.invokeOnCompletion`. This could happen before `expect(2)` and fail the test.
1 parent b7cbe09 commit 2397ffc

File tree

1 file changed

+4
-4
lines changed

1 file changed

+4
-4
lines changed

integration/kotlinx-coroutines-guava/test/ListenableFutureTest.kt

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -627,16 +627,15 @@ class ListenableFutureTest : TestBase() {
627627
fun testFutureIsDoneAfterChildrenCompleted() = runTest {
628628
expect(1)
629629
val testException = TestException()
630+
val latch = CountDownLatch(1)
630631
// Don't propagate exception to the test and use different dispatchers as we are going to block test thread.
631632
val future = future(context = NonCancellable + Dispatchers.Default) {
632-
val foo = async {
633+
val foo = async(start = CoroutineStart.UNDISPATCHED) {
633634
try {
634635
delay(Long.MAX_VALUE)
635636
42
636637
} finally {
637-
withContext(NonCancellable) {
638-
delay(200)
639-
}
638+
latch.await()
640639
}
641640
}
642641
foo.invokeOnCompletion {
@@ -647,6 +646,7 @@ class ListenableFutureTest : TestBase() {
647646
}
648647
yield()
649648
expect(2)
649+
latch.countDown()
650650
// Blocking get should succeed after internal coroutine completes.
651651
val thrown = assertFailsWith<ExecutionException> { future.get() }
652652
expect(4)

0 commit comments

Comments
 (0)