Skip to content

Commit

Permalink
Simplify TestCoroutineScope cleanup
Browse files Browse the repository at this point in the history
  • Loading branch information
dkhalanskyjb committed Oct 28, 2021
1 parent d327ae4 commit e821121
Showing 1 changed file with 14 additions and 10 deletions.
24 changes: 14 additions & 10 deletions kotlinx-coroutines-test/common/src/TestCoroutineScope.kt
Expand Up @@ -45,19 +45,23 @@ private class TestCoroutineScopeImpl(

override fun cleanupTestCoroutines() {
val delayController = coroutineContext.delayController
if (delayController != null) {
delayController.cleanupTestCoroutines()
coroutineContext.uncaughtExceptionCaptor.cleanupTestCoroutinesCaptor()
val hasUnfinishedJobs = if (delayController != null) {
try {
delayController.cleanupTestCoroutines()
false
} catch (e: UncompletedCoroutinesError) {
true
}
} else {
testScheduler.runCurrent()
coroutineContext.uncaughtExceptionCaptor.cleanupTestCoroutinesCaptor()
if (!testScheduler.isIdle()) {
throw UncompletedCoroutinesError(
"Unfinished coroutines during teardown. Ensure all coroutines are" +
" completed or cancelled by your test."
)
}
!testScheduler.isIdle()
}
coroutineContext.uncaughtExceptionCaptor.cleanupTestCoroutinesCaptor()
if (hasUnfinishedJobs)
throw UncompletedCoroutinesError(
"Unfinished coroutines during teardown. Ensure all coroutines are" +
" completed or cancelled by your test."
)
val jobs = coroutineContext.activeJobs()
if ((jobs - initialJobs).isNotEmpty())
throw UncompletedCoroutinesError("Test finished with active jobs: $jobs")
Expand Down

0 comments on commit e821121

Please sign in to comment.