-
Notifications
You must be signed in to change notification settings - Fork 1.8k
/
DurationOverflowTest.kt
79 lines (66 loc) · 1.98 KB
/
DurationOverflowTest.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
/*
* Copyright 2016-2018 JetBrains s.r.o. Use of this source code is governed by the Apache 2.0 license.
*/
package kotlinx.coroutines.time
import kotlinx.coroutines.*
import kotlinx.coroutines.selects.*
import org.junit.Test
import java.time.*
import java.time.temporal.*
import kotlin.test.*
class DurationOverflowTest : TestBase() {
private val durations = ChronoUnit.values().map { it.duration }
@Test
fun testDelay() = runTest {
var counter = 0
for (duration in durations) {
expect(++counter)
delay(duration.negated()) // Instant bail out from negative values
launch(start = CoroutineStart.UNDISPATCHED) {
expect(++counter)
delay(duration)
}.cancelAndJoin()
expect(++counter)
}
finish(++counter)
}
@Test
fun testOnTimeout() = runTest {
for (duration in durations) {
// Does not crash on overflows
select<Unit> {
onTimeout(duration) {}
onTimeout(duration.negated()) {}
}
}
}
@Test
fun testWithTimeout() = runTest {
for (duration in durations) {
withTimeout(duration) {}
}
}
@Test
fun testWithTimeoutOrNull() = runTest {
for (duration in durations) {
withTimeoutOrNull(duration) {}
}
}
@Test
fun testWithTimeoutOrNullNegativeDuration() = runTest {
val result = withTimeoutOrNull(Duration.ofSeconds(1).negated()) {
1
}
assertNull(result)
}
@Test
fun testZeroDurationWithTimeout() = runTest {
assertFailsWith<TimeoutCancellationException> { withTimeout(0L) {} }
assertFailsWith<TimeoutCancellationException> { withTimeout(Duration.ZERO) {} }
}
@Test
fun testZeroDurationWithTimeoutOrNull() = runTest {
assertNull(withTimeoutOrNull(0L) {})
assertNull(withTimeoutOrNull(Duration.ZERO) {})
}
}