Skip to content

Propagate exception from stateIn to the caller when the upstream fail… #2329

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Oct 22, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 12 additions & 5 deletions kotlinx-coroutines-core/common/src/flow/operators/Share.kt
Original file line number Diff line number Diff line change
Expand Up @@ -330,13 +330,20 @@ private fun <T> CoroutineScope.launchSharingDeferred(
result: CompletableDeferred<StateFlow<T>>
) {
launch(context) {
var state: MutableStateFlow<T>? = null
upstream.collect { value ->
state?.let { it.value = value } ?: run {
state = MutableStateFlow(value).also {
result.complete(it.asStateFlow())
try {
var state: MutableStateFlow<T>? = null
upstream.collect { value ->
state?.let { it.value = value } ?: run {
state = MutableStateFlow(value).also {
result.complete(it.asStateFlow())
}
}
}
} catch (e: Throwable) {
// Notify the waiter that the flow has failed
result.completeExceptionally(e)
// But still cancel the scope where state was (not) produced
throw e
}
}
}
Expand Down
25 changes: 18 additions & 7 deletions kotlinx-coroutines-core/common/test/flow/sharing/StateInTest.kt
Original file line number Diff line number Diff line change
Expand Up @@ -30,21 +30,21 @@ class StateInTest : TestBase() {

@Test
fun testUpstreamCompletedNoInitialValue() =
testUpstreamCompletedOrFailedReset(failed = false, iv = false)
testUpstreamCompletedOrFailedReset(failed = false, withInitialValue = false)

@Test
fun testUpstreamFailedNoInitialValue() =
testUpstreamCompletedOrFailedReset(failed = true, iv = false)
testUpstreamCompletedOrFailedReset(failed = true, withInitialValue = false)

@Test
fun testUpstreamCompletedWithInitialValue() =
testUpstreamCompletedOrFailedReset(failed = false, iv = true)
testUpstreamCompletedOrFailedReset(failed = false, withInitialValue = true)

@Test
fun testUpstreamFailedWithInitialValue() =
testUpstreamCompletedOrFailedReset(failed = true, iv = true)
testUpstreamCompletedOrFailedReset(failed = true, withInitialValue = true)

private fun testUpstreamCompletedOrFailedReset(failed: Boolean, iv: Boolean) = runTest {
private fun testUpstreamCompletedOrFailedReset(failed: Boolean, withInitialValue: Boolean) = runTest {
val emitted = Job()
val terminate = Job()
val sharingJob = CompletableDeferred<Unit>()
Expand All @@ -56,7 +56,7 @@ class StateInTest : TestBase() {
}
val scope = this + sharingJob
val shared: StateFlow<String?>
if (iv) {
if (withInitialValue) {
shared = upstream.stateIn(scope, SharingStarted.Eagerly, null)
assertEquals(null, shared.value)
} else {
Expand All @@ -75,4 +75,15 @@ class StateInTest : TestBase() {
assertNull(sharingJob.getCompletionExceptionOrNull())
}
}
}

@Test
fun testUpstreamFailedIMmediatelyWithInitialValue() = runTest {
val ceh = CoroutineExceptionHandler { _, _ -> expect(2) }
val flow = flow<Int> {
expect(1)
throw TestException()
}
assertFailsWith<TestException> { flow.stateIn(CoroutineScope(ceh)) }
finish(3)
}
}