-
Notifications
You must be signed in to change notification settings - Fork 1.9k
/
Copy pathUnconfinedTestDispatcherTest.kt
167 lines (144 loc) · 4.91 KB
/
UnconfinedTestDispatcherTest.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
/*
* 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 kotlinx.coroutines.channels.*
import kotlinx.coroutines.flow.*
import kotlin.test.*
class UnconfinedTestDispatcherTest {
@Test
fun reproducer1742() {
class ObservableValue<T>(initial: T) {
var value: T = initial
private set
private val listeners = mutableListOf<(T) -> Unit>()
fun set(value: T) {
this.value = value
listeners.forEach { it(value) }
}
fun addListener(listener: (T) -> Unit) {
listeners.add(listener)
}
fun removeListener(listener: (T) -> Unit) {
listeners.remove(listener)
}
}
fun <T> ObservableValue<T>.observe(): Flow<T> =
callbackFlow {
val listener = { value: T ->
if (!isClosedForSend) {
trySend(value)
}
}
addListener(listener)
listener(value)
awaitClose { removeListener(listener) }
}
val intProvider = ObservableValue(0)
val stringProvider = ObservableValue("")
var data = Pair(0, "")
val scope = CoroutineScope(UnconfinedTestDispatcher())
scope.launch {
combine(
intProvider.observe(),
stringProvider.observe()
) { intValue, stringValue -> Pair(intValue, stringValue) }
.collect { pair ->
data = pair
}
}
intProvider.set(1)
stringProvider.set("3")
intProvider.set(2)
intProvider.set(3)
scope.cancel()
assertEquals(Pair(3, "3"), data)
}
@Test
fun reproducer2082() = runTest {
val subject1 = MutableStateFlow(1)
val subject2 = MutableStateFlow("a")
val values = mutableListOf<Pair<Int, String>>()
val job = launch(UnconfinedTestDispatcher(testScheduler)) {
combine(subject1, subject2) { intVal, strVal -> intVal to strVal }
.collect {
delay(10000)
values += it
}
}
subject1.value = 2
delay(10000)
subject2.value = "b"
delay(10000)
subject1.value = 3
delay(10000)
subject2.value = "c"
delay(10000)
delay(10000)
delay(1)
job.cancel()
assertEquals(listOf(Pair(1, "a"), Pair(2, "a"), Pair(2, "b"), Pair(3, "b"), Pair(3, "c")), values)
}
@Test
fun reproducer2405() = createTestResult {
val dispatcher = UnconfinedTestDispatcher()
var collectedError = false
withContext(dispatcher) {
flow { emit(1) }
.combine(
flow<String> { throw IllegalArgumentException() }
) { int, string -> int.toString() + string }
.catch { emit("error") }
.collect {
assertEquals("error", it)
collectedError = true
}
}
assertTrue(collectedError)
}
/** An example from the [UnconfinedTestDispatcher] documentation. */
@Test
fun testUnconfinedDispatcher() = runTest {
val values = mutableListOf<Int>()
val stateFlow = MutableStateFlow(0)
val job = launch(UnconfinedTestDispatcher(testScheduler)) {
stateFlow.collect {
values.add(it)
}
}
stateFlow.value = 1
stateFlow.value = 2
stateFlow.value = 3
job.cancel()
assertEquals(listOf(0, 1, 2, 3), values)
}
/** Tests that child coroutines are eagerly entered. */
@Test
fun testEagerlyEnteringChildCoroutines() = runTest(UnconfinedTestDispatcher()) {
var entered = false
val deferred = CompletableDeferred<Unit>()
var completed = false
launch {
entered = true
deferred.await()
completed = true
}
assertTrue(entered) // `entered = true` already executed.
assertFalse(completed) // however, the child coroutine then suspended, so it is enqueued.
deferred.complete(Unit) // resume the coroutine.
assertTrue(completed) // now the child coroutine is immediately completed.
}
/** Tests that the [TestCoroutineScheduler] used for [Dispatchers.Main] gets used by default. */
@Test
fun testSchedulerReuse() {
val dispatcher1 = StandardTestDispatcher()
Dispatchers.setMain(dispatcher1)
try {
val dispatcher2 = UnconfinedTestDispatcher()
assertSame(dispatcher1.scheduler, dispatcher2.scheduler)
} finally {
Dispatchers.resetMain()
}
}
}