-
Notifications
You must be signed in to change notification settings - Fork 1.9k
/
Copy pathFlowCancellationTest.kt
46 lines (39 loc) · 1.04 KB
/
FlowCancellationTest.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
/*
* Copyright 2016-2020 JetBrains s.r.o. Use of this source code is governed by the Apache 2.0 license.
*/
package kotlinx.coroutines.flow
import kotlinx.coroutines.*
import kotlinx.coroutines.channels.*
import kotlin.test.*
class FlowCancellationTest : TestBase() {
@Test
fun testEmitIsCooperative() = runTest {
val latch = Channel<Unit>(1)
val job = flow {
expect(1)
latch.send(Unit)
while (true) {
emit(42)
}
}.launchIn(this + Dispatchers.Default)
latch.receive()
expect(2)
job.cancelAndJoin()
finish(3)
}
@Test
fun testIsActiveOnCurrentContext() = runTest {
val latch = Channel<Unit>(1)
val job = flow<Unit> {
expect(1)
latch.send(Unit)
while (currentContext().isActive) {
// Do nothing
}
}.launchIn(this + Dispatchers.Default)
latch.receive()
expect(2)
job.cancelAndJoin()
finish(3)
}
}