Skip to content

Commit 1e9be83

Browse files
committed
Don't complete the created job on TestCoroutineScope.cleanup
1 parent 2cddb28 commit 1e9be83

File tree

2 files changed

+9
-51
lines changed

2 files changed

+9
-51
lines changed

kotlinx-coroutines-test/common/src/TestCoroutineScope.kt

+9-26
Original file line numberDiff line numberDiff line change
@@ -32,8 +32,7 @@ public sealed interface TestCoroutineScope: CoroutineScope, UncaughtExceptionCap
3232
}
3333

3434
private class TestCoroutineScopeImpl(
35-
override val coroutineContext: CoroutineContext,
36-
private val ownJob: CompletableJob?
35+
override val coroutineContext: CoroutineContext
3736
):
3837
TestCoroutineScope,
3938
UncaughtExceptionCaptor by coroutineContext.uncaughtExceptionCaptor
@@ -45,17 +44,11 @@ private class TestCoroutineScopeImpl(
4544
private val initialJobs = coroutineContext.activeJobs()
4645

4746
override fun cleanupTestCoroutines() {
48-
try {
49-
coroutineContext.uncaughtExceptionCaptor.cleanupTestCoroutinesCaptor()
50-
coroutineContext.delayController?.cleanupTestCoroutines()
51-
val jobs = coroutineContext.activeJobs()
52-
if ((jobs - initialJobs).isNotEmpty())
53-
throw UncompletedCoroutinesError("Test finished with active jobs: $jobs")
54-
} catch (exception: Throwable) {
55-
ownJob?.completeExceptionally(exception)
56-
throw exception
57-
}
58-
ownJob?.complete()
47+
coroutineContext.uncaughtExceptionCaptor.cleanupTestCoroutinesCaptor()
48+
coroutineContext.delayController?.cleanupTestCoroutines()
49+
val jobs = coroutineContext.activeJobs()
50+
if ((jobs - initialJobs).isNotEmpty())
51+
throw UncompletedCoroutinesError("Test finished with active jobs: $jobs")
5952
}
6053
}
6154

@@ -72,9 +65,7 @@ private fun CoroutineContext.activeJobs(): Set<Job> {
7265
* * If [context] doesn't have a [ContinuationInterceptor], a [TestCoroutineDispatcher] is created.
7366
* * If [context] does not provide a [CoroutineExceptionHandler], [TestCoroutineExceptionHandler] is created
7467
* automatically.
75-
* * If [context] provides a [Job], that job is used for the new scope, but is not completed once the scope completes.
76-
* On the other hand, if there is no [Job] in the context, a [CompletableJob] is created and completed on
77-
* [TestCoroutineScope.cleanupTestCoroutines].
68+
* * If [context] provides a [Job], that job is used for the new scope; otherwise, a [CompletableJob] is created.
7869
*
7970
* @throws IllegalArgumentException if [context] has both [TestCoroutineScheduler] and a [TestDispatcher] linked to a
8071
* different scheduler.
@@ -112,16 +103,8 @@ public fun TestCoroutineScope(context: CoroutineContext = EmptyCoroutineContext)
112103
}
113104
this ?: TestCoroutineExceptionHandler()
114105
}
115-
val job: Job
116-
val ownJob: CompletableJob?
117-
if (context[Job] == null) {
118-
ownJob = SupervisorJob()
119-
job = ownJob
120-
} else {
121-
ownJob = null
122-
job = context[Job]!!
123-
}
124-
return TestCoroutineScopeImpl(context + scheduler + dispatcher + exceptionHandler + job, ownJob)
106+
val job: Job = context[Job] ?: SupervisorJob()
107+
return TestCoroutineScopeImpl(context + scheduler + dispatcher + exceptionHandler + job)
125108
}
126109

127110
private inline val CoroutineContext.uncaughtExceptionCaptor: UncaughtExceptionCaptor

kotlinx-coroutines-test/common/test/TestCoroutineScopeTest.kt

-25
Original file line numberDiff line numberDiff line change
@@ -95,31 +95,6 @@ class TestCoroutineScopeTest {
9595
assertFalse(result)
9696
}
9797

98-
/** Tests that the coroutine scope completes its job if the job was not passed to it as an argument. */
99-
@Test
100-
fun testCompletesOwnJob() {
101-
val scope = TestCoroutineScope()
102-
var handlerCalled = false
103-
scope.coroutineContext.job.invokeOnCompletion {
104-
handlerCalled = true
105-
}
106-
scope.cleanupTestCoroutines()
107-
assertTrue(handlerCalled)
108-
}
109-
110-
/** Tests that the coroutine scope completes its job if the job was not passed to it as an argument. */
111-
@Test
112-
fun testDoesNotCompleteGivenJob() {
113-
var handlerCalled = false
114-
val job = Job()
115-
job.invokeOnCompletion {
116-
handlerCalled = true
117-
}
118-
val scope = TestCoroutineScope(job)
119-
scope.cleanupTestCoroutines()
120-
assertFalse(handlerCalled)
121-
}
122-
12398
private val invalidContexts = listOf(
12499
Dispatchers.Default, // not a [TestDispatcher]
125100
TestCoroutineDispatcher() + TestCoroutineScheduler(), // the dispatcher is not linked to the scheduler

0 commit comments

Comments
 (0)