-
Notifications
You must be signed in to change notification settings - Fork 1.9k
/
Copy pathDisabledHandlerTest.kt
64 lines (57 loc) · 1.7 KB
/
DisabledHandlerTest.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
/*
* Copyright 2016-2021 JetBrains s.r.o. Use of this source code is governed by the Apache 2.0 license.
*/
package kotlinx.coroutines.android
import android.os.*
import kotlinx.coroutines.*
import org.junit.*
import org.junit.runner.*
import org.robolectric.*
import org.robolectric.annotation.*
@RunWith(RobolectricTestRunner::class)
@Config(manifest = Config.NONE, sdk = [28])
class DisabledHandlerTest : TestBase() {
private var delegateToSuper = false
private val disabledDispatcher = object : Handler() {
override fun sendMessageAtTime(msg: Message?, uptimeMillis: Long): Boolean {
if (delegateToSuper) return super.sendMessageAtTime(msg, uptimeMillis)
return false
}
}.asCoroutineDispatcher()
@Test
fun testRunBlocking() {
expect(1)
try {
runBlocking(disabledDispatcher) {
expectUnreached()
}
expectUnreached()
} catch (e: CancellationException) {
finish(2)
}
}
@Test
fun testInvokeOnCancellation() = runTest {
val job = launch(disabledDispatcher, start = CoroutineStart.LAZY) { expectUnreached() }
job.invokeOnCompletion { if (it != null) expect(2) }
yield()
expect(1)
job.join()
finish(3)
}
@Test
fun testWithTimeout() = runTest {
delegateToSuper = true
try {
withContext(disabledDispatcher) {
expect(1)
delegateToSuper = false
delay(Long.MAX_VALUE - 1)
expectUnreached()
}
expectUnreached()
} catch (e: CancellationException) {
finish(2)
}
}
}