@@ -12,7 +12,7 @@ class TestCoroutineSchedulerTest {
12
12
@Test
13
13
fun testContextElement () = runTest {
14
14
assertFailsWith<IllegalStateException > {
15
- withContext(TestCoroutineDispatcher ()) {
15
+ withContext(StandardTestDispatcher ()) {
16
16
}
17
17
}
18
18
}
@@ -44,28 +44,35 @@ class TestCoroutineSchedulerTest {
44
44
/* * Tests that if [TestCoroutineScheduler.advanceTimeBy] encounters an arithmetic overflow, all the tasks scheduled
45
45
* until the moment [Long.MAX_VALUE] get run. */
46
46
@Test
47
- fun testAdvanceTimeByEnormousDelays () = runTest {
48
- val initialDelay = 10L
49
- delay(initialDelay)
50
- assertEquals(initialDelay, currentTime)
51
- var enteredInfinity = false
52
- launch {
53
- delay(Long .MAX_VALUE - 1 ) // delay(Long.MAX_VALUE) does nothing
54
- assertEquals(Long .MAX_VALUE , currentTime)
55
- enteredInfinity = true
56
- }
57
- var enteredNearInfinity = false
58
- launch {
59
- delay(Long .MAX_VALUE - initialDelay - 1 )
60
- assertEquals(Long .MAX_VALUE - 1 , currentTime)
61
- enteredNearInfinity = true
47
+ fun testAdvanceTimeByEnormousDelays () = forTestDispatchers {
48
+ assertRunsFast {
49
+ with (createTestCoroutineScope(it)) {
50
+ launch {
51
+ val initialDelay = 10L
52
+ delay(initialDelay)
53
+ assertEquals(initialDelay, currentTime)
54
+ var enteredInfinity = false
55
+ launch {
56
+ delay(Long .MAX_VALUE - 1 ) // delay(Long.MAX_VALUE) does nothing
57
+ assertEquals(Long .MAX_VALUE , currentTime)
58
+ enteredInfinity = true
59
+ }
60
+ var enteredNearInfinity = false
61
+ launch {
62
+ delay(Long .MAX_VALUE - initialDelay - 1 )
63
+ assertEquals(Long .MAX_VALUE - 1 , currentTime)
64
+ enteredNearInfinity = true
65
+ }
66
+ testScheduler.advanceTimeBy(Long .MAX_VALUE )
67
+ assertFalse(enteredInfinity)
68
+ assertTrue(enteredNearInfinity)
69
+ assertEquals(Long .MAX_VALUE , currentTime)
70
+ testScheduler.runCurrent()
71
+ assertTrue(enteredInfinity)
72
+ }
73
+ testScheduler.advanceUntilIdle()
74
+ }
62
75
}
63
- testScheduler.advanceTimeBy(Long .MAX_VALUE )
64
- assertFalse(enteredInfinity)
65
- assertTrue(enteredNearInfinity)
66
- assertEquals(Long .MAX_VALUE , currentTime)
67
- testScheduler.runCurrent()
68
- assertTrue(enteredInfinity)
69
76
}
70
77
71
78
/* * Tests the basic functionality of [TestCoroutineScheduler.advanceTimeBy]. */
@@ -124,48 +131,52 @@ class TestCoroutineSchedulerTest {
124
131
125
132
/* * Tests that [TestCoroutineScheduler.runCurrent] will not run new tasks after the current time has advanced. */
126
133
@Test
127
- fun testRunCurrentNotDrainingQueue () = assertRunsFast {
128
- val scheduler = TestCoroutineScheduler ()
129
- val scope = createTestCoroutineScope(scheduler)
130
- var stage = 1
131
- scope.launch {
132
- delay(SLOW )
133
- launch {
134
+ fun testRunCurrentNotDrainingQueue () = forTestDispatchers {
135
+ assertRunsFast {
136
+ val scheduler = it.scheduler
137
+ val scope = createTestCoroutineScope(it)
138
+ var stage = 1
139
+ scope.launch {
134
140
delay(SLOW )
135
- stage = 3
141
+ launch {
142
+ delay(SLOW )
143
+ stage = 3
144
+ }
145
+ scheduler.advanceTimeBy(SLOW )
146
+ stage = 2
136
147
}
137
148
scheduler.advanceTimeBy(SLOW )
138
- stage = 2
149
+ assertEquals(1 , stage)
150
+ scheduler.runCurrent()
151
+ assertEquals(2 , stage)
152
+ scheduler.runCurrent()
153
+ assertEquals(3 , stage)
139
154
}
140
- scheduler.advanceTimeBy(SLOW )
141
- assertEquals(1 , stage)
142
- scheduler.runCurrent()
143
- assertEquals(2 , stage)
144
- scheduler.runCurrent()
145
- assertEquals(3 , stage)
146
155
}
147
156
148
157
/* * Tests that [TestCoroutineScheduler.advanceUntilIdle] doesn't hang when itself running in a scheduler task. */
149
158
@Test
150
- fun testNestedAdvanceUntilIdle () = assertRunsFast {
151
- val scheduler = TestCoroutineScheduler ()
152
- val scope = createTestCoroutineScope(scheduler)
153
- var executed = false
154
- scope.launch {
155
- launch {
156
- delay(SLOW )
157
- executed = true
159
+ fun testNestedAdvanceUntilIdle () = forTestDispatchers {
160
+ assertRunsFast {
161
+ val scheduler = it.scheduler
162
+ val scope = createTestCoroutineScope(it)
163
+ var executed = false
164
+ scope.launch {
165
+ launch {
166
+ delay(SLOW )
167
+ executed = true
168
+ }
169
+ scheduler.advanceUntilIdle()
158
170
}
159
171
scheduler.advanceUntilIdle()
172
+ assertTrue(executed)
160
173
}
161
- scheduler.advanceUntilIdle()
162
- assertTrue(executed)
163
174
}
164
175
165
176
/* * Tests [yield] scheduling tasks for future execution and not executing immediately. */
166
177
@Test
167
- fun testYield () {
168
- val scope = createTestCoroutineScope()
178
+ fun testYield () = forTestDispatchers {
179
+ val scope = createTestCoroutineScope(it )
169
180
var stage = 0
170
181
scope.launch {
171
182
yield ()
@@ -205,8 +216,8 @@ class TestCoroutineSchedulerTest {
205
216
206
217
/* * Tests that timeouts get triggered. */
207
218
@Test
208
- fun testSmallTimeouts () {
209
- val scope = createTestCoroutineScope(TestCoroutineDispatcher () )
219
+ fun testSmallTimeouts () = forTestDispatchers {
220
+ val scope = createTestCoroutineScope(it )
210
221
scope.checkTimeout(true ) {
211
222
val half = SLOW / 2
212
223
delay(half)
@@ -216,8 +227,8 @@ class TestCoroutineSchedulerTest {
216
227
217
228
/* * Tests that timeouts don't get triggered if the code finishes in time. */
218
229
@Test
219
- fun testLargeTimeouts () {
220
- val scope = createTestCoroutineScope()
230
+ fun testLargeTimeouts () = forTestDispatchers {
231
+ val scope = createTestCoroutineScope(it )
221
232
scope.checkTimeout(false ) {
222
233
val half = SLOW / 2
223
234
delay(half)
@@ -227,8 +238,8 @@ class TestCoroutineSchedulerTest {
227
238
228
239
/* * Tests that timeouts get triggered if the code fails to finish in time asynchronously. */
229
240
@Test
230
- fun testSmallAsynchronousTimeouts () {
231
- val scope = createTestCoroutineScope()
241
+ fun testSmallAsynchronousTimeouts () = forTestDispatchers {
242
+ val scope = createTestCoroutineScope(it )
232
243
val deferred = CompletableDeferred <Unit >()
233
244
scope.launch {
234
245
val half = SLOW / 2
@@ -243,8 +254,8 @@ class TestCoroutineSchedulerTest {
243
254
244
255
/* * Tests that timeouts don't get triggered if the code finishes in time, even if it does so asynchronously. */
245
256
@Test
246
- fun testLargeAsynchronousTimeouts () {
247
- val scope = createTestCoroutineScope()
257
+ fun testLargeAsynchronousTimeouts () = forTestDispatchers {
258
+ val scope = createTestCoroutineScope(it )
248
259
val deferred = CompletableDeferred <Unit >()
249
260
scope.launch {
250
261
val half = SLOW / 2
@@ -256,4 +267,13 @@ class TestCoroutineSchedulerTest {
256
267
deferred.await()
257
268
}
258
269
}
270
+
271
+ private fun forTestDispatchers (block : (TestDispatcher ) -> Unit ): Unit =
272
+ listOf (TestCoroutineDispatcher (), StandardTestDispatcher (), UnconfinedTestDispatcher ()).forEach {
273
+ try {
274
+ block(it)
275
+ } catch (e: Throwable ) {
276
+ throw RuntimeException (" Test failed for dispatcher $it " , e)
277
+ }
278
+ }
259
279
}
0 commit comments