|
| 1 | +package com.github.michaelbull.retry.result |
| 2 | + |
| 3 | +import com.github.michaelbull.result.Err |
| 4 | +import com.github.michaelbull.result.Ok |
| 5 | +import com.github.michaelbull.retry.policy.constantDelay |
| 6 | +import com.github.michaelbull.retry.policy.continueIf |
| 7 | +import com.github.michaelbull.retry.policy.stopAtAttempts |
| 8 | +import kotlinx.coroutines.Deferred |
| 9 | +import kotlinx.coroutines.ExperimentalCoroutinesApi |
| 10 | +import kotlinx.coroutines.async |
| 11 | +import kotlinx.coroutines.cancel |
| 12 | +import kotlinx.coroutines.delay |
| 13 | +import kotlinx.coroutines.launch |
| 14 | +import kotlinx.coroutines.test.runTest |
| 15 | +import kotlin.coroutines.cancellation.CancellationException |
| 16 | +import kotlin.test.Test |
| 17 | +import kotlin.test.assertEquals |
| 18 | +import kotlin.test.assertFailsWith |
| 19 | +import kotlin.test.assertFalse |
| 20 | +import kotlin.test.assertTrue |
| 21 | + |
| 22 | +@ExperimentalCoroutinesApi |
| 23 | +class RetryTest { |
| 24 | + |
| 25 | + private data class AttemptsError(val attempts: Int) |
| 26 | + |
| 27 | + @Test |
| 28 | + fun retryToAttemptLimit() = runTest { |
| 29 | + val fiveTimes = stopAtAttempts<AttemptsError>(5) |
| 30 | + var attempts = 0 |
| 31 | + |
| 32 | + val result = retry(fiveTimes) { |
| 33 | + attempts++ |
| 34 | + |
| 35 | + if (attempts < 5) { |
| 36 | + Err(AttemptsError(attempts)) |
| 37 | + } else { |
| 38 | + Ok(Unit) |
| 39 | + } |
| 40 | + } |
| 41 | + |
| 42 | + assertEquals(Ok(Unit), result) |
| 43 | + assertEquals(5, attempts) |
| 44 | + } |
| 45 | + |
| 46 | + @Test |
| 47 | + fun retryExhaustingAttemptLimit() = runTest { |
| 48 | + val tenTimes = stopAtAttempts<AttemptsError>(10) |
| 49 | + var attempts = 0 |
| 50 | + |
| 51 | + val result = retry(tenTimes) { |
| 52 | + attempts++ |
| 53 | + |
| 54 | + if (attempts < 15) { |
| 55 | + Err(AttemptsError(attempts)) |
| 56 | + } else { |
| 57 | + Ok(Unit) |
| 58 | + } |
| 59 | + } |
| 60 | + |
| 61 | + assertEquals(Err(AttemptsError(10)), result) |
| 62 | + assertEquals(10, attempts) |
| 63 | + } |
| 64 | + |
| 65 | + @Test |
| 66 | + fun retryThrowsCancellationException() = runTest { |
| 67 | + val tenTimes = stopAtAttempts<Unit>(10) |
| 68 | + |
| 69 | + assertFailsWith<CancellationException> { |
| 70 | + retry(tenTimes) { |
| 71 | + Ok(Unit).also { |
| 72 | + throw CancellationException() |
| 73 | + } |
| 74 | + } |
| 75 | + } |
| 76 | + } |
| 77 | + |
| 78 | + @Test |
| 79 | + fun retryStopsAfterCancellation() = runTest { |
| 80 | + val fiveTimes = stopAtAttempts<Unit>(5) |
| 81 | + var attempts = 0 |
| 82 | + |
| 83 | + assertFailsWith<CancellationException> { |
| 84 | + retry(fiveTimes) { |
| 85 | + attempts++ |
| 86 | + |
| 87 | + if (attempts == 2) { |
| 88 | + throw CancellationException() |
| 89 | + } else { |
| 90 | + Err(Unit) |
| 91 | + } |
| 92 | + } |
| 93 | + } |
| 94 | + |
| 95 | + assertEquals(2, attempts) |
| 96 | + } |
| 97 | + |
| 98 | + @Test |
| 99 | + fun retryWithCustomPolicy() = runTest { |
| 100 | + val uptoFifteenTimes = continueIf<AttemptsError> { (failure) -> |
| 101 | + failure.attempts < 15 |
| 102 | + } |
| 103 | + |
| 104 | + var attempts = 0 |
| 105 | + |
| 106 | + val result = retry(uptoFifteenTimes) { |
| 107 | + attempts++ |
| 108 | + Err(AttemptsError(attempts)) |
| 109 | + } |
| 110 | + |
| 111 | + assertEquals(Err(AttemptsError(15)), result) |
| 112 | + } |
| 113 | + |
| 114 | + @Test |
| 115 | + fun cancelRetryFromJob() = runTest { |
| 116 | + val every100ms = constantDelay<AttemptsError>(100) |
| 117 | + var attempts = 0 |
| 118 | + |
| 119 | + val job = backgroundScope.launch { |
| 120 | + retry(every100ms) { |
| 121 | + attempts++ |
| 122 | + Err(AttemptsError(attempts)) |
| 123 | + } |
| 124 | + } |
| 125 | + |
| 126 | + testScheduler.advanceTimeBy(350) |
| 127 | + testScheduler.runCurrent() |
| 128 | + |
| 129 | + job.cancel() |
| 130 | + |
| 131 | + testScheduler.advanceUntilIdle() |
| 132 | + |
| 133 | + assertTrue(job.isCancelled) |
| 134 | + assertEquals(4, attempts) |
| 135 | + |
| 136 | + testScheduler.advanceTimeBy(2000) |
| 137 | + testScheduler.runCurrent() |
| 138 | + |
| 139 | + assertTrue(job.isCancelled) |
| 140 | + assertEquals(4, attempts) |
| 141 | + } |
| 142 | + |
| 143 | + @Test |
| 144 | + fun cancelRetryWithinJob() = runTest { |
| 145 | + val every20ms = constantDelay<AttemptsError>(20) |
| 146 | + var attempts = 0 |
| 147 | + |
| 148 | + val job = launch { |
| 149 | + retry(every20ms) { |
| 150 | + attempts++ |
| 151 | + |
| 152 | + if (attempts == 15) { |
| 153 | + cancel() |
| 154 | + } |
| 155 | + |
| 156 | + Err(AttemptsError(attempts)) |
| 157 | + } |
| 158 | + } |
| 159 | + |
| 160 | + testScheduler.advanceUntilIdle() |
| 161 | + |
| 162 | + assertTrue(job.isCancelled) |
| 163 | + assertEquals(15, attempts) |
| 164 | + |
| 165 | + testScheduler.advanceTimeBy(2000) |
| 166 | + testScheduler.runCurrent() |
| 167 | + |
| 168 | + assertTrue(job.isCancelled) |
| 169 | + assertEquals(15, attempts) |
| 170 | + } |
| 171 | + |
| 172 | + @Test |
| 173 | + fun cancelRetryWithinChildJob() = runTest { |
| 174 | + val every20ms = constantDelay<AttemptsError>(20) |
| 175 | + var attempts = 0 |
| 176 | + |
| 177 | + lateinit var childJobOne: Deferred<Int> |
| 178 | + lateinit var childJobTwo: Deferred<Int> |
| 179 | + |
| 180 | + val parentJob = launch { |
| 181 | + retry(every20ms) { |
| 182 | + childJobOne = async { |
| 183 | + delay(100) |
| 184 | + attempts |
| 185 | + } |
| 186 | + |
| 187 | + childJobTwo = async { |
| 188 | + delay(50) |
| 189 | + |
| 190 | + if (attempts == 15) { |
| 191 | + cancel() |
| 192 | + } |
| 193 | + |
| 194 | + 1 |
| 195 | + } |
| 196 | + |
| 197 | + attempts = childJobOne.await() + childJobTwo.await() |
| 198 | + |
| 199 | + Err(AttemptsError(attempts)) |
| 200 | + } |
| 201 | + } |
| 202 | + |
| 203 | + testScheduler.advanceUntilIdle() |
| 204 | + |
| 205 | + assertTrue(parentJob.isCancelled) |
| 206 | + assertFalse(childJobOne.isCancelled) |
| 207 | + assertTrue(childJobTwo.isCancelled) |
| 208 | + assertEquals(15, attempts) |
| 209 | + |
| 210 | + testScheduler.advanceTimeBy(2000) |
| 211 | + testScheduler.runCurrent() |
| 212 | + |
| 213 | + assertTrue(parentJob.isCancelled) |
| 214 | + assertFalse(childJobOne.isCancelled) |
| 215 | + assertTrue(childJobTwo.isCancelled) |
| 216 | + assertEquals(15, attempts) |
| 217 | + } |
| 218 | +} |
0 commit comments