-
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.
Specify exception types, add tests on it
- Loading branch information
Showing
7 changed files
with
65 additions
and
4 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
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -14,6 +14,7 @@ import kotlin.jvm.* | |
|
||
/** | ||
* Returns a flow that ignores first [count] elements. | ||
* Throws [IllegalArgumentException] if [count] is negative. | ||
*/ | ||
@FlowPreview | ||
public fun <T> Flow<T>.drop(count: Int): Flow<T> { | ||
|
@@ -45,10 +46,12 @@ public fun <T> Flow<T>.dropWhile(predicate: suspend (T) -> Boolean): Flow<T> = f | |
/** | ||
* Returns a flow that contains first [count] elements. | ||
* When [count] elements are consumed, the original flow is cancelled. | ||
* Throws [IllegalArgumentException] if [count] is negative. | ||
*/ | ||
@FlowPreview | ||
public fun <T> Flow<T>.take(count: Int): Flow<T> { | ||
require(count > 0) { "Take count should be positive, but had $count" } | ||
require(count >= 0) { "Requested element count $count is less than zero." } | ||
if (count == 0) return emptyFlow() | ||
This comment was marked as resolved.
Sorry, something went wrong.
This comment was marked as resolved.
Sorry, something went wrong.
elizarov
Contributor
|
||
return flow { | ||
var consumed = 0 | ||
try { | ||
|
31 changes: 31 additions & 0 deletions
31
kotlinx-coroutines-core/common/test/flow/channels/FlowViaChannelTest.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,31 @@ | ||
/* | ||
* 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 kotlin.test.* | ||
|
||
class FlowViaChannelTest : TestBase() { | ||
@Test | ||
fun testRegular() = runTest { | ||
val flow = flowViaChannel<Int> { | ||
it.send(1) | ||
it.send(2) | ||
it.close() | ||
} | ||
assertEquals(listOf(1, 2), flow.toList()) | ||
} | ||
|
||
@Test | ||
fun testConflated() = runTest { | ||
val flow = flowViaChannel<Int>(bufferSize = Channel.CONFLATED) { | ||
it.send(1) | ||
it.send(2) | ||
it.close() | ||
} | ||
assertEquals(listOf(1), flow.toList()) | ||
} | ||
} |
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
I don't think this is safe. It means any side-effects of the upstream are ignored. You still need to trigger the upstream.