-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
test: add
withTimeoutSafe
and delaySafe
as a safe replacements fo…
…r `withTimeout`/`delay` when used inside `TestScope` More info: Kotlin/kotlinx.coroutines#3588 https://github.com/Kotlin/kotlinx.coroutines/blob/dea2ca5/kotlinx-coroutines-test/README.md#using-withtimeout-inside-runtest Signed-off-by: Artyom Shendrik <[email protected]>
- Loading branch information
Showing
10 changed files
with
269 additions
and
33 deletions.
There are no files selected for viewing
26 changes: 26 additions & 0 deletions
26
fluxo-core/src/commonTest/kotlin/kt/fluxo/test/DelaySafe.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,26 @@ | ||
package kt.fluxo.test | ||
|
||
import kotlinx.coroutines.Dispatchers | ||
import kotlinx.coroutines.delay | ||
import kotlinx.coroutines.test.TestScope | ||
import kotlinx.coroutines.test.runTest | ||
import kotlinx.coroutines.withContext | ||
|
||
/** | ||
* Use as a safe replacement for [delay] when used inside a [runTest]/[TestScope]. | ||
* | ||
* Delays coroutine for a given time without blocking a thread and resumes it after a specified time. | ||
* | ||
* [Documentation on delay-skipping in tests](https://github.com/Kotlin/kotlinx.coroutines/blob/81e17dd/kotlinx-coroutines-test/README.md#delay-skipping) | ||
* | ||
* @param timeMillis timeout time in milliseconds. | ||
* | ||
* @see delay | ||
* @see withTimeoutSafe | ||
*/ | ||
@Suppress("MaxLineLength") | ||
suspend fun delaySafe(timeMillis: Long) { | ||
withContext(Dispatchers.Default) { | ||
delay(timeMillis) | ||
} | ||
} |
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
42 changes: 42 additions & 0 deletions
42
fluxo-core/src/commonTest/kotlin/kt/fluxo/test/WithTimeoutSafe.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,42 @@ | ||
package kt.fluxo.test | ||
|
||
import kotlinx.coroutines.CoroutineScope | ||
import kotlinx.coroutines.Dispatchers | ||
import kotlinx.coroutines.TimeoutCancellationException | ||
import kotlinx.coroutines.currentCoroutineContext | ||
import kotlinx.coroutines.test.TestScope | ||
import kotlinx.coroutines.test.runTest | ||
import kotlinx.coroutines.withContext | ||
import kotlinx.coroutines.withTimeout | ||
import kotlin.contracts.InvocationKind | ||
import kotlin.contracts.contract | ||
|
||
/** | ||
* Use as a safe replacement for [withTimeout] when used inside a [runTest]/[TestScope]. | ||
* | ||
* Runs a given suspending [block] of code inside a coroutine with a specified [timeout][timeMillis] and throws | ||
* a [TimeoutCancellationException] if the timeout was exceeded. | ||
* | ||
* [Issue with the problem](https://github.com/Kotlin/kotlinx.coroutines/issues/3588) | ||
* | ||
* [Documented as intended behavior](https://github.com/Kotlin/kotlinx.coroutines/blob/master/kotlinx-coroutines-test/README.md#using-withtimeout-inside-runtest) | ||
* | ||
* @param timeMillis timeout time in milliseconds. | ||
* | ||
* @see withTimeout | ||
* @see delaySafe | ||
*/ | ||
@Suppress("MaxLineLength") | ||
suspend inline fun <T> withTimeoutSafe(timeMillis: Long, crossinline block: suspend CoroutineScope.() -> T): T { | ||
contract { | ||
callsInPlace(block, InvocationKind.EXACTLY_ONCE) | ||
} | ||
val context = currentCoroutineContext() | ||
return withContext(Dispatchers.Default) { | ||
withTimeout(timeMillis) { | ||
withContext(context) { | ||
block() | ||
} | ||
} | ||
} | ||
} |
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
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
Oops, something went wrong.