Skip to content

Commit 15d1dec

Browse files
committed
Fixes
1 parent ba0cccd commit 15d1dec

File tree

2 files changed

+22
-25
lines changed

2 files changed

+22
-25
lines changed

kotlinx-coroutines-test/common/src/TestBuilders.kt

+19-22
Original file line numberDiff line numberDiff line change
@@ -87,7 +87,7 @@ public fun TestCoroutineDispatcher.runBlockingTest(block: suspend TestCoroutineS
8787
* * Don't nest functions returning a [TestResult].
8888
*/
8989
@Suppress("NO_ACTUAL_FOR_EXPECT")
90-
@DelicateCoroutinesApi
90+
@ExperimentalCoroutinesApi
9191
public expect class TestResult
9292

9393
/**
@@ -159,7 +159,7 @@ public expect class TestResult
159159
*
160160
* In the general case, if there are active jobs, it's impossible to detect if they are going to complete eventually due
161161
* to the asynchronous nature of coroutines. In order to prevent tests hanging in this scenario, [runTest] will wait
162-
* for [dispatchTimeoutMs] milliseconds (by default, 10 seconds) from the moment when [TestCoroutineScheduler] becomes
162+
* for [dispatchTimeoutMs] milliseconds (by default, 60 seconds) from the moment when [TestCoroutineScheduler] becomes
163163
* idle before throwing [AssertionError]. If some dispatcher linked to [TestCoroutineScheduler] receives a
164164
* task during that time, the timer gets reset.
165165
*
@@ -172,7 +172,7 @@ public expect class TestResult
172172
* @throws IllegalArgumentException if the [context] is invalid. See the [TestCoroutineScope] constructor docs for
173173
* details.
174174
*/
175-
@DelicateCoroutinesApi
175+
@ExperimentalCoroutinesApi
176176
public fun runTest(
177177
context: CoroutineContext = EmptyCoroutineContext,
178178
dispatchTimeoutMs: Long = DEFAULT_DISPATCH_TIMEOUT_MS,
@@ -197,24 +197,21 @@ public fun runTest(
197197
completed = true
198198
continue
199199
}
200-
try {
201-
withTimeout(dispatchTimeoutMs) {
202-
select<Unit> {
203-
testScope.onJoin {
204-
completed = true
205-
}
206-
scheduler.onDispatchEvent {
207-
// we received knowledge that `scheduler` observed a dispatch event, so we reset the timeout
208-
}
209-
}
200+
select<Unit> {
201+
testScope.onJoin {
202+
completed = true
210203
}
211-
} catch (e: TimeoutCancellationException) {
212-
try {
213-
testScope.cleanupTestCoroutines()
214-
} catch (e: UncompletedCoroutinesError) {
215-
// we expect these and will instead throw a more informative exception just below.
204+
scheduler.onDispatchEvent {
205+
// we received knowledge that `scheduler` observed a dispatch event, so we reset the timeout
206+
}
207+
onTimeout(dispatchTimeoutMs) {
208+
try {
209+
testScope.cleanupTestCoroutines()
210+
} catch (e: UncompletedCoroutinesError) {
211+
// we expect these and will instead throw a more informative exception just below.
212+
}
213+
throw UncompletedCoroutinesError("The test coroutine was not completed after waiting for $dispatchTimeoutMs ms")
216214
}
217-
throw UncompletedCoroutinesError("The test coroutine was not completed after waiting for $dispatchTimeoutMs ms")
218215
}
219216
}
220217
testScope.getCompletionExceptionOrNull()?.let {
@@ -248,7 +245,7 @@ internal expect fun createTestResult(testProcedure: suspend () -> Unit): TestRes
248245
* Since this function returns [TestResult], in order to work correctly on the JS, its result must be returned
249246
* immediately from the test body. See the docs for [TestResult] for details.
250247
*/
251-
@DelicateCoroutinesApi
248+
@ExperimentalCoroutinesApi
252249
public fun TestCoroutineScope.runTest(
253250
dispatchTimeoutMs: Long = DEFAULT_DISPATCH_TIMEOUT_MS,
254251
block: suspend TestCoroutineScope.() -> Unit
@@ -263,7 +260,7 @@ public fun TestCoroutineScope.runTest(
263260
* Since this function returns [TestResult], in order to work correctly on the JS, its result must be returned
264261
* immediately from the test body. See the docs for [TestResult] for details.
265262
*/
266-
@DelicateCoroutinesApi
263+
@ExperimentalCoroutinesApi
267264
public fun TestDispatcher.runTest(
268265
dispatchTimeoutMs: Long = DEFAULT_DISPATCH_TIMEOUT_MS,
269266
block: suspend TestCoroutineScope.() -> Unit
@@ -280,7 +277,7 @@ private object RunningInRunTest: CoroutineContext.Key<RunningInRunTest>, Corouti
280277

281278
/** The default timeout to use when waiting for asynchronous completions of the coroutines managed by
282279
* a [TestCoroutineScheduler]. */
283-
private const val DEFAULT_DISPATCH_TIMEOUT_MS = 10_000L
280+
private const val DEFAULT_DISPATCH_TIMEOUT_MS = 60_000L
284281

285282
private class TestBodyCoroutine<T>(
286283
private val testScope: TestCoroutineScope,

kotlinx-coroutines-test/common/test/RunTestTest.kt

+3-3
Original file line numberDiff line numberDiff line change
@@ -78,7 +78,7 @@ class RunTestTest {
7878
delay(10)
7979
3
8080
}
81-
throw IllegalStateException("shouldn't be reached")
81+
fail("shouldn't be reached")
8282
}
8383
}
8484

@@ -93,7 +93,7 @@ class RunTestTest {
9393
delay(10000)
9494
3
9595
}
96-
throw RuntimeException("shouldn't be reached")
96+
fail("shouldn't be reached")
9797
}
9898
}
9999

@@ -117,7 +117,7 @@ class RunTestTest {
117117
delay(10000)
118118
3
119119
}
120-
throw RuntimeException("shouldn't be reached")
120+
fail("shouldn't be reached")
121121
}
122122
}
123123

0 commit comments

Comments
 (0)