-
Notifications
You must be signed in to change notification settings - Fork 1.9k
/
Copy pathRunBlockingTestOnTestScopeTest.kt
165 lines (151 loc) · 4.57 KB
/
RunBlockingTestOnTestScopeTest.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
/*
* 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.flow.*
import kotlin.test.*
/** Copy of [RunTestTest], but for [runBlockingTestOnTestScope], where applicable. */
@Suppress("DEPRECATION")
class RunBlockingTestOnTestScopeTest {
@Test
fun testRunTestWithIllegalContext() {
for (ctx in TestScopeTest.invalidContexts) {
assertFailsWith<IllegalArgumentException> {
runBlockingTestOnTestScope(ctx) { }
}
}
}
@Test
fun testThrowingInRunTestBody() {
assertFailsWith<RuntimeException> {
runBlockingTestOnTestScope {
throw RuntimeException()
}
}
}
@Test
fun testThrowingInRunTestPendingTask() {
assertFailsWith<RuntimeException> {
runBlockingTestOnTestScope {
launch {
delay(SLOW)
throw RuntimeException()
}
}
}
}
@Test
fun reproducer2405() = runBlockingTestOnTestScope {
val dispatcher = StandardTestDispatcher(testScheduler)
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)
}
@Test
fun testChildrenCancellationOnTestBodyFailure() {
var job: Job? = null
assertFailsWith<AssertionError> {
runBlockingTestOnTestScope {
job = launch {
while (true) {
delay(1000)
}
}
throw AssertionError()
}
}
assertTrue(job!!.isCancelled)
}
@Test
fun testTimeout() {
assertFailsWith<TimeoutCancellationException> {
runBlockingTestOnTestScope {
withTimeout(50) {
launch {
delay(1000)
}
}
}
}
}
@Test
fun testRunTestThrowsRootCause() {
assertFailsWith<TestException> {
runBlockingTestOnTestScope {
launch {
throw TestException()
}
}
}
}
@Test
fun testCompletesOwnJob() {
var handlerCalled = false
runBlockingTestOnTestScope {
coroutineContext.job.invokeOnCompletion {
handlerCalled = true
}
}
assertTrue(handlerCalled)
}
@Test
fun testDoesNotCompleteGivenJob() {
var handlerCalled = false
val job = Job()
job.invokeOnCompletion {
handlerCalled = true
}
runBlockingTestOnTestScope(job) {
assertTrue(coroutineContext.job in job.children)
}
assertFalse(handlerCalled)
assertEquals(0, job.children.filter { it.isActive }.count())
}
@Test
fun testSuppressedExceptions() {
try {
runBlockingTestOnTestScope {
launch(SupervisorJob()) { throw TestException("x") }
launch(SupervisorJob()) { throw TestException("y") }
launch(SupervisorJob()) { throw TestException("z") }
throw TestException("w")
}
fail("should not be reached")
} catch (e: TestException) {
assertEquals("w", e.message)
val suppressed = e.suppressedExceptions +
(e.suppressedExceptions.firstOrNull()?.suppressedExceptions ?: emptyList())
assertEquals(3, suppressed.size)
assertEquals("x", suppressed[0].message)
assertEquals("y", suppressed[1].message)
assertEquals("z", suppressed[2].message)
}
}
@Test
fun testScopeRunTestExceptionHandler(): TestResult {
val scope = TestCoroutineScope()
return testResultMap({
try {
it()
fail("should not be reached")
} catch (e: TestException) {
// expected
}
}) {
scope.runTest {
launch(SupervisorJob()) { throw TestException("x") }
}
}
}
}