-
Notifications
You must be signed in to change notification settings - Fork 1.8k
/
DelayDurationTest.kt
72 lines (59 loc) · 1.44 KB
/
DelayDurationTest.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
/*
* 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", "DEPRECATION")
// KT-21913
package kotlinx.coroutines
import kotlin.test.Test
import kotlin.time.*
@ExperimentalTime
class DelayDurationTest : TestBase() {
@Test
fun testCancellation() = runTest(expected = { it is CancellationException }) {
runAndCancel(1.seconds)
}
@Test
fun testInfinite() = runTest(expected = { it is CancellationException }) {
runAndCancel(Duration.INFINITE)
}
@Test
fun testRegularDelay() = runTest {
val deferred = async {
expect(2)
delay(1.seconds)
expect(4)
}
expect(1)
yield()
expect(3)
deferred.await()
finish(5)
}
@Test
fun testNanoDelay() = runTest {
val deferred = async {
expect(2)
delay(1.nanoseconds)
expect(4)
}
expect(1)
yield()
expect(3)
deferred.await()
finish(5)
}
private suspend fun runAndCancel(time: Duration) = coroutineScope {
expect(1)
val deferred = async {
expect(2)
delay(time)
expectUnreached()
}
yield()
expect(3)
require(deferred.isActive)
deferred.cancel()
finish(4)
deferred.await()
}
}