From 0a60a3b0cdd50ba82852b5a28508c34cd1246533 Mon Sep 17 00:00:00 2001 From: Dmitry Khalanskiy Date: Wed, 27 Oct 2021 10:43:26 +0300 Subject: [PATCH] Fix ownJob not always completing --- .../common/src/TestCoroutineScope.kt | 12 +++++++----- 1 file changed, 7 insertions(+), 5 deletions(-) diff --git a/kotlinx-coroutines-test/common/src/TestCoroutineScope.kt b/kotlinx-coroutines-test/common/src/TestCoroutineScope.kt index ec7c332410..69af8cd764 100644 --- a/kotlinx-coroutines-test/common/src/TestCoroutineScope.kt +++ b/kotlinx-coroutines-test/common/src/TestCoroutineScope.kt @@ -45,11 +45,13 @@ private class TestCoroutineScopeImpl( private val initialJobs = coroutineContext.activeJobs() override fun cleanupTestCoroutines() { - coroutineContext.uncaughtExceptionCaptor.cleanupTestCoroutinesCaptor() - coroutineContext.delayController?.cleanupTestCoroutines() - val jobs = coroutineContext.activeJobs() - if ((jobs - initialJobs).isNotEmpty()) { - val exception = UncompletedCoroutinesError("Test finished with active jobs: $jobs") + try { + coroutineContext.uncaughtExceptionCaptor.cleanupTestCoroutinesCaptor() + coroutineContext.delayController?.cleanupTestCoroutines() + val jobs = coroutineContext.activeJobs() + if ((jobs - initialJobs).isNotEmpty()) + throw UncompletedCoroutinesError("Test finished with active jobs: $jobs") + } catch (exception: Throwable) { ownJob?.completeExceptionally(exception) throw exception }