/
RetryTest.kt
164 lines (144 loc) · 3.84 KB
/
RetryTest.kt
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
/*
* 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 kotlin.test.*
class RetryTest : TestBase() {
@Test
fun testRetryWhen() = runTest {
expect(1)
val flow = flow {
emit(1)
throw TestException()
}
val sum = flow.retryWhen { cause, attempt ->
assertTrue(cause is TestException)
expect(2 + attempt.toInt())
attempt < 3
}.catch { cause ->
expect(6)
assertTrue(cause is TestException)
}.sum()
assertEquals(4, sum)
finish(7)
}
@Test
fun testRetry() = runTest {
var counter = 0
val flow = flow {
emit(1)
if (++counter < 4) throw TestException()
}
assertEquals(4, flow.retry(4).sum())
counter = 0
assertFailsWith<TestException>(flow)
counter = 0
assertFailsWith<TestException>(flow.retry(2))
}
@Test
fun testRetryPredicate() = runTest {
var counter = 0
val flow = flow {
emit(1);
if (++counter == 1) throw TestException()
}
assertEquals(2, flow.retry(1) { it is TestException }.sum())
counter = 0
assertFailsWith<TestException>(flow.retry(1) { it !is TestException })
}
@Test
fun testRetryExceptionFromDownstream() = runTest {
var executed = 0
val flow = flow {
emit(1)
}.retry(42).map {
++executed
throw TestException()
}
assertFailsWith<TestException>(flow)
assertEquals(1, executed)
}
@Test
fun testWithTimeoutRetried() = runTest {
var state = 0
val flow = flow {
if (state++ == 0) {
expect(1)
withTimeout(1) {
hang { expect(2) }
}
expectUnreached()
}
expect(3)
emit(1)
}.retry(1)
assertEquals(1, flow.single())
finish(4)
}
@Test
fun testCancellationFromUpstreamIsNotRetried() = runTest {
val flow = flow<Int> {
hang { }
}.retry()
val job = launch {
expect(1)
flow.collect { }
}
yield()
expect(2)
job.cancelAndJoin()
finish(3)
}
@Test
fun testUpstreamExceptionConcurrentWithDownstream() = runTest {
val flow = flow {
try {
expect(1)
emit(1)
} finally {
expect(3)
throw TestException()
}
}.retry { expectUnreached(); true }.onEach {
expect(2)
throw TestException2()
}
assertFailsWith<TestException>(flow)
finish(4)
}
@Test
fun testUpstreamExceptionConcurrentWithDownstreamCancellation() = runTest {
val flow = flow {
try {
expect(1)
emit(1)
} finally {
expect(3)
throw TestException()
}
}.retry { expectUnreached(); true }.onEach {
expect(2)
throw CancellationException("")
}
assertFailsWith<TestException>(flow)
finish(4)
}
@Test
fun testUpstreamCancellationIsIgnoredWhenDownstreamFails() = runTest {
val flow = flow {
try {
expect(1)
emit(1)
} finally {
expect(3)
throw CancellationException("")
}
}.retry { expectUnreached(); true }.onEach {
expect(2)
throw TestException("")
}
assertFailsWith<TestException>(flow)
finish(4)
}
}