-
Notifications
You must be signed in to change notification settings - Fork 1.9k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Also, all flow-truncating operators are refactored via a common internal collectWhile operator that properly uses AbortFlowException and checks for its ownership, so that we don't have to look for bugs in interactions between all those operators (and zip, too, which is also flow-truncating). Fixes #2065
- Loading branch information
Showing
5 changed files
with
123 additions
and
49 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
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
49 changes: 49 additions & 0 deletions
49
kotlinx-coroutines-core/common/test/flow/operators/TransformWhileTest.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,49 @@ | ||
/* | ||
* 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 kotlin.test.* | ||
|
||
class TransformWhileTest : TestBase() { | ||
@Test | ||
fun testSimple() = runTest { | ||
val flow = (0..10).asFlow() | ||
val expected = listOf("A", "B", "C", "D") | ||
val actual = flow.transformWhile { value -> | ||
when(value) { | ||
0 -> { emit("A"); true } | ||
1 -> true | ||
2 -> { emit("B"); emit("C"); true } | ||
3 -> { emit("D"); false } | ||
else -> { expectUnreached(); false } | ||
} | ||
}.toList() | ||
assertEquals(expected, actual) | ||
} | ||
|
||
@Test | ||
fun testExample() = runTest { | ||
val source = listOf( | ||
DownloadProgress(0), | ||
DownloadProgress(50), | ||
DownloadProgress(100), | ||
DownloadProgress(147) | ||
) | ||
val expected = source.subList(0, 3) | ||
val actual = source.asFlow().completeWhenDone().toList() | ||
assertEquals(expected, actual) | ||
} | ||
|
||
private fun Flow<DownloadProgress>.completeWhenDone(): Flow<DownloadProgress> = | ||
transformWhile { progress -> | ||
emit(progress) // always emit progress | ||
!progress.isDone() // continue while download is not done | ||
} | ||
|
||
private data class DownloadProgress(val percent: Int) { | ||
fun isDone() = percent >= 100 | ||
} | ||
} |