-
Notifications
You must be signed in to change notification settings - Fork 62
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Make sure consume elements from the TestChannel
- Loading branch information
Christian Melchior
committed
Nov 28, 2023
1 parent
87cc784
commit 5b5f857
Showing
3 changed files
with
30 additions
and
21 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
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -22,9 +22,9 @@ import io.realm.kotlin.test.platform.PlatformUtils | |
import io.realm.kotlin.types.RealmInstant | ||
import io.realm.kotlin.types.RealmObject | ||
import kotlinx.coroutines.TimeoutCancellationException | ||
import kotlinx.coroutines.channels.BufferOverflow | ||
import kotlinx.coroutines.channels.Channel | ||
import kotlinx.coroutines.selects.onTimeout | ||
import kotlinx.coroutines.selects.select | ||
import kotlinx.coroutines.withTimeout | ||
import kotlinx.datetime.Instant | ||
import kotlin.time.Duration | ||
import kotlin.time.Duration.Companion.minutes | ||
|
@@ -93,19 +93,34 @@ fun Instant.toRealmInstant(): RealmInstant { | |
} | ||
} | ||
|
||
/** | ||
* Channel implementation specifically suited for tests. Its size is unlimited, but will fail | ||
* the test if canceled containing unconsumed elements. | ||
*/ | ||
inline fun <T> TestChannel(): Channel<T> { | ||
return Channel<T>(capacity = Channel.UNLIMITED, onBufferOverflow = BufferOverflow.SUSPEND) { | ||
throw AssertionError("Failed to deliver: $it") | ||
} | ||
} | ||
|
||
// Variant of `Channel.receiveOrFail()` that will will throw if a timeout is hit. | ||
suspend fun <T : Any?> Channel<T>.receiveOrFail(timeout: Duration = 1.minutes, message: String? = null): T { | ||
// return withTimeout(timeout) { | ||
// [email protected]() | ||
// } | ||
return select { | ||
this@receiveOrFail.onReceive { | ||
println("Got: $it") | ||
it | ||
} | ||
onTimeout(timeout) { | ||
@Suppress("invisible_member") | ||
throw TimeoutCancellationException("Timeout after $timeout: ${if (message.isNullOrBlank()) "<no message>" else message}") | ||
try { | ||
return withTimeout(timeout) { | ||
this@receiveOrFail.receive() | ||
} | ||
} catch (ex: TimeoutCancellationException) { | ||
throw AssertionError("Timeout after $timeout: ${if (message.isNullOrBlank()) "<no message>" else message}") | ||
} | ||
// return select { | ||
// [email protected] { | ||
// println("Got: $it") | ||
// // receive() // Make sure to empty the channel | ||
// it | ||
// } | ||
// onTimeout(timeout) { | ||
// @Suppress("invisible_member") | ||
// throw TimeoutCancellationException() | ||
// } | ||
// } | ||
} |
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
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