Skip to content

Commit 2a815e8

Browse files
committed
Do not invoke retry predicate when exceptions is thrown from downstream
1 parent 24586c1 commit 2a815e8

File tree

2 files changed

+36
-1
lines changed

2 files changed

+36
-1
lines changed

kotlinx-coroutines-core/common/src/flow/operators/Errors.kt

+1-1
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,7 @@ public fun <T> Flow<T>.retry(
6161
try {
6262
emit(value)
6363
} catch (e: Throwable) {
64-
fromDownstream = predicate(e)
64+
fromDownstream = true
6565
throw e
6666
}
6767
}

kotlinx-coroutines-core/common/test/flow/operators/TakeTest.kt

+35
Original file line numberDiff line numberDiff line change
@@ -63,4 +63,39 @@ class TakeTest : TestBase() {
6363
assertEquals(42, flow.single())
6464
assertTrue(cancelled)
6565
}
66+
67+
@Test
68+
fun takeWithRetries() = runTest {
69+
val flow = flow {
70+
expect(1)
71+
emit(1)
72+
expect(2)
73+
emit(2)
74+
75+
while (true) {
76+
emit(42)
77+
expectUnreached()
78+
}
79+
80+
}.retry(2) {
81+
expectUnreached()
82+
true
83+
}.take(2)
84+
85+
val sum = flow.sum()
86+
assertEquals(3, sum)
87+
finish(3)
88+
}
89+
90+
@Test
91+
fun testNonIdempotentRetry() = runTest {
92+
var count = 0
93+
flow { while (true) emit(1) }
94+
.retry { count++ % 2 != 0 }
95+
.take(1)
96+
.collect {
97+
expect(1)
98+
}
99+
finish(2)
100+
}
66101
}

0 commit comments

Comments
 (0)