Skip to content

Commit 78d1698

Browse files
committed
Fixes
1 parent 14338a7 commit 78d1698

File tree

6 files changed

+21
-13
lines changed

6 files changed

+21
-13
lines changed

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

+1-1
Original file line numberDiff line numberDiff line change
@@ -164,7 +164,7 @@ public fun TestScope.runTest(
164164
): TestResult = asSpecificImplementation().let {
165165
it.enter()
166166
createTestResult {
167-
runTestCoroutine(it, dispatchTimeoutMs, testBody) { it.finish() }
167+
runTestCoroutine(it, dispatchTimeoutMs, testBody) { it.leave() }
168168
}
169169
}
170170

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

+8-5
Original file line numberDiff line numberDiff line change
@@ -157,10 +157,10 @@ internal class TestScopeImpl(context: CoroutineContext) :
157157

158158
override val testScheduler get() = context[TestCoroutineScheduler]!!
159159

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()
164164

165165
/** Called upon entry to [runTest]. Will throw if called more than once. */
166166
fun enter() {
@@ -180,7 +180,7 @@ internal class TestScopeImpl(context: CoroutineContext) :
180180
}
181181

182182
/** Called at the end of the test. May only be called once. */
183-
fun finish(): List<Throwable> {
183+
fun leave(): List<Throwable> {
184184
val exceptions = synchronized(lock) {
185185
check(entered && !finished)
186186
finished = true
@@ -206,6 +206,9 @@ internal class TestScopeImpl(context: CoroutineContext) :
206206
}
207207
}
208208
}
209+
210+
override fun toString(): String =
211+
"TestScope[" + (if (finished) "test ended" else if (entered) "test started" else "test not started") + "]"
209212
}
210213

211214
/** Use the knowledge that any [TestScope] that we receive is necessarily a [TestScopeImpl]. */

kotlinx-coroutines-test/common/src/migration/TestBuildersDeprecated.kt

+1-1
Original file line numberDiff line numberDiff line change
@@ -82,7 +82,7 @@ public fun runBlockingTestOnTestScope(
8282
deferred.getCompletionExceptionOrNull()?.let {
8383
throw it
8484
}
85-
scope.finish().throwAll()
85+
scope.leave().throwAll()
8686
val jobs = context.activeJobs() - startJobs
8787
if (jobs.isNotEmpty())
8888
throw UncompletedCoroutinesError("Some jobs were not completed at the end of the test: $jobs")

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

+6-1
Original file line numberDiff line numberDiff line change
@@ -12,10 +12,15 @@ class StandardTestDispatcherTest: OrderedExecutionTestBase() {
1212

1313
private val scope = TestScope(StandardTestDispatcher())
1414

15+
@BeforeTest
16+
fun init() {
17+
scope.asSpecificImplementation().enter()
18+
}
19+
1520
@AfterTest
1621
fun cleanup() {
1722
scope.runCurrent()
18-
assertEquals(listOf(), scope.asSpecificImplementation().finish())
23+
assertEquals(listOf(), scope.asSpecificImplementation().leave())
1924
}
2025

2126
/** Tests that the [StandardTestDispatcher] follows an execution order similar to `runBlocking`. */

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

+1-1
Original file line numberDiff line numberDiff line change
@@ -247,7 +247,7 @@ class TestCoroutineSchedulerTest {
247247
}
248248
}
249249
advanceUntilIdle()
250-
asSpecificImplementation().finish().throwAll()
250+
asSpecificImplementation().leave().throwAll()
251251
if (timesOut)
252252
assertTrue(caughtException)
253253
else

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

+4-4
Original file line numberDiff line numberDiff line change
@@ -88,7 +88,7 @@ class TestScopeTest {
8888
}
8989
assertFalse(result)
9090
scope.asSpecificImplementation().enter()
91-
assertFailsWith<UncompletedCoroutinesError> { scope.asSpecificImplementation().finish() }
91+
assertFailsWith<UncompletedCoroutinesError> { scope.asSpecificImplementation().leave() }
9292
assertFalse(result)
9393
}
9494

@@ -104,7 +104,7 @@ class TestScopeTest {
104104
}
105105
assertFalse(result)
106106
scope.asSpecificImplementation().enter()
107-
assertFailsWith<UncompletedCoroutinesError> { scope.asSpecificImplementation().finish() }
107+
assertFailsWith<UncompletedCoroutinesError> { scope.asSpecificImplementation().leave() }
108108
assertFalse(result)
109109
}
110110

@@ -121,7 +121,7 @@ class TestScopeTest {
121121
job.cancel()
122122
assertFalse(result)
123123
scope.asSpecificImplementation().enter()
124-
assertFailsWith<UncompletedCoroutinesError> { scope.asSpecificImplementation().finish() }
124+
assertFailsWith<UncompletedCoroutinesError> { scope.asSpecificImplementation().leave() }
125125
assertFalse(result)
126126
}
127127

@@ -155,7 +155,7 @@ class TestScopeTest {
155155
launch(SupervisorJob()) { throw TestException("y") }
156156
launch(SupervisorJob()) { throw TestException("z") }
157157
runCurrent()
158-
val e = asSpecificImplementation().finish()
158+
val e = asSpecificImplementation().leave()
159159
assertEquals(3, e.size)
160160
assertEquals("x", e[0].message)
161161
assertEquals("y", e[1].message)

0 commit comments

Comments
 (0)