From 15d1decb9a54a509a8b7ca626582c0b1ef911b62 Mon Sep 17 00:00:00 2001 From: Dmitry Khalanskiy Date: Thu, 28 Oct 2021 12:35:12 +0300 Subject: [PATCH] Fixes --- .../common/src/TestBuilders.kt | 41 +++++++++---------- .../common/test/RunTestTest.kt | 6 +-- 2 files changed, 22 insertions(+), 25 deletions(-) diff --git a/kotlinx-coroutines-test/common/src/TestBuilders.kt b/kotlinx-coroutines-test/common/src/TestBuilders.kt index 1867999830..ecdd8fb80f 100644 --- a/kotlinx-coroutines-test/common/src/TestBuilders.kt +++ b/kotlinx-coroutines-test/common/src/TestBuilders.kt @@ -87,7 +87,7 @@ public fun TestCoroutineDispatcher.runBlockingTest(block: suspend TestCoroutineS * * Don't nest functions returning a [TestResult]. */ @Suppress("NO_ACTUAL_FOR_EXPECT") -@DelicateCoroutinesApi +@ExperimentalCoroutinesApi public expect class TestResult /** @@ -159,7 +159,7 @@ public expect class TestResult * * In the general case, if there are active jobs, it's impossible to detect if they are going to complete eventually due * to the asynchronous nature of coroutines. In order to prevent tests hanging in this scenario, [runTest] will wait - * for [dispatchTimeoutMs] milliseconds (by default, 10 seconds) from the moment when [TestCoroutineScheduler] becomes + * for [dispatchTimeoutMs] milliseconds (by default, 60 seconds) from the moment when [TestCoroutineScheduler] becomes * idle before throwing [AssertionError]. If some dispatcher linked to [TestCoroutineScheduler] receives a * task during that time, the timer gets reset. * @@ -172,7 +172,7 @@ public expect class TestResult * @throws IllegalArgumentException if the [context] is invalid. See the [TestCoroutineScope] constructor docs for * details. */ -@DelicateCoroutinesApi +@ExperimentalCoroutinesApi public fun runTest( context: CoroutineContext = EmptyCoroutineContext, dispatchTimeoutMs: Long = DEFAULT_DISPATCH_TIMEOUT_MS, @@ -197,24 +197,21 @@ public fun runTest( completed = true continue } - try { - withTimeout(dispatchTimeoutMs) { - select { - testScope.onJoin { - completed = true - } - scheduler.onDispatchEvent { - // we received knowledge that `scheduler` observed a dispatch event, so we reset the timeout - } - } + select { + testScope.onJoin { + completed = true } - } catch (e: TimeoutCancellationException) { - try { - testScope.cleanupTestCoroutines() - } catch (e: UncompletedCoroutinesError) { - // we expect these and will instead throw a more informative exception just below. + scheduler.onDispatchEvent { + // we received knowledge that `scheduler` observed a dispatch event, so we reset the timeout + } + onTimeout(dispatchTimeoutMs) { + try { + testScope.cleanupTestCoroutines() + } catch (e: UncompletedCoroutinesError) { + // we expect these and will instead throw a more informative exception just below. + } + throw UncompletedCoroutinesError("The test coroutine was not completed after waiting for $dispatchTimeoutMs ms") } - throw UncompletedCoroutinesError("The test coroutine was not completed after waiting for $dispatchTimeoutMs ms") } } testScope.getCompletionExceptionOrNull()?.let { @@ -248,7 +245,7 @@ internal expect fun createTestResult(testProcedure: suspend () -> Unit): TestRes * Since this function returns [TestResult], in order to work correctly on the JS, its result must be returned * immediately from the test body. See the docs for [TestResult] for details. */ -@DelicateCoroutinesApi +@ExperimentalCoroutinesApi public fun TestCoroutineScope.runTest( dispatchTimeoutMs: Long = DEFAULT_DISPATCH_TIMEOUT_MS, block: suspend TestCoroutineScope.() -> Unit @@ -263,7 +260,7 @@ public fun TestCoroutineScope.runTest( * Since this function returns [TestResult], in order to work correctly on the JS, its result must be returned * immediately from the test body. See the docs for [TestResult] for details. */ -@DelicateCoroutinesApi +@ExperimentalCoroutinesApi public fun TestDispatcher.runTest( dispatchTimeoutMs: Long = DEFAULT_DISPATCH_TIMEOUT_MS, block: suspend TestCoroutineScope.() -> Unit @@ -280,7 +277,7 @@ private object RunningInRunTest: CoroutineContext.Key, Corouti /** The default timeout to use when waiting for asynchronous completions of the coroutines managed by * a [TestCoroutineScheduler]. */ -private const val DEFAULT_DISPATCH_TIMEOUT_MS = 10_000L +private const val DEFAULT_DISPATCH_TIMEOUT_MS = 60_000L private class TestBodyCoroutine( private val testScope: TestCoroutineScope, diff --git a/kotlinx-coroutines-test/common/test/RunTestTest.kt b/kotlinx-coroutines-test/common/test/RunTestTest.kt index e086fea9ea..d64e7c327e 100644 --- a/kotlinx-coroutines-test/common/test/RunTestTest.kt +++ b/kotlinx-coroutines-test/common/test/RunTestTest.kt @@ -78,7 +78,7 @@ class RunTestTest { delay(10) 3 } - throw IllegalStateException("shouldn't be reached") + fail("shouldn't be reached") } } @@ -93,7 +93,7 @@ class RunTestTest { delay(10000) 3 } - throw RuntimeException("shouldn't be reached") + fail("shouldn't be reached") } } @@ -117,7 +117,7 @@ class RunTestTest { delay(10000) 3 } - throw RuntimeException("shouldn't be reached") + fail("shouldn't be reached") } }