-
Notifications
You must be signed in to change notification settings - Fork 1.8k
/
TestCoroutineSchedulerTest.kt
259 lines (243 loc) · 7.73 KB
/
TestCoroutineSchedulerTest.kt
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
/*
* Copyright 2016-2021 JetBrains s.r.o. Use of this source code is governed by the Apache 2.0 license.
*/
package kotlinx.coroutines.test
import kotlinx.coroutines.*
import kotlin.test.*
class TestCoroutineSchedulerTest {
/** Tests that `TestCoroutineScheduler` attempts to detect if there are several instances of it. */
@Test
fun testContextElement() = runTest {
assertFailsWith<IllegalStateException> {
withContext(TestCoroutineDispatcher()) {
}
}
}
/** Tests that, as opposed to [DelayController.advanceTimeBy] or [TestCoroutineScope.advanceTimeBy],
* [TestCoroutineScheduler.advanceTimeBy] doesn't run the tasks scheduled at the target moment. */
@Test
fun testAdvanceTimeByDoesNotRunCurrent() = runTest {
var entered = false
launch {
delay(15)
entered = true
}
testScheduler.advanceTimeBy(15)
assertFalse(entered)
testScheduler.runCurrent()
assertTrue(entered)
}
/** Tests that [TestCoroutineScheduler.advanceTimeBy] doesn't accept negative delays. */
@Test
fun testAdvanceTimeByWithNegativeDelay() {
val scheduler = TestCoroutineScheduler()
assertFailsWith<IllegalArgumentException> {
scheduler.advanceTimeBy(-1)
}
}
/** Tests that if [TestCoroutineScheduler.advanceTimeBy] encounters an arithmetic overflow, all the tasks scheduled
* until the moment [Long.MAX_VALUE] get run. */
@Test
fun testAdvanceTimeByEnormousDelays() = runTest {
val initialDelay = 10L
delay(initialDelay)
assertEquals(initialDelay, currentTime)
var enteredInfinity = false
launch {
delay(Long.MAX_VALUE - 1) // delay(Long.MAX_VALUE) does nothing
assertEquals(Long.MAX_VALUE, currentTime)
enteredInfinity = true
}
var enteredNearInfinity = false
launch {
delay(Long.MAX_VALUE - initialDelay - 1)
assertEquals(Long.MAX_VALUE - 1, currentTime)
enteredNearInfinity = true
}
testScheduler.advanceTimeBy(Long.MAX_VALUE)
assertFalse(enteredInfinity)
assertTrue(enteredNearInfinity)
assertEquals(Long.MAX_VALUE, currentTime)
testScheduler.runCurrent()
assertTrue(enteredInfinity)
}
/** Tests the basic functionality of [TestCoroutineScheduler.advanceTimeBy]. */
@Test
fun testAdvanceTimeBy() = assertRunsFast {
val scheduler = TestCoroutineScheduler()
val scope = TestCoroutineScope(scheduler)
var stage = 1
scope.launch {
delay(1_000)
assertEquals(1_000, scheduler.currentTime)
stage = 2
delay(500)
assertEquals(1_500, scheduler.currentTime)
stage = 3
delay(501)
assertEquals(2_001, scheduler.currentTime)
stage = 4
}
assertEquals(1, stage)
assertEquals(0, scheduler.currentTime)
scheduler.advanceTimeBy(2_000)
assertEquals(3, stage)
assertEquals(2_000, scheduler.currentTime)
scheduler.advanceTimeBy(2)
assertEquals(4, stage)
assertEquals(2_002, scheduler.currentTime)
scope.cleanupTestCoroutines()
}
/** Tests the basic functionality of [TestCoroutineScheduler.runCurrent]. */
@Test
fun testRunCurrent() = runTest {
var stage = 0
launch {
delay(1)
++stage
delay(1)
stage += 10
}
launch {
delay(1)
++stage
delay(1)
stage += 10
}
testScheduler.advanceTimeBy(1)
assertEquals(0, stage)
runCurrent()
assertEquals(2, stage)
testScheduler.advanceTimeBy(1)
assertEquals(2, stage)
runCurrent()
assertEquals(22, stage)
}
/** Tests that [TestCoroutineScheduler.runCurrent] will not run new tasks after the current time has advanced. */
@Test
fun testRunCurrentNotDrainingQueue() = assertRunsFast {
val scheduler = TestCoroutineScheduler()
val scope = TestCoroutineScope(scheduler)
var stage = 1
scope.launch {
delay(SLOW)
launch {
delay(SLOW)
stage = 3
}
scheduler.advanceTimeBy(SLOW)
stage = 2
}
scheduler.advanceTimeBy(SLOW)
assertEquals(1, stage)
scheduler.runCurrent()
assertEquals(2, stage)
scheduler.runCurrent()
assertEquals(3, stage)
}
/** Tests that [TestCoroutineScheduler.advanceUntilIdle] doesn't hang when itself running in a scheduler task. */
@Test
fun testNestedAdvanceUntilIdle() = assertRunsFast {
val scheduler = TestCoroutineScheduler()
val scope = TestCoroutineScope(scheduler)
var executed = false
scope.launch {
launch {
delay(SLOW)
executed = true
}
scheduler.advanceUntilIdle()
}
scheduler.advanceUntilIdle()
assertTrue(executed)
}
/** Tests [yield] scheduling tasks for future execution and not executing immediately. */
@Test
fun testYield() {
val scope = TestCoroutineScope()
var stage = 0
scope.launch {
yield()
assertEquals(1, stage)
stage = 2
}
scope.launch {
yield()
assertEquals(2, stage)
stage = 3
}
assertEquals(0, stage)
stage = 1
scope.runCurrent()
}
private fun TestCoroutineScope.checkTimeout(
timesOut: Boolean, timeoutMillis: Long = SLOW, block: suspend () -> Unit
) = assertRunsFast {
var caughtException = false
launch {
try {
withTimeout(timeoutMillis) {
block()
}
} catch (e: TimeoutCancellationException) {
caughtException = true
}
}
advanceUntilIdle()
cleanupTestCoroutines()
if (timesOut)
assertTrue(caughtException)
else
assertFalse(caughtException)
}
/** Tests that timeouts get triggered. */
@Test
fun testSmallTimeouts() {
val scope = TestCoroutineScope()
scope.checkTimeout(true) {
val half = SLOW / 2
delay(half)
delay(SLOW - half)
}
}
/** Tests that timeouts don't get triggered if the code finishes in time. */
@Test
fun testLargeTimeouts() {
val scope = TestCoroutineScope()
scope.checkTimeout(false) {
val half = SLOW / 2
delay(half)
delay(SLOW - half - 1)
}
}
/** Tests that timeouts get triggered if the code fails to finish in time asynchronously. */
@Test
fun testSmallAsynchronousTimeouts() {
val scope = TestCoroutineScope()
val deferred = CompletableDeferred<Unit>()
scope.launch {
val half = SLOW / 2
delay(half)
delay(SLOW - half)
deferred.complete(Unit)
}
scope.checkTimeout(true) {
deferred.await()
}
}
/** Tests that timeouts don't get triggered if the code finishes in time, even if it does so asynchronously. */
@Test
fun testLargeAsynchronousTimeouts() {
val scope = TestCoroutineScope()
val deferred = CompletableDeferred<Unit>()
scope.launch {
val half = SLOW / 2
delay(half)
delay(SLOW - half - 1)
deferred.complete(Unit)
}
scope.checkTimeout(false) {
deferred.await()
}
}
}