/
FlowCallbackTest.kt
57 lines (51 loc) · 1.43 KB
/
FlowCallbackTest.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
/*
* Copyright 2016-2019 JetBrains s.r.o. Use of this source code is governed by the Apache 2.0 license.
*/
@file:Suppress("NAMED_ARGUMENTS_NOT_ALLOWED") // KT-21913
package kotlinx.coroutines.flow
import kotlinx.coroutines.*
import kotlinx.coroutines.channels.*
import kotlin.test.*
class FlowCallbackTest : TestBase() {
@Test
fun testClosedPrematurely() = runTest {
val outerScope = this
val flow = callbackFlow {
// ~ callback-based API
outerScope.launch(Job()) {
expect(2)
try {
send(1)
expectUnreached()
} catch (e: IllegalStateException) {
expect(3)
assertTrue(e.message!!.contains("awaitClose"))
}
}
expect(1)
}
try {
flow.collect()
} catch (e: IllegalStateException) {
expect(4)
assertTrue(e.message!!.contains("awaitClose"))
}
finish(5)
}
@Test
fun testNotClosedPrematurely() = runTest {
val outerScope = this
val flow = callbackFlow {
// ~ callback-based API
outerScope.launch(Job()) {
expect(2)
send(1)
close()
}
expect(1)
awaitClose()
}
assertEquals(listOf(1), flow.toList())
finish(3)
}
}