forked from Kotlin/kotlinx.coroutines
-
Notifications
You must be signed in to change notification settings - Fork 0
/
DelayDurationTest.kt
59 lines (48 loc) · 1.26 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
/*
* 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.Duration
import kotlin.time.ExperimentalTime
import kotlin.time.milliseconds
import kotlin.time.seconds
@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.milliseconds)
expect(3)
}
expect(1)
yield()
deferred.await()
finish(4)
}
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()
}
}