in kotlinx-coroutines-test/jvm/src/migration/TestCoroutineScope.kt [69:100]
override fun cleanupTestCoroutines() {
val delayController = coroutineContext.delayController
val hasUnfinishedJobs = if (delayController != null) {
try {
delayController.cleanupTestCoroutines()
false
} catch (_: UncompletedCoroutinesError) {
true
}
} else {
testScheduler.runCurrent()
!testScheduler.isIdle(strict = false)
}
(coroutineContext[CoroutineExceptionHandler] as? TestCoroutineExceptionHandler)?.cleanupTestCoroutines()
synchronized(lock) {
if (cleanedUp)
throw IllegalStateException("Attempting to clean up a test coroutine scope more than once.")
cleanedUp = true
}
exceptions.firstOrNull()?.let { toThrow ->
exceptions.drop(1).forEach { toThrow.addSuppressed(it) }
throw toThrow
}
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")
}