aboutsummaryrefslogtreecommitdiffstats
path: root/kotlinx-coroutines-core/jvm/test/lincheck/ChannelsLincheckTest.kt
diff options
context:
space:
mode:
Diffstat (limited to 'kotlinx-coroutines-core/jvm/test/lincheck/ChannelsLincheckTest.kt')
-rw-r--r--kotlinx-coroutines-core/jvm/test/lincheck/ChannelsLincheckTest.kt30
1 files changed, 15 insertions, 15 deletions
diff --git a/kotlinx-coroutines-core/jvm/test/lincheck/ChannelsLincheckTest.kt b/kotlinx-coroutines-core/jvm/test/lincheck/ChannelsLincheckTest.kt
index fbd5c0d8..74cc1783 100644
--- a/kotlinx-coroutines-core/jvm/test/lincheck/ChannelsLincheckTest.kt
+++ b/kotlinx-coroutines-core/jvm/test/lincheck/ChannelsLincheckTest.kt
@@ -63,11 +63,12 @@ abstract class ChannelLincheckTestBase(
}
@Operation
- fun offer(@Param(name = "value") value: Int): Any = try {
- c.offer(value)
- } catch (e: NumberedCancellationException) {
- e.testResult
- }
+ fun trySend(@Param(name = "value") value: Int): Any = c.trySend(value)
+ .onSuccess { return true }
+ .onFailure {
+ return if (it is NumberedCancellationException) it.testResult
+ else false
+ }
// TODO: this operation should be (and can be!) linearizable, but is not
// @Operation
@@ -85,11 +86,10 @@ abstract class ChannelLincheckTestBase(
}
@Operation
- fun poll(): Any? = try {
- c.poll()
- } catch (e: NumberedCancellationException) {
- e.testResult
- }
+ fun tryReceive(): Any? =
+ c.tryReceive()
+ .onSuccess { return it }
+ .onFailure { return if (it is NumberedCancellationException) it.testResult else null }
// TODO: this operation should be (and can be!) linearizable, but is not
// @Operation
@@ -131,7 +131,7 @@ abstract class SequentialIntChannelBase(private val capacity: Int) : VerifierSta
private val buffer = ArrayList<Int>()
private var closedMessage: String? = null
- suspend fun send(x: Int): Any = when (val offerRes = offer(x)) {
+ suspend fun send(x: Int): Any = when (val offerRes = trySend(x)) {
true -> Unit
false -> suspendCancellableCoroutine { cont ->
senders.add(cont to x)
@@ -139,7 +139,7 @@ abstract class SequentialIntChannelBase(private val capacity: Int) : VerifierSta
else -> offerRes
}
- fun offer(element: Int): Any {
+ fun trySend(element: Int): Any {
if (closedMessage !== null) return closedMessage!!
if (capacity == CONFLATED) {
if (resumeFirstReceiver(element)) return true
@@ -163,11 +163,11 @@ abstract class SequentialIntChannelBase(private val capacity: Int) : VerifierSta
return false
}
- suspend fun receive(): Any = poll() ?: suspendCancellableCoroutine { cont ->
+ suspend fun receive(): Any = tryReceive() ?: suspendCancellableCoroutine { cont ->
receivers.add(cont)
}
- fun poll(): Any? {
+ fun tryReceive(): Any? {
if (buffer.isNotEmpty()) {
val el = buffer.removeAt(0)
resumeFirstSender().also {
@@ -221,4 +221,4 @@ private fun <T> CancellableContinuation<T>.resume(res: T): Boolean {
val token = tryResume(res) ?: return false
completeResume(token)
return true
-} \ No newline at end of file
+}