File tree 6 files changed +21
-13
lines changed
kotlinx-coroutines-test/common
6 files changed +21
-13
lines changed Original file line number Diff line number Diff line change @@ -164,7 +164,7 @@ public fun TestScope.runTest(
164
164
): TestResult = asSpecificImplementation().let {
165
165
it.enter()
166
166
createTestResult {
167
- runTestCoroutine(it, dispatchTimeoutMs, testBody) { it.finish () }
167
+ runTestCoroutine(it, dispatchTimeoutMs, testBody) { it.leave () }
168
168
}
169
169
}
170
170
Original file line number Diff line number Diff line change @@ -157,10 +157,10 @@ internal class TestScopeImpl(context: CoroutineContext) :
157
157
158
158
override val testScheduler get() = context[TestCoroutineScheduler ]!!
159
159
160
- var entered = false
161
- var finished = false
162
- val uncaughtExceptions = mutableListOf<Throwable >()
163
- val lock = SynchronizedObject ()
160
+ private var entered = false
161
+ private var finished = false
162
+ private val uncaughtExceptions = mutableListOf<Throwable >()
163
+ private val lock = SynchronizedObject ()
164
164
165
165
/* * Called upon entry to [runTest]. Will throw if called more than once. */
166
166
fun enter () {
@@ -180,7 +180,7 @@ internal class TestScopeImpl(context: CoroutineContext) :
180
180
}
181
181
182
182
/* * Called at the end of the test. May only be called once. */
183
- fun finish (): List <Throwable > {
183
+ fun leave (): List <Throwable > {
184
184
val exceptions = synchronized(lock) {
185
185
check(entered && ! finished)
186
186
finished = true
@@ -206,6 +206,9 @@ internal class TestScopeImpl(context: CoroutineContext) :
206
206
}
207
207
}
208
208
}
209
+
210
+ override fun toString (): String =
211
+ " TestScope[" + (if (finished) " test ended" else if (entered) " test started" else " test not started" ) + " ]"
209
212
}
210
213
211
214
/* * Use the knowledge that any [TestScope] that we receive is necessarily a [TestScopeImpl]. */
Original file line number Diff line number Diff line change @@ -82,7 +82,7 @@ public fun runBlockingTestOnTestScope(
82
82
deferred.getCompletionExceptionOrNull()?.let {
83
83
throw it
84
84
}
85
- scope.finish ().throwAll()
85
+ scope.leave ().throwAll()
86
86
val jobs = context.activeJobs() - startJobs
87
87
if (jobs.isNotEmpty())
88
88
throw UncompletedCoroutinesError (" Some jobs were not completed at the end of the test: $jobs " )
Original file line number Diff line number Diff line change @@ -12,10 +12,15 @@ class StandardTestDispatcherTest: OrderedExecutionTestBase() {
12
12
13
13
private val scope = TestScope (StandardTestDispatcher ())
14
14
15
+ @BeforeTest
16
+ fun init () {
17
+ scope.asSpecificImplementation().enter()
18
+ }
19
+
15
20
@AfterTest
16
21
fun cleanup () {
17
22
scope.runCurrent()
18
- assertEquals(listOf (), scope.asSpecificImplementation().finish ())
23
+ assertEquals(listOf (), scope.asSpecificImplementation().leave ())
19
24
}
20
25
21
26
/* * Tests that the [StandardTestDispatcher] follows an execution order similar to `runBlocking`. */
Original file line number Diff line number Diff line change @@ -247,7 +247,7 @@ class TestCoroutineSchedulerTest {
247
247
}
248
248
}
249
249
advanceUntilIdle()
250
- asSpecificImplementation().finish ().throwAll()
250
+ asSpecificImplementation().leave ().throwAll()
251
251
if (timesOut)
252
252
assertTrue(caughtException)
253
253
else
Original file line number Diff line number Diff line change @@ -88,7 +88,7 @@ class TestScopeTest {
88
88
}
89
89
assertFalse(result)
90
90
scope.asSpecificImplementation().enter()
91
- assertFailsWith<UncompletedCoroutinesError > { scope.asSpecificImplementation().finish () }
91
+ assertFailsWith<UncompletedCoroutinesError > { scope.asSpecificImplementation().leave () }
92
92
assertFalse(result)
93
93
}
94
94
@@ -104,7 +104,7 @@ class TestScopeTest {
104
104
}
105
105
assertFalse(result)
106
106
scope.asSpecificImplementation().enter()
107
- assertFailsWith<UncompletedCoroutinesError > { scope.asSpecificImplementation().finish () }
107
+ assertFailsWith<UncompletedCoroutinesError > { scope.asSpecificImplementation().leave () }
108
108
assertFalse(result)
109
109
}
110
110
@@ -121,7 +121,7 @@ class TestScopeTest {
121
121
job.cancel()
122
122
assertFalse(result)
123
123
scope.asSpecificImplementation().enter()
124
- assertFailsWith<UncompletedCoroutinesError > { scope.asSpecificImplementation().finish () }
124
+ assertFailsWith<UncompletedCoroutinesError > { scope.asSpecificImplementation().leave () }
125
125
assertFalse(result)
126
126
}
127
127
@@ -155,7 +155,7 @@ class TestScopeTest {
155
155
launch(SupervisorJob ()) { throw TestException (" y" ) }
156
156
launch(SupervisorJob ()) { throw TestException (" z" ) }
157
157
runCurrent()
158
- val e = asSpecificImplementation().finish ()
158
+ val e = asSpecificImplementation().leave ()
159
159
assertEquals(3 , e.size)
160
160
assertEquals(" x" , e[0 ].message)
161
161
assertEquals(" y" , e[1 ].message)
You can’t perform that action at this time.
0 commit comments