in kotlinx-coroutines-test/common/src/TestScope.kt [218:243]
fun enter() {
val exceptions = synchronized(lock) {
if (entered)
throw IllegalStateException("Only a single call to `runTest` can be performed during one test.")
entered = true
check(!finished)
/** the order is important: [reportException] is only guaranteed not to throw if [entered] is `true` but
* [finished] is `false`.
* However, we also want [uncaughtExceptions] to be queried after the callback is registered,
* because the exception collector will be able to report the exceptions that arrived before this test but
* after the previous one, and learning about such exceptions as soon is possible is nice. */
@Suppress("INVISIBLE_REFERENCE", "INVISIBLE_MEMBER") // do not remove the INVISIBLE_REFERENCE suppression: required in K2
run { ensurePlatformExceptionHandlerLoaded(ExceptionCollector) }
if (catchNonTestRelatedExceptions) {
ExceptionCollector.addOnExceptionCallback(lock, this::reportException)
}
uncaughtExceptions
}
if (exceptions.isNotEmpty()) {
ExceptionCollector.removeOnExceptionCallback(lock)
throw UncaughtExceptionsBeforeTest().apply {
for (e in exceptions)
addSuppressed(e)
}
}
}