@@ -4,14 +4,18 @@ import com.github.michaelbull.retry.policy.constantDelay
4
4
import com.github.michaelbull.retry.policy.continueIf
5
5
import com.github.michaelbull.retry.policy.plus
6
6
import com.github.michaelbull.retry.policy.stopAtAttempts
7
+ import kotlinx.coroutines.Deferred
7
8
import kotlinx.coroutines.ExperimentalCoroutinesApi
9
+ import kotlinx.coroutines.async
8
10
import kotlinx.coroutines.cancel
11
+ import kotlinx.coroutines.delay
9
12
import kotlinx.coroutines.launch
10
13
import kotlinx.coroutines.test.runTest
11
14
import kotlin.coroutines.cancellation.CancellationException
12
15
import kotlin.test.Test
13
16
import kotlin.test.assertEquals
14
17
import kotlin.test.assertFailsWith
18
+ import kotlin.test.assertFalse
15
19
import kotlin.test.assertTrue
16
20
17
21
@ExperimentalCoroutinesApi
@@ -103,7 +107,36 @@ class RetryTest {
103
107
}
104
108
105
109
@Test
106
- fun cancelRetryFromChildJob () = runTest {
110
+ fun cancelRetryFromJob () = runTest {
111
+ val policy = constantDelay<Throwable >(100 )
112
+ var attempts = 0
113
+
114
+ val job = backgroundScope.launch {
115
+ retry(policy) {
116
+ attempts++
117
+ throw AttemptsException (attempts)
118
+ }
119
+ }
120
+
121
+ testScheduler.advanceTimeBy(350 )
122
+ testScheduler.runCurrent()
123
+
124
+ job.cancel()
125
+
126
+ testScheduler.advanceUntilIdle()
127
+
128
+ assertTrue(job.isCancelled)
129
+ assertEquals(4 , attempts)
130
+
131
+ testScheduler.advanceTimeBy(2000 )
132
+ testScheduler.runCurrent()
133
+
134
+ assertTrue(job.isCancelled)
135
+ assertEquals(4 , attempts)
136
+ }
137
+
138
+ @Test
139
+ fun cancelRetryWithinJob () = runTest {
107
140
val policy = constantDelay<Throwable >(20 )
108
141
var attempts = 0
109
142
@@ -132,31 +165,49 @@ class RetryTest {
132
165
}
133
166
134
167
@Test
135
- fun cancelRetryFromParentJob () = runTest {
136
- val policy = constantDelay<Throwable >(100 )
168
+ fun cancelRetryFromWithinChildJob () = runTest {
169
+ val policy = constantDelay<Throwable >(20 )
137
170
var attempts = 0
138
171
139
- val job = backgroundScope.launch {
172
+ lateinit var childJobOne: Deferred <Int >
173
+ lateinit var childJobTwo: Deferred <Int >
174
+
175
+ val parentJob = launch {
140
176
retry(policy) {
141
- attempts++
177
+ childJobOne = async {
178
+ delay(100 )
179
+ attempts
180
+ }
181
+
182
+ childJobTwo = async {
183
+ delay(50 )
184
+
185
+ if (attempts == 15 ) {
186
+ cancel()
187
+ }
188
+
189
+ 1
190
+ }
191
+
192
+ attempts = childJobOne.await() + childJobTwo.await()
193
+
142
194
throw AttemptsException (attempts)
143
195
}
144
196
}
145
197
146
- testScheduler.advanceTimeBy(350 )
147
- testScheduler.runCurrent()
148
-
149
- job.cancel()
150
-
151
198
testScheduler.advanceUntilIdle()
152
199
153
- assertTrue(job.isCancelled)
154
- assertEquals(4 , attempts)
200
+ assertTrue(parentJob.isCancelled)
201
+ assertFalse(childJobOne.isCancelled)
202
+ assertTrue(childJobTwo.isCancelled)
203
+ assertEquals(15 , attempts)
155
204
156
205
testScheduler.advanceTimeBy(2000 )
157
206
testScheduler.runCurrent()
158
207
159
- assertTrue(job.isCancelled)
160
- assertEquals(4 , attempts)
208
+ assertTrue(parentJob.isCancelled)
209
+ assertFalse(childJobOne.isCancelled)
210
+ assertTrue(childJobTwo.isCancelled)
211
+ assertEquals(15 , attempts)
161
212
}
162
213
}
0 commit comments