-
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.
Flow.first operators family (without firstOrNull and firstOrDefault s…
…upport) Fixes #1078
- Loading branch information
Showing
3 changed files
with
133 additions
and
2 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
86 changes: 86 additions & 0 deletions
86
kotlinx-coroutines-core/common/test/flow/terminal/FirstTest.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,86 @@ | ||
/* | ||
* 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 FirstTest : TestBase() { | ||
@Test | ||
fun testFirst() = runTest { | ||
val flow = flowOf(1, 2, 3) | ||
assertEquals(1, flow.first()) | ||
} | ||
|
||
@Test | ||
fun testNulls() = runTest { | ||
val flow = flowOf(null, 1) | ||
assertNull(flow.first()) | ||
assertNull(flow.first { it == null }) | ||
assertEquals(1, flow.first { it != null }) | ||
} | ||
|
||
@Test | ||
fun testFirstWithPredicate() = runTest { | ||
val flow = flowOf(1, 2, 3) | ||
assertEquals(1, flow.first { it > 0 }) | ||
assertEquals(2, flow.first { it > 1 }) | ||
assertFailsWith<NoSuchElementException> { flow.first { it > 3 } } | ||
} | ||
|
||
@Test | ||
fun testFirstCancellation() = runTest { | ||
val latch = Channel<Unit>() | ||
val flow = flow { | ||
coroutineScope { | ||
launch { | ||
latch.send(Unit) | ||
hang { expect(1) } | ||
} | ||
emit(1) | ||
emit(2) | ||
} | ||
} | ||
|
||
|
||
val result = flow.first { | ||
latch.receive() | ||
true | ||
} | ||
assertEquals(1, result) | ||
finish(2) | ||
} | ||
|
||
@Test | ||
fun testEmptyFlow() = runTest { | ||
assertFailsWith<NoSuchElementException> { emptyFlow<Int>().first() } | ||
assertFailsWith<NoSuchElementException> { emptyFlow<Int>().first { true } } | ||
} | ||
|
||
@Test | ||
fun testErrorCancelsUpstream() = runTest { | ||
val latch = Channel<Unit>() | ||
val flow = flow { | ||
coroutineScope { | ||
launch { | ||
latch.send(Unit) | ||
hang { expect(1) } | ||
} | ||
emit(1) | ||
} | ||
} | ||
|
||
assertFailsWith<TestException> { | ||
flow.first { | ||
latch.receive() | ||
throw TestException() | ||
} | ||
} | ||
|
||
assertEquals(1, flow.first()) | ||
finish(2) | ||
} | ||
} |