Skip to content

Commit f087205

Browse files
committed
Simplify TestCoroutineScope cleanup
1 parent 75356b3 commit f087205

File tree

1 file changed

+14
-10
lines changed

1 file changed

+14
-10
lines changed

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

+14-10
Original file line numberDiff line numberDiff line change
@@ -45,19 +45,23 @@ private class TestCoroutineScopeImpl(
4545

4646
override fun cleanupTestCoroutines() {
4747
val delayController = coroutineContext.delayController
48-
if (delayController != null) {
49-
delayController.cleanupTestCoroutines()
50-
coroutineContext.uncaughtExceptionCaptor.cleanupTestCoroutinesCaptor()
48+
val hasUnfinishedJobs = if (delayController != null) {
49+
try {
50+
delayController.cleanupTestCoroutines()
51+
false
52+
} catch (e: UncompletedCoroutinesError) {
53+
true
54+
}
5155
} else {
5256
testScheduler.runCurrent()
53-
coroutineContext.uncaughtExceptionCaptor.cleanupTestCoroutinesCaptor()
54-
if (!testScheduler.isIdle()) {
55-
throw UncompletedCoroutinesError(
56-
"Unfinished coroutines during teardown. Ensure all coroutines are" +
57-
" completed or cancelled by your test."
58-
)
59-
}
57+
!testScheduler.isIdle()
6058
}
59+
coroutineContext.uncaughtExceptionCaptor.cleanupTestCoroutinesCaptor()
60+
if (hasUnfinishedJobs)
61+
throw UncompletedCoroutinesError(
62+
"Unfinished coroutines during teardown. Ensure all coroutines are" +
63+
" completed or cancelled by your test."
64+
)
6165
val jobs = coroutineContext.activeJobs()
6266
if ((jobs - initialJobs).isNotEmpty())
6367
throw UncompletedCoroutinesError("Test finished with active jobs: $jobs")

0 commit comments

Comments
 (0)