blob: 91d01744a9ac56eb8eaee5a86c20ba83fc9e7051 (
plain)
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
|
package ca.allanwang.kau.kotlin
import kotlinx.coroutines.CancellationException
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.Job
import kotlinx.coroutines.runBlocking
import kotlinx.coroutines.withContext
import org.junit.Test
import kotlin.test.assertEquals
import kotlin.test.fail
/**
* Tests geared towards coroutines
*/
class CoroutineTest {
/**
* If a job is cancelled, then a switch to a new context will not run
*/
@Test
fun implicitCancellationBefore() {
val job = Job()
var id = 0
try {
runBlocking(job) {
id++
job.cancel()
withContext(Dispatchers.IO) {
fail("Context switch should not be reached")
}
}
} catch (ignore: CancellationException) {
} finally {
assertEquals(1, id, "Launcher never executed")
}
}
/**
* If a job is cancelled, then a switch from a new context will not run
*/
@Test
fun implicitCancellationAfter() {
val job = Job()
var id = 0
try {
runBlocking(job) {
withContext(Dispatchers.IO) {
id++
job.cancel()
}
fail("Post context switch should not be reached")
}
} catch (ignore: CancellationException) {
} finally {
assertEquals(1, id, "Context switch never executed")
}
}
}
|