-
Notifications
You must be signed in to change notification settings - Fork 1.9k
/
Copy pathRunInterruptibleTest.kt
63 lines (56 loc) · 1.48 KB
/
RunInterruptibleTest.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
/*
* Copyright 2016-2018 JetBrains s.r.o. Use of this source code is governed by the Apache 2.0 license.
*/
package kotlinx.coroutines
import kotlinx.coroutines.channels.*
import java.io.*
import java.util.concurrent.*
import java.util.concurrent.atomic.*
import kotlin.test.*
class RunInterruptibleTest : TestBase() {
@Test
fun testNormalRun() = runTest {
val result = runInterruptible {
val x = 1
val y = 2
Thread.sleep(1)
x + y
}
assertEquals(3, result)
}
@Test
fun testExceptionalRun() = runTest {
try {
runInterruptible {
expect(1)
throw TestException()
}
} catch (e: TestException) {
finish(2)
}
}
@Test
fun testInterrupt() = runTest {
val latch = Channel<Unit>(1)
val job = launch {
runInterruptible(Dispatchers.IO) {
expect(2)
latch.offer(Unit)
try {
Thread.sleep(10_000L)
expectUnreached()
} catch (e: InterruptedException) {
expect(4)
assertFalse { Thread.currentThread().isInterrupted }
}
}
}
launch(start = CoroutineStart.UNDISPATCHED) {
expect(1)
latch.receive()
expect(3)
job.cancelAndJoin()
}.join()
finish(5)
}
}