-
Notifications
You must be signed in to change notification settings - Fork 1.9k
/
Copy pathTestScopeTest.kt
500 lines (474 loc) · 15.4 KB
/
TestScopeTest.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
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
/*
* Copyright 2016-2019 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 kotlinx.coroutines.channels.*
import kotlinx.coroutines.flow.*
import kotlin.coroutines.*
import kotlin.test.*
class TestScopeTest {
/** Tests failing to create a [TestScope] with incorrect contexts. */
@Test
fun testCreateThrowsOnInvalidArguments() {
for (ctx in invalidContexts) {
assertFailsWith<IllegalArgumentException> {
TestScope(ctx)
}
}
}
/** Tests that a newly-created [TestScope] provides the correct scheduler. */
@Test
fun testCreateProvidesScheduler() {
// Creates a new scheduler.
run {
val scope = TestScope()
assertNotNull(scope.coroutineContext[TestCoroutineScheduler])
}
// Reuses the scheduler that the dispatcher is linked to.
run {
val dispatcher = StandardTestDispatcher()
val scope = TestScope(dispatcher)
assertSame(dispatcher.scheduler, scope.coroutineContext[TestCoroutineScheduler])
}
// Uses the scheduler passed to it.
run {
val scheduler = TestCoroutineScheduler()
val scope = TestScope(scheduler)
assertSame(scheduler, scope.coroutineContext[TestCoroutineScheduler])
assertSame(scheduler, (scope.coroutineContext[ContinuationInterceptor] as TestDispatcher).scheduler)
}
// Doesn't touch the passed dispatcher and the scheduler if they match.
run {
val scheduler = TestCoroutineScheduler()
val dispatcher = StandardTestDispatcher(scheduler)
val scope = TestScope(scheduler + dispatcher)
assertSame(scheduler, scope.coroutineContext[TestCoroutineScheduler])
assertSame(dispatcher, scope.coroutineContext[ContinuationInterceptor])
}
}
/** Part of [testCreateProvidesScheduler], disabled for Native */
@Test
fun testCreateReusesScheduler() {
// Reuses the scheduler of `Dispatchers.Main`
run {
val scheduler = TestCoroutineScheduler()
val mainDispatcher = StandardTestDispatcher(scheduler)
Dispatchers.setMain(mainDispatcher)
try {
val scope = TestScope()
assertSame(scheduler, scope.coroutineContext[TestCoroutineScheduler])
assertNotSame(mainDispatcher, scope.coroutineContext[ContinuationInterceptor])
} finally {
Dispatchers.resetMain()
}
}
// Does not reuse the scheduler of `Dispatchers.Main` if one is explicitly passed
run {
val mainDispatcher = StandardTestDispatcher()
Dispatchers.setMain(mainDispatcher)
try {
val scheduler = TestCoroutineScheduler()
val scope = TestScope(scheduler)
assertSame(scheduler, scope.coroutineContext[TestCoroutineScheduler])
assertNotSame(mainDispatcher.scheduler, scope.coroutineContext[TestCoroutineScheduler])
assertNotSame(mainDispatcher, scope.coroutineContext[ContinuationInterceptor])
} finally {
Dispatchers.resetMain()
}
}
}
/** Tests that the cleanup procedure throws if there were uncompleted delays by the end. */
@Test
fun testPresentDelaysThrowing() {
val scope = TestScope()
var result = false
scope.launch {
delay(5)
result = true
}
assertFalse(result)
scope.asSpecificImplementation().enter()
assertFailsWith<UncompletedCoroutinesError> { scope.asSpecificImplementation().leave() }
assertFalse(result)
}
/** Tests that the cleanup procedure throws if there were active jobs by the end. */
@Test
fun testActiveJobsThrowing() {
val scope = TestScope()
var result = false
val deferred = CompletableDeferred<String>()
scope.launch {
deferred.await()
result = true
}
assertFalse(result)
scope.asSpecificImplementation().enter()
assertFailsWith<UncompletedCoroutinesError> { scope.asSpecificImplementation().leave() }
assertFalse(result)
}
/** Tests that the cleanup procedure throws even if it detects that the job is already cancelled. */
@Test
fun testCancelledDelaysThrowing() {
val scope = TestScope()
var result = false
val deferred = CompletableDeferred<String>()
val job = scope.launch {
deferred.await()
result = true
}
job.cancel()
assertFalse(result)
scope.asSpecificImplementation().enter()
assertFailsWith<UncompletedCoroutinesError> { scope.asSpecificImplementation().leave() }
assertFalse(result)
}
/** Tests that uncaught exceptions are thrown at the cleanup. */
@Test
fun testGetsCancelledOnChildFailure(): TestResult {
val scope = TestScope()
val exception = TestException("test")
scope.launch {
throw exception
}
return testResultMap({
try {
it()
fail("should not reach")
} catch (e: TestException) {
// expected
}
}) {
scope.runTest {
}
}
}
/** Tests that, when reporting several exceptions, the first one is thrown, with the rest suppressed. */
@Test
fun testSuppressedExceptions() {
TestScope().apply {
asSpecificImplementation().enter()
launch(SupervisorJob()) { throw TestException("x") }
launch(SupervisorJob()) { throw TestException("y") }
launch(SupervisorJob()) { throw TestException("z") }
runCurrent()
val e = asSpecificImplementation().leave()
assertEquals(3, e.size)
assertEquals("x", e[0].message)
assertEquals("y", e[1].message)
assertEquals("z", e[2].message)
}
}
/** Tests that the background work is being run at all. */
@Test
fun testBackgroundWorkBeingRun(): TestResult = runTest {
var i = 0
var j = 0
backgroundScope.launch {
++i
}
backgroundScope.launch {
delay(10)
++j
}
assertEquals(0, i)
assertEquals(0, j)
delay(1)
assertEquals(1, i)
assertEquals(0, j)
delay(10)
assertEquals(1, i)
assertEquals(1, j)
}
/**
* Tests that the background work gets cancelled after the test body finishes.
*/
@Test
fun testBackgroundWorkCancelled(): TestResult {
var cancelled = false
return testResultMap({
it()
assertTrue(cancelled)
}) {
runTest {
var i = 0
backgroundScope.launch {
try {
while (isActive) {
++i
yield()
}
} catch (e: CancellationException) {
cancelled = true
}
}
repeat(5) {
assertEquals(i, it)
yield()
}
}
}
}
/** Tests the interactions between the time-control commands and the background work. */
@Test
fun testBackgroundWorkTimeControl(): TestResult = runTest {
var i = 0
var j = 0
backgroundScope.launch {
while (true) {
++i
delay(100)
}
}
backgroundScope.launch {
while (true) {
++j
delay(50)
}
}
advanceUntilIdle() // should do nothing, as only background work is left.
assertEquals(0, i)
assertEquals(0, j)
val job = launch {
delay(1)
// the background work scheduled for earlier gets executed before the normal work scheduled for later does
assertEquals(1, i)
assertEquals(1, j)
}
job.join()
advanceTimeBy(199) // should work the same for the background tasks
assertEquals(2, i)
assertEquals(4, j)
advanceUntilIdle() // once again, should do nothing
assertEquals(2, i)
assertEquals(4, j)
runCurrent() // should behave the same way as for the normal work
assertEquals(3, i)
assertEquals(5, j)
launch {
delay(1001)
assertEquals(13, i)
assertEquals(25, j)
}
advanceUntilIdle() // should execute the normal work, and with that, the background one, too
}
/**
* Tests that an error in a background coroutine does not cancel the test, but is reported at the end.
*/
@Test
fun testBackgroundWorkErrorReporting(): TestResult {
var testFinished = false
val exception = RuntimeException("x")
return testResultMap({
try {
it()
fail("unreached")
} catch (e: Throwable) {
assertSame(e, exception)
assertTrue(testFinished)
}
}) {
runTest {
backgroundScope.launch {
throw exception
}
delay(1000)
testFinished = true
}
}
}
/**
* Tests that the background work gets to finish what it's doing after the test is completed.
*/
@Test
fun testBackgroundWorkFinalizing(): TestResult {
var taskEnded = 0
val nTasks = 10
return testResultMap({
try {
it()
fail("unreached")
} catch (e: TestException) {
assertEquals(2, e.suppressedExceptions.size)
assertEquals(nTasks, taskEnded)
}
}) {
runTest {
repeat(nTasks) {
backgroundScope.launch {
try {
while (true) {
delay(1)
}
} finally {
++taskEnded
if (taskEnded <= 2)
throw TestException()
}
}
}
delay(100)
throw TestException()
}
}
}
/**
* Tests using [Flow.stateIn] as a background job.
*/
@Test
fun testExampleBackgroundJob1() = runTest {
val myFlow = flow {
var i = 0
while (true) {
emit(++i)
delay(1)
}
}
val stateFlow = myFlow.stateIn(backgroundScope, SharingStarted.Eagerly, 0)
var j = 0
repeat(100) {
assertEquals(j++, stateFlow.value)
delay(1)
}
}
/**
* A test from the documentation of [TestScope.backgroundScope].
*/
@Test
fun testExampleBackgroundJob2() = runTest {
val channel = Channel<Int>()
backgroundScope.launch {
var i = 0
while (true) {
channel.send(i++)
}
}
repeat(100) {
assertEquals(it, channel.receive())
}
}
/**
* Tests that the test will timeout due to idleness even if some background tasks are running.
*/
@Test
fun testBackgroundWorkNotPreventingTimeout(): TestResult = testResultMap({
try {
it()
fail("unreached")
} catch (_: UncompletedCoroutinesError) {
}
}) {
runTest(dispatchTimeoutMs = 100) {
backgroundScope.launch {
while (true) {
yield()
}
}
backgroundScope.launch {
while (true) {
delay(1)
}
}
val deferred = CompletableDeferred<Unit>()
deferred.await()
}
}
/**
* Tests that the background work will not prevent the test from timing out even in some cases
* when the unconfined dispatcher is used.
*/
@Test
fun testUnconfinedBackgroundWorkNotPreventingTimeout(): TestResult = testResultMap({
try {
it()
fail("unreached")
} catch (_: UncompletedCoroutinesError) {
}
}) {
runTest(UnconfinedTestDispatcher(), dispatchTimeoutMs = 100) {
/**
* Having a coroutine like this will still cause the test to hang:
backgroundScope.launch {
while (true) {
yield()
}
}
* The reason is that even the initial [advanceUntilIdle] will never return in this case.
*/
backgroundScope.launch {
while (true) {
delay(1)
}
}
val deferred = CompletableDeferred<Unit>()
deferred.await()
}
}
/**
* Tests that even the exceptions in the background scope that don't typically get reported and need to be queried
* (like failures in [async]) will still surface in some simple scenarios.
*/
@Test
fun testAsyncFailureInBackgroundReported() = testResultMap({
try {
it()
fail("unreached")
} catch (e: TestException) {
assertEquals("z", e.message)
assertEquals(setOf("x", "y"), e.suppressedExceptions.map { it.message }.toSet())
}
}) {
runTest {
backgroundScope.async {
throw TestException("x")
}
backgroundScope.produce<Unit> {
throw TestException("y")
}
delay(1)
throw TestException("z")
}
}
/**
* Tests that, if an exception reaches the [TestScope] exception reporting mechanism via several
* channels, it will only be reported once.
*/
@Test
fun testNoDuplicateExceptions() = testResultMap({
try {
it()
fail("unreached")
} catch (e: TestException) {
assertEquals("y", e.message)
assertEquals(listOf("x"), e.suppressedExceptions.map { it.message })
}
}) {
runTest {
backgroundScope.launch {
throw TestException("x")
}
delay(1)
throw TestException("y")
}
}
/**
* Tests that [TestScope.withTimeout] notifies the programmer about using the virtual time.
*/
@Test
fun testTimingOutWithVirtualTimeMessage() = runTest {
try {
withTimeout(1_000_000) {
Channel<Unit>().receive()
}
} catch (e: TimeoutCancellationException) {
assertContains(e.message!!, "virtual")
}
}
companion object {
internal val invalidContexts = listOf(
Dispatchers.Default, // not a [TestDispatcher]
CoroutineExceptionHandler { _, _ -> }, // exception handlers can't be overridden
StandardTestDispatcher() + TestCoroutineScheduler(), // the dispatcher is not linked to the scheduler
)
}
}