-
Notifications
You must be signed in to change notification settings - Fork 1.8k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Fixes #1491
- Loading branch information
Showing
4 changed files
with
126 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
68 changes: 68 additions & 0 deletions
68
kotlinx-coroutines-core/common/test/flow/operators/MergeTest.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,68 @@ | ||
/* | ||
* 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 kotlin.test.* | ||
import kotlinx.coroutines.flow.merge as originalMerge | ||
|
||
abstract class MergeTest : TestBase() { | ||
|
||
abstract fun <T> Iterable<Flow<T>>.merge(): Flow<T> | ||
|
||
@Test | ||
fun testMerge() = runTest { | ||
val n = 100 | ||
val sum = (1..n).map { flowOf(it) } | ||
.merge() | ||
.sum() | ||
|
||
assertEquals(n * (n + 1) / 2, sum) | ||
} | ||
|
||
@Test | ||
fun testSingle() = runTest { | ||
val flow = listOf(flowOf(), flowOf(42), flowOf()).merge() | ||
val value = flow.single() | ||
assertEquals(42, value) | ||
} | ||
|
||
@Test | ||
fun testNulls() = runTest { | ||
val list = listOf(flowOf(1), flowOf(null), flowOf(2)).merge().toList() | ||
assertEquals(listOf(1, null, 2), list) | ||
} | ||
|
||
@Test | ||
fun testContext() = runTest { | ||
val flow = flow { | ||
emit(NamedDispatchers.name()) | ||
}.flowOn(NamedDispatchers("source")) | ||
|
||
val result = listOf(flow).merge().flowOn(NamedDispatchers("irrelevant")).toList() | ||
assertEquals(listOf("source"), result) | ||
} | ||
|
||
@Test | ||
fun testIsolatedContext() = runTest { | ||
val flow = flow { | ||
emit(NamedDispatchers.name()) | ||
} | ||
|
||
val result = listOf(flow.flowOn(NamedDispatchers("1")), flow.flowOn(NamedDispatchers("2"))) | ||
.merge() | ||
.flowOn(NamedDispatchers("irrelevant")) | ||
.toList() | ||
assertEquals(listOf("1", "2"), result) | ||
} | ||
} | ||
|
||
class IterableMergeTest : MergeTest() { | ||
override fun <T> Iterable<Flow<T>>.merge(): Flow<T> = originalMerge() | ||
} | ||
|
||
class VarargMergeTest : MergeTest() { | ||
override fun <T> Iterable<Flow<T>>.merge(): Flow<T> = originalMerge(*toList().toTypedArray()) | ||
} |