forked from Kotlin/kotlinx.coroutines
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Comply with Subscriber rule 2.7 in the
await*
impl (Kotlin#3360)
There is a possibility of a race between Subscription.request and Subscription.cancel methods since cancellation handler could be executed in a separate thread. Rule [2.7](https://github.com/reactive-streams/reactive-streams-jvm/blob/v1.0.3/README.md#2-subscriber-code) requires Subscription methods to be executed serially.
- Loading branch information
1 parent
7eb4618
commit 836a4bb
Showing
2 changed files
with
68 additions
and
5 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
43 changes: 43 additions & 0 deletions
43
reactive/kotlinx-coroutines-reactive/test/AwaitCancellationStressTest.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,43 @@ | ||
/* | ||
* Copyright 2016-2020 JetBrains s.r.o. Use of this source code is governed by the Apache 2.0 license. | ||
*/ | ||
|
||
package kotlinx.coroutines.reactive | ||
|
||
import kotlinx.coroutines.* | ||
import org.junit.* | ||
import org.reactivestreams.* | ||
import java.util.concurrent.locks.* | ||
|
||
/** | ||
* This test checks implementation of rule 2.7 for await methods - serial execution of subscription methods | ||
*/ | ||
class AwaitCancellationStressTest : TestBase() { | ||
private val iterations = 10_000 * stressTestMultiplier | ||
|
||
@Test | ||
fun testAwaitCancellationOrder() = runTest { | ||
repeat(iterations) { | ||
val job = launch(Dispatchers.Default) { | ||
testPublisher().awaitFirst() | ||
} | ||
job.cancelAndJoin() | ||
} | ||
} | ||
|
||
private fun testPublisher() = Publisher<Int> { s -> | ||
val lock = ReentrantLock() | ||
s.onSubscribe(object : Subscription { | ||
override fun request(n: Long) { | ||
check(lock.tryLock()) | ||
s.onNext(42) | ||
lock.unlock() | ||
} | ||
|
||
override fun cancel() { | ||
check(lock.tryLock()) | ||
lock.unlock() | ||
} | ||
}) | ||
} | ||
} |