in kotlinx-coroutines-test/jvm/src/migration/TestBuildersDeprecated.kt [78:110]
public fun runBlockingTestOnTestScope(
context: CoroutineContext = EmptyCoroutineContext,
testBody: suspend TestScope.() -> Unit
) {
val completeContext = TestCoroutineDispatcher() + SupervisorJob() + context
val startJobs = completeContext.activeJobs()
val scope = TestScope(completeContext).asSpecificImplementation()
scope.enter()
scope.start(CoroutineStart.UNDISPATCHED, scope) {
scope.testBody()
}
scope.testScheduler.advanceUntilIdle()
val throwable = try {
scope.getCompletionExceptionOrNull()
} catch (e: IllegalStateException) {
null // the deferred was not completed yet; `scope.legacyLeave()` should complain then about unfinished jobs
}
scope.backgroundScope.cancel()
scope.testScheduler.advanceUntilIdleOr { false }
throwable?.let {
val exceptions = try {
scope.legacyLeave()
} catch (e: UncompletedCoroutinesError) {
listOf()
}
throwAll(it, exceptions)
return
}
throwAll(null, scope.legacyLeave())
val jobs = completeContext.activeJobs() - startJobs
if (jobs.isNotEmpty())
throw UncompletedCoroutinesError("Some jobs were not completed at the end of the test: $jobs")
}