-
Notifications
You must be signed in to change notification settings - Fork 1.8k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Make Mono.awaitSingleOrNull wait for onComplete() (#3489)
Fixes #3487
- Loading branch information
1 parent
f3527c9
commit 92a2495
Showing
2 changed files
with
60 additions
and
4 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
54 changes: 54 additions & 0 deletions
54
reactive/kotlinx-coroutines-reactor/test/MonoAwaitStressTest.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,54 @@ | ||
/* | ||
* Copyright 2016-2022 JetBrains s.r.o. Use of this source code is governed by the Apache 2.0 license. | ||
*/ | ||
|
||
package kotlinx.coroutines.reactor | ||
|
||
import kotlinx.coroutines.* | ||
import org.junit.Test | ||
import org.reactivestreams.* | ||
import reactor.core.* | ||
import reactor.core.publisher.* | ||
import kotlin.concurrent.* | ||
import kotlin.test.* | ||
|
||
class MonoAwaitStressTest: TestBase() { | ||
private val N_REPEATS = 10_000 * stressTestMultiplier | ||
|
||
private var completed: Boolean = false | ||
|
||
private var thread: Thread? = null | ||
|
||
/** | ||
* Tests that [Mono.awaitSingleOrNull] does await [CoreSubscriber.onComplete] and does not return | ||
* the value as soon as it has it. | ||
*/ | ||
@Test | ||
fun testAwaitingRacingWithCompletion() = runTest { | ||
val mono = object: Mono<Int>() { | ||
override fun subscribe(s: CoreSubscriber<in Int>) { | ||
s.onSubscribe(object : Subscription { | ||
override fun request(n: Long) { | ||
thread = thread { | ||
s.onNext(1) | ||
Thread.yield() | ||
completed = true | ||
s.onComplete() | ||
} | ||
} | ||
|
||
override fun cancel() { | ||
} | ||
}) | ||
} | ||
} | ||
repeat(N_REPEATS) { | ||
thread = null | ||
completed = false | ||
val value = mono.awaitSingleOrNull() | ||
assertTrue(completed, "iteration $it") | ||
assertEquals(1, value) | ||
thread!!.join() | ||
} | ||
} | ||
} |