blob: 6a50bc92dc3a639efc88f55cc3692d29af5d80c6 [file] [log] [blame]
Vsevolod Tolstopyatovd57bfa22019-04-04 14:25:13 +03001/*
2 * Copyright 2016-2019 JetBrains s.r.o. Use of this source code is governed by the Apache 2.0 license.
3 */
4
5package kotlinx.coroutines.flow
6
7import kotlinx.coroutines.*
8import kotlin.test.*
9
10class ConcatenateTest : TestBase() {
11 @Test
12 fun testConcatenate() = runTest {
13 val n = 100
14 val sum = (1..n).asFlow()
15 .map { value ->
16 flow {
17 repeat(value) {
18 emit(it + 1)
19 }
20 }
21 }.concatenate().sum()
22 assertEquals(n * (n + 1) * (n + 2) / 6, sum)
23 }
24
25 @Test
26 fun testSingle() = runTest {
27 val flows = flow {
28 repeat(100) {
29 if (it == 99) emit(flowOf(42))
30 else emit(flowOf())
31 }
32 }
33
34 val value = flows.concatenate().single()
35 assertEquals(42, value)
36 }
37
38
39 @Test
40 fun testContext() = runTest {
41 val flow = flow {
42 emit(flow {
43 expect(2)
44 assertEquals("first", NamedDispatchers.name())
45 emit(1)
46 }.flowOn(NamedDispatchers("first")))
47
48 emit(flow {
49 expect(3)
50 assertEquals("second", NamedDispatchers.name())
51 emit(1)
52 }.flowOn(NamedDispatchers("second")))
53 }.concatenate().flowOn(NamedDispatchers("first"))
54
55 expect(1)
56 assertEquals(2, flow.sum())
57 finish(4)
58 }
59}