-
Notifications
You must be signed in to change notification settings - Fork 1.9k
/
Copy pathWithTimeoutOrNullTest.kt
235 lines (214 loc) · 5.44 KB
/
WithTimeoutOrNullTest.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
/*
* Copyright 2016-2018 JetBrains s.r.o. Use of this source code is governed by the Apache 2.0 license.
*/
@file:Suppress("NAMED_ARGUMENTS_NOT_ALLOWED") // KT-21913
package kotlinx.coroutines
import kotlinx.coroutines.channels.*
import kotlin.test.*
class WithTimeoutOrNullTest : TestBase() {
/**
* Tests a case of no timeout and no suspension inside.
*/
@Test
fun testBasicNoSuspend() = runTest {
expect(1)
val result = withTimeoutOrNull(10_000) {
expect(2)
"OK"
}
assertEquals("OK", result)
finish(3)
}
/**
* Tests a case of no timeout and one suspension inside.
*/
@Test
fun testBasicSuspend() = runTest {
expect(1)
val result = withTimeoutOrNull(10_000) {
expect(2)
yield()
expect(3)
"OK"
}
assertEquals("OK", result)
finish(4)
}
/**
* Tests property dispatching of `withTimeoutOrNull` blocks
*/
@Test
fun testDispatch() = runTest {
expect(1)
launch {
expect(4)
yield() // back to main
expect(7)
}
expect(2)
// test that it does not yield to the above job when started
val result = withTimeoutOrNull(1000) {
expect(3)
yield() // yield only now
expect(5)
"OK"
}
assertEquals("OK", result)
expect(6)
yield() // back to launch
finish(8)
}
/**
* Tests that a 100% CPU-consuming loop will react on timeout if it has yields.
*/
@Test
fun testYieldBlockingWithTimeout() = runTest {
expect(1)
val result = withTimeoutOrNull(100) {
while (true) {
yield()
}
}
assertNull(result)
finish(2)
}
@Test
fun testSmallTimeout() = runTest {
val channel = Channel<Int>(1)
val value = withTimeoutOrNull(1) {
channel.receive()
}
assertNull(value)
}
@Test
fun testThrowException() = runTest(expected = {it is AssertionError}) {
withTimeoutOrNull(Long.MAX_VALUE) {
throw AssertionError()
}
}
@Test
fun testInnerTimeout() = runTest(
expected = { it is CancellationException }
) {
withTimeoutOrNull(1000) {
withTimeout(10) {
while (true) {
yield()
}
}
expectUnreached() // will timeout
}
expectUnreached() // will timeout
}
@Test
fun testNestedTimeout() = runTest(expected = { it is TimeoutCancellationException }) {
withTimeoutOrNull(Long.MAX_VALUE) {
// Exception from this withTimeout is not suppressed by withTimeoutOrNull
withTimeout(10) {
delay(Long.MAX_VALUE)
1
}
}
expectUnreached()
}
@Test
fun testOuterTimeout() = runTest {
var counter = 0
val result = withTimeoutOrNull(250) {
while (true) {
val inner = withTimeoutOrNull(100) {
while (true) {
yield()
}
}
assertNull(inner)
counter++
}
}
assertNull(result)
check(counter in 1..2) {"Executed: $counter times"}
}
@Test
fun testBadClass() = runTest {
val bad = BadClass()
val result = withTimeoutOrNull(100) {
bad
}
assertSame(bad, result)
}
@Test
fun testNullOnTimeout() = runTest {
expect(1)
val result = withTimeoutOrNull(100) {
expect(2)
delay(1000)
expectUnreached()
"OK"
}
assertNull(result)
finish(3)
}
@Test
fun testSuppressExceptionWithResult() = runTest {
expect(1)
val result = withTimeoutOrNull(100) {
expect(2)
try {
delay(1000)
} catch (e: CancellationException) {
expect(3)
}
"OK"
}
assertNull(result)
finish(4)
}
@Test
fun testSuppressExceptionWithAnotherException() = runTest {
expect(1)
try {
withTimeoutOrNull(100) {
expect(2)
try {
delay(1000)
} catch (e: CancellationException) {
expect(3)
throw TestException()
}
expectUnreached()
"OK"
}
expectUnreached()
} catch (e: TestException) {
// catches TestException
finish(4)
}
}
@Test
fun testNegativeTimeout() = runTest {
expect(1)
var result = withTimeoutOrNull(-1) {
expectUnreached()
}
assertNull(result)
result = withTimeoutOrNull(0) {
expectUnreached()
}
assertNull(result)
finish(2)
}
@Test
fun testExceptionFromWithinTimeout() = runTest {
expect(1)
try {
expect(2)
withTimeoutOrNull(1000) {
expect(3)
throw TestException()
}
expectUnreached()
} catch (e: TestException) {
finish(4)
}
}
}