@@ -32,8 +32,7 @@ public sealed interface TestCoroutineScope: CoroutineScope, UncaughtExceptionCap
32
32
}
33
33
34
34
private class TestCoroutineScopeImpl (
35
- override val coroutineContext : CoroutineContext ,
36
- private val ownJob : CompletableJob ?
35
+ override val coroutineContext : CoroutineContext
37
36
):
38
37
TestCoroutineScope ,
39
38
UncaughtExceptionCaptor by coroutineContext.uncaughtExceptionCaptor
@@ -45,17 +44,11 @@ private class TestCoroutineScopeImpl(
45
44
private val initialJobs = coroutineContext.activeJobs()
46
45
47
46
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 " )
59
52
}
60
53
}
61
54
@@ -72,9 +65,7 @@ private fun CoroutineContext.activeJobs(): Set<Job> {
72
65
* * If [context] doesn't have a [ContinuationInterceptor], a [TestCoroutineDispatcher] is created.
73
66
* * If [context] does not provide a [CoroutineExceptionHandler], [TestCoroutineExceptionHandler] is created
74
67
* 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.
78
69
*
79
70
* @throws IllegalArgumentException if [context] has both [TestCoroutineScheduler] and a [TestDispatcher] linked to a
80
71
* different scheduler.
@@ -112,16 +103,8 @@ public fun TestCoroutineScope(context: CoroutineContext = EmptyCoroutineContext)
112
103
}
113
104
this ? : TestCoroutineExceptionHandler ()
114
105
}
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)
125
108
}
126
109
127
110
private inline val CoroutineContext .uncaughtExceptionCaptor: UncaughtExceptionCaptor
0 commit comments