-
Notifications
You must be signed in to change notification settings - Fork 1.9k
/
Copy pathTestBuildersTest.kt
131 lines (112 loc) · 3.31 KB
/
TestBuildersTest.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
/*
* 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 kotlin.coroutines.*
import kotlin.test.*
class TestBuildersTest {
@Test
fun scopeRunBlocking_passesDispatcher() {
val scope = TestCoroutineScope()
scope.runBlockingTest {
assertSame(scope.coroutineContext[ContinuationInterceptor], coroutineContext[ContinuationInterceptor])
}
}
@Test
fun dispatcherRunBlocking_passesDispatcher() {
val dispatcher = TestCoroutineDispatcher()
dispatcher.runBlockingTest {
assertSame(dispatcher, coroutineContext[ContinuationInterceptor])
}
}
@Test
fun scopeRunBlocking_advancesPreviousDelay() {
val scope = TestCoroutineScope()
val deferred = scope.async {
delay(SLOW)
3
}
scope.runBlockingTest {
assertRunsFast {
assertEquals(3, deferred.await())
}
}
}
@Test
fun dispatcherRunBlocking_advancesPreviousDelay() {
val dispatcher = TestCoroutineDispatcher()
val scope = CoroutineScope(dispatcher)
val deferred = scope.async {
delay(SLOW)
3
}
dispatcher.runBlockingTest {
assertRunsFast {
assertEquals(3, deferred.await())
}
}
}
@Test
fun scopeRunBlocking_disablesImmediatelyOnExit() {
val scope = TestCoroutineScope()
scope.runBlockingTest {
assertRunsFast {
delay(SLOW)
}
}
val deferred = scope.async {
delay(SLOW)
3
}
scope.runCurrent()
assertTrue(deferred.isActive)
scope.advanceUntilIdle()
assertEquals(3, deferred.getCompleted())
}
@Test
fun whenInAsync_runBlocking_nestsProperly() {
// this is not a supported use case, but it is possible so ensure it works
val dispatcher = TestCoroutineDispatcher()
val scope = TestCoroutineScope(dispatcher)
val deferred = scope.async {
delay(1_000)
var retval = 2
runBlockingTest {
delay(1_000)
retval++
}
retval
}
scope.advanceTimeBy(1_000)
scope.launch {
assertRunsFast {
assertEquals(3, deferred.getCompleted())
}
}
scope.runCurrent() // execute the launch without changing to immediate dispatch (testing internals)
scope.cleanupTestCoroutines()
}
@Test
fun whenInRunBlocking_runBlockingTest_nestsProperly() {
// this is not a supported use case, but it is possible so ensure it works
val scope = TestCoroutineScope()
var calls = 0
scope.runBlockingTest {
delay(1_000)
calls++
runBlockingTest {
val job = launch {
delay(1_000)
calls++
}
assertTrue(job.isActive)
advanceUntilIdle()
assertFalse(job.isActive)
calls++
}
++calls
}
assertEquals(4, calls)
}
}