diff --git a/kotlinx-coroutines-test/common/src/TestCoroutineScope.kt b/kotlinx-coroutines-test/common/src/TestCoroutineScope.kt index a2962abf77..8b4d97139d 100644 --- a/kotlinx-coroutines-test/common/src/TestCoroutineScope.kt +++ b/kotlinx-coroutines-test/common/src/TestCoroutineScope.kt @@ -33,9 +33,9 @@ public interface TestCoroutineScope: CoroutineScope, UncaughtExceptionCaptor { ?: throw UnsupportedOperationException("This scope does not have a TestCoroutineScheduler linked to it") } -private class TestCoroutineScopeImpl ( +private class TestCoroutineScopeImpl( override val coroutineContext: CoroutineContext, - val ownJob: CompletableJob? + private val ownJob: CompletableJob? ): TestCoroutineScope, UncaughtExceptionCaptor by coroutineContext.uncaughtExceptionCaptor @@ -44,14 +44,16 @@ private class TestCoroutineScopeImpl ( get() = coroutineContext[TestCoroutineScheduler]!! /** These jobs existed before the coroutine scope was used, so it's alright if they don't get cancelled. */ - val initialJobs = coroutineContext.activeJobs() + private val initialJobs = coroutineContext.activeJobs() override fun cleanupTestCoroutines() { coroutineContext.uncaughtExceptionCaptor.cleanupTestCoroutinesCaptor() coroutineContext.delayController?.cleanupTestCoroutines() val jobs = coroutineContext.activeJobs() if ((jobs - initialJobs).isNotEmpty()) { - throw UncompletedCoroutinesError("Test finished with active jobs: $jobs") + val exception = UncompletedCoroutinesError("Test finished with active jobs: $jobs") + ownJob?.completeExceptionally(exception) + throw exception } ownJob?.complete() }