forked from Kotlin/kotlinx.coroutines
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* Introduce Flow.onEmpty operator Fixes Kotlin#1890
- Loading branch information
Showing
5 changed files
with
156 additions
and
5 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
81 changes: 81 additions & 0 deletions
81
kotlinx-coroutines-core/common/test/flow/operators/OnEmptyTest.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,81 @@ | ||
/* | ||
* Copyright 2016-2020 JetBrains s.r.o. Use of this source code is governed by the Apache 2.0 license. | ||
*/ | ||
|
||
package kotlinx.coroutines.flow.operators | ||
|
||
import kotlinx.coroutines.* | ||
import kotlinx.coroutines.flow.* | ||
import kotlin.test.* | ||
|
||
class OnEmptyTest : TestBase() { | ||
|
||
@Test | ||
fun testOnEmptyInvoked() = runTest { | ||
val flow = emptyFlow<Int>().onEmpty { emit(1) } | ||
assertEquals(1, flow.single()) | ||
} | ||
|
||
@Test | ||
fun testOnEmptyNotInvoked() = runTest { | ||
val flow = flowOf(1).onEmpty { emit(2) } | ||
assertEquals(1, flow.single()) | ||
} | ||
|
||
@Test | ||
fun testOnEmptyNotInvokedOnError() = runTest { | ||
val flow = flow<Int> { | ||
throw TestException() | ||
}.onEmpty { expectUnreached() } | ||
assertFailsWith<TestException>(flow) | ||
} | ||
|
||
@Test | ||
fun testOnEmptyNotInvokedOnCancellation() = runTest { | ||
val flow = flow<Int> { | ||
expect(2) | ||
hang { expect(4) } | ||
}.onEmpty { expectUnreached() } | ||
|
||
expect(1) | ||
val job = flow.onEach { expectUnreached() }.launchIn(this) | ||
yield() | ||
expect(3) | ||
job.cancelAndJoin() | ||
finish(5) | ||
} | ||
|
||
@Test | ||
fun testOnEmptyCancellation() = runTest { | ||
val flow = emptyFlow<Int>().onEmpty { | ||
expect(2) | ||
hang { expect(4) } | ||
emit(1) | ||
} | ||
expect(1) | ||
val job = flow.onEach { expectUnreached() }.launchIn(this) | ||
yield() | ||
expect(3) | ||
job.cancelAndJoin() | ||
finish(5) | ||
} | ||
|
||
@Test | ||
fun testTransparencyViolation() = runTest { | ||
val flow = emptyFlow<Int>().onEmpty { | ||
expect(2) | ||
coroutineScope { | ||
launch { | ||
try { | ||
emit(1) | ||
} catch (e: IllegalStateException) { | ||
expect(3) | ||
} | ||
} | ||
} | ||
} | ||
expect(1) | ||
assertNull(flow.singleOrNull()) | ||
finish(4) | ||
} | ||
} |
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