Skip to content

Commit 3460815

Browse files
committed
Add test case for cancellation within child job
1 parent f266f6e commit 3460815

File tree

1 file changed

+65
-14
lines changed
  • kotlin-retry/src/commonTest/kotlin/com/github/michaelbull/retry

1 file changed

+65
-14
lines changed

kotlin-retry/src/commonTest/kotlin/com/github/michaelbull/retry/RetryTest.kt

Lines changed: 65 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -4,14 +4,18 @@ import com.github.michaelbull.retry.policy.constantDelay
44
import com.github.michaelbull.retry.policy.continueIf
55
import com.github.michaelbull.retry.policy.plus
66
import com.github.michaelbull.retry.policy.stopAtAttempts
7+
import kotlinx.coroutines.Deferred
78
import kotlinx.coroutines.ExperimentalCoroutinesApi
9+
import kotlinx.coroutines.async
810
import kotlinx.coroutines.cancel
11+
import kotlinx.coroutines.delay
912
import kotlinx.coroutines.launch
1013
import kotlinx.coroutines.test.runTest
1114
import kotlin.coroutines.cancellation.CancellationException
1215
import kotlin.test.Test
1316
import kotlin.test.assertEquals
1417
import kotlin.test.assertFailsWith
18+
import kotlin.test.assertFalse
1519
import kotlin.test.assertTrue
1620

1721
@ExperimentalCoroutinesApi
@@ -103,7 +107,36 @@ class RetryTest {
103107
}
104108

105109
@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 {
107140
val policy = constantDelay<Throwable>(20)
108141
var attempts = 0
109142

@@ -132,31 +165,49 @@ class RetryTest {
132165
}
133166

134167
@Test
135-
fun cancelRetryFromParentJob() = runTest {
136-
val policy = constantDelay<Throwable>(100)
168+
fun cancelRetryFromWithinChildJob() = runTest {
169+
val policy = constantDelay<Throwable>(20)
137170
var attempts = 0
138171

139-
val job = backgroundScope.launch {
172+
lateinit var childJobOne: Deferred<Int>
173+
lateinit var childJobTwo: Deferred<Int>
174+
175+
val parentJob = launch {
140176
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+
142194
throw AttemptsException(attempts)
143195
}
144196
}
145197

146-
testScheduler.advanceTimeBy(350)
147-
testScheduler.runCurrent()
148-
149-
job.cancel()
150-
151198
testScheduler.advanceUntilIdle()
152199

153-
assertTrue(job.isCancelled)
154-
assertEquals(4, attempts)
200+
assertTrue(parentJob.isCancelled)
201+
assertFalse(childJobOne.isCancelled)
202+
assertTrue(childJobTwo.isCancelled)
203+
assertEquals(15, attempts)
155204

156205
testScheduler.advanceTimeBy(2000)
157206
testScheduler.runCurrent()
158207

159-
assertTrue(job.isCancelled)
160-
assertEquals(4, attempts)
208+
assertTrue(parentJob.isCancelled)
209+
assertFalse(childJobOne.isCancelled)
210+
assertTrue(childJobTwo.isCancelled)
211+
assertEquals(15, attempts)
161212
}
162213
}

0 commit comments

Comments
 (0)