Skip to content

Commit 637fb09

Browse files
committed
More tests
1 parent b9ba46a commit 637fb09

File tree

3 files changed

+89
-11
lines changed

3 files changed

+89
-11
lines changed

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

+3-1
Original file line numberDiff line numberDiff line change
@@ -60,4 +60,6 @@ open class OrderedExecutionTestBase {
6060
fun ensureFinishCalls() {
6161
assertTrue(finished.value || actionIndex.value == 0, "Expected `finish` to be called")
6262
}
63-
}
63+
}
64+
65+
internal fun <T> T.void() { }

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

+39-9
Original file line numberDiff line numberDiff line change
@@ -10,18 +10,48 @@ import kotlin.test.*
1010

1111
class StandardTestDispatcherTest: OrderedExecutionTestBase() {
1212

13+
private val scope = createTestCoroutineScope(StandardTestDispatcher())
14+
15+
@AfterTest
16+
fun cleanup() = scope.cleanupTestCoroutines()
17+
1318
/** Tests that the [StandardTestDispatcher] follows an execution order similar to `runBlocking`. */
1419
@Test
15-
fun testFlowsNotSkippingValues() {
16-
val scope = createTestCoroutineScope(StandardTestDispatcher())
20+
fun testFlowsNotSkippingValues() = scope.launch {
21+
// https://github.com/Kotlin/kotlinx.coroutines/issues/1626#issuecomment-554632852
22+
val list = flowOf(1).onStart { emit(0) }
23+
.combine(flowOf("A")) { int, str -> "$str$int" }
24+
.toList()
25+
assertEquals(list, listOf("A0", "A1"))
26+
}.void()
27+
28+
/** Tests that each [launch] gets dispatched. */
29+
@Test
30+
fun testLaunchDispatched() = scope.launch {
31+
expect(1)
32+
launch {
33+
expect(3)
34+
}
35+
finish(2)
36+
}.void()
37+
38+
/** Tests that dispatching is done in a predictable order and [yield] puts this task at the end of the queue. */
39+
@Test
40+
fun testYield() = scope.launch {
41+
expect(1)
42+
scope.launch {
43+
expect(3)
44+
yield()
45+
expect(6)
46+
}
1747
scope.launch {
18-
// https://github.com/Kotlin/kotlinx.coroutines/issues/1626#issuecomment-554632852
19-
val list = flowOf(1).onStart { emit(0) }
20-
.combine(flowOf("A")) { int, str -> "$str$int" }
21-
.toList()
22-
assertEquals(list, listOf("A0", "A1"))
48+
expect(4)
49+
yield()
50+
finish(7)
2351
}
24-
scope.cleanupTestCoroutines()
25-
}
52+
expect(2)
53+
yield()
54+
expect(5)
55+
}.void()
2656

2757
}

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

+47-1
Original file line numberDiff line numberDiff line change
@@ -193,6 +193,46 @@ class TestCoroutineSchedulerTest {
193193
scope.runCurrent()
194194
}
195195

196+
/** Tests that dispatching the delayed tasks is ordered by their waking times. */
197+
@Test
198+
fun testDelaysPriority() = forTestDispatchers {
199+
val scope = createTestCoroutineScope(it)
200+
var lastMeasurement = 0L
201+
fun checkTime(time: Long) {
202+
assertTrue(lastMeasurement < time)
203+
assertEquals(time, scope.currentTime)
204+
lastMeasurement = scope.currentTime
205+
}
206+
scope.launch {
207+
launch {
208+
delay(100)
209+
checkTime(100)
210+
val deferred = async {
211+
delay(70)
212+
checkTime(170)
213+
}
214+
delay(1)
215+
checkTime(101)
216+
deferred.await()
217+
delay(1)
218+
checkTime(171)
219+
}
220+
launch {
221+
delay(200)
222+
checkTime(200)
223+
}
224+
launch {
225+
delay(150)
226+
checkTime(150)
227+
delay(22)
228+
checkTime(172)
229+
}
230+
delay(201)
231+
}
232+
scope.advanceUntilIdle()
233+
checkTime(201)
234+
}
235+
196236
private fun TestCoroutineScope.checkTimeout(
197237
timesOut: Boolean, timeoutMillis: Long = SLOW, block: suspend () -> Unit
198238
) = assertRunsFast {
@@ -269,7 +309,13 @@ class TestCoroutineSchedulerTest {
269309
}
270310

271311
private fun forTestDispatchers(block: (TestDispatcher) -> Unit): Unit =
272-
listOf(TestCoroutineDispatcher(), StandardTestDispatcher(), UnconfinedTestDispatcher()).forEach {
312+
@Suppress("DEPRECATION")
313+
listOf(
314+
TestCoroutineDispatcher(),
315+
TestCoroutineDispatcher().also { it.pauseDispatcher() },
316+
StandardTestDispatcher(),
317+
UnconfinedTestDispatcher()
318+
).forEach {
273319
try {
274320
block(it)
275321
} catch (e: Throwable) {

0 commit comments

Comments
 (0)