aboutsummaryrefslogtreecommitdiffstats
path: root/kotlinx-coroutines-core/jvm/test/flow/CallbackFlowTest.kt
blob: e3db2626ce6c619d1b90f647e5d7e18b05e18046 (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
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
/*
 * Copyright 2016-2019 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 org.junit.Test
import kotlin.concurrent.*
import kotlin.test.*

class CallbackFlowTest : TestBase() {

    private class CallbackApi(val block: (SendChannel<Int>) -> Unit) {
        var started = false
        @Volatile
        var stopped = false
        lateinit var thread: Thread

        fun start(sink: SendChannel<Int>) {
            started = true
            thread = thread {
                while (!stopped) {
                    block(sink)
                }
            }
        }

        fun stop() {
            stopped = true
        }
    }

    @Test(timeout = 5_000L)
    fun testThrowingConsumer() = runTest {
        var i = 0
        val api = CallbackApi {
            runCatching {  it.offer(++i) }
        }

        val flow = callbackFlow<Int> {
            api.start(channel)
            awaitClose {
                api.stop()
            }
        }

        var receivedConsensus = 0
        var isDone = false
        var exception: Throwable? = null
        val job = flow
            .filter { it > 10 }
            .launchIn(this) {
                onEach {
                    if (it == 11) {
                        ++receivedConsensus
                    } else {
                        receivedConsensus = 42
                    }
                    throw RuntimeException()
                }
                catch<Throwable> { exception = it }
                finally { isDone = true }
            }
        job.join()
        assertEquals(1, receivedConsensus)
        assertTrue(isDone)
        assertTrue { exception is RuntimeException }
        api.thread.join()
        assertTrue(api.started)
        assertTrue(api.stopped)
    }

    @Test(timeout = 5_000L)
    fun testThrowingSource() = runBlocking {
        var i = 0
        val api = CallbackApi {
            if (i < 5) {
                it.offer(++i)
            } else {
                it.close(RuntimeException())
            }
        }

        val flow = callbackFlow<Int>() {
            api.start(channel)
            awaitClose {
                api.stop()
            }
        }

        var received = 0
        var isDone = false
        var exception: Throwable? = null
        val job = flow.launchIn(this) {
            onEach { ++received }
            catch<Throwable> { exception = it }
            finally { isDone = true }
        }

        job.join()
        assertTrue(isDone)
        assertTrue { exception is RuntimeException }
        api.thread.join()
        assertTrue(api.started)
        assertTrue(api.stopped)
    }


    @Test
    fun testMergeExample() = runTest {
        // Too slow on JS
        withContext(Dispatchers.Default) {
            val f1 = (1..10_000).asFlow()
            val f2 = (10_001..20_000).asFlow()
            assertEquals((1..20_000).toSet(), f1.merge(f2).toSet())
        }
    }

    private fun Flow<Int>.merge(other: Flow<Int>): Flow<Int> = channelFlow {
        launch {
            collect { send(it) }
        }
        other.collect { send(it) }
    }
}