-
Notifications
You must be signed in to change notification settings - Fork 1.8k
/
Scratch.kt
54 lines (47 loc) · 1.37 KB
/
Scratch.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
/*
* Copyright 2016-2020 JetBrains s.r.o. Use of this source code is governed by the Apache 2.0 license.
*/
package kotlinx.coroutines
import org.junit.*
import kotlin.coroutines.*
class Scratch : TestBase() {
@Test
fun test1() = runTest(expected = { it is CancellationException }) {
suspendCancellableCoroutine<Unit> { c ->
c.invokeOnCancellation {
println(it)
}
c.cancel()
}
}
@Test
fun test2() = runTest(expected = { it is CancellationException }) {
suspendCancellableCoroutine<Unit> { c ->
c.cancel()
c.invokeOnCancellation {
println(it)
}
}
}
@Test
fun test3() = runTest {
suspendCancellableCoroutine<Unit> { c ->
c.invokeOnCancellation {
println("Invoked") // Printed
}
assertFailsWith<IllegalStateException> {
c.invokeOnCancellation { expectUnreached() } // Triggers ISE
}
c.resume(Unit)
}
}
@Test
fun test4() = runTest(expected = { it is IllegalStateException }) {
suspendCancellableCoroutine<Unit> { c ->
c.invokeOnCancellation {
println("Invoked") // Not printed
}
c.resumeWith(Result.failure(IllegalStateException()))
}
}
}