Skip to content

Commit a0b7174

Browse files
committed
Add retryUnless policy
Facilitates creating a RetryPolicy that only retries if a given predicate is not satisfied. Analogous to takeUnless in the stdlib.
1 parent f2ddbd6 commit a0b7174

File tree

2 files changed

+51
-3
lines changed

2 files changed

+51
-3
lines changed

src/main/kotlin/com/github/michaelbull/retry/policy/RetryIf.kt renamed to src/main/kotlin/com/github/michaelbull/retry/policy/Predicate.kt

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,3 +17,17 @@ inline fun <E> retryIf(crossinline predicate: RetryFailure<E>.() -> Boolean): Re
1717
StopRetrying
1818
}
1919
}
20+
21+
/**
22+
* Creates a [RetryPolicy] that returns an [instruction][RetryInstruction] to
23+
* [ContinueRetrying] if the [RetryFailure] _does not_ satisfy the given
24+
* [predicate], or an [instruction][RetryInstruction] to [StopRetrying] if it
25+
* does.
26+
*/
27+
inline fun <E> retryUnless(crossinline predicate: RetryFailure<E>.() -> Boolean): RetryPolicy<E> = {
28+
if (!predicate(this)) {
29+
ContinueRetrying
30+
} else {
31+
StopRetrying
32+
}
33+
}

src/test/kotlin/com/github/michaelbull/retry/policy/RetryIfTest.kt renamed to src/test/kotlin/com/github/michaelbull/retry/policy/PredicateTest.kt

Lines changed: 37 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,14 +8,18 @@ import org.junit.jupiter.api.Test
88
import org.junit.jupiter.api.assertThrows
99

1010
@ExperimentalCoroutinesApi
11-
class RetryIfTest {
11+
class PredicateTest {
1212

1313
private val retryIllegalState = retryIf<Throwable> {
1414
reason is IllegalStateException
1515
}
1616

17+
private val dontRetryOob = retryUnless<Throwable> {
18+
reason is IndexOutOfBoundsException
19+
}
20+
1721
@Test
18-
fun `retry with filter should retry specified exception`() = runBlockingTest {
22+
fun `retryIf should retry specified exception`() = runBlockingTest {
1923
var attempts = 0
2024

2125
retry(limitAttempts(5) + retryIllegalState) {
@@ -29,7 +33,7 @@ class RetryIfTest {
2933
}
3034

3135
@Test
32-
fun `retry with filter should not retry unspecified exception`() = runBlockingTest {
36+
fun `retryIf should not retry unspecified exception`() = runBlockingTest {
3337
var attempts = 0
3438

3539
assertThrows<UnsupportedOperationException> {
@@ -43,4 +47,34 @@ class RetryIfTest {
4347

4448
assertEquals(1, attempts)
4549
}
50+
51+
@Test
52+
fun `retryUnless should not retry specified exception`() = runBlockingTest {
53+
var attempts = 0
54+
55+
assertThrows<IndexOutOfBoundsException> {
56+
retry(limitAttempts(5) + dontRetryOob) {
57+
attempts++
58+
if (attempts < 5) {
59+
throw IndexOutOfBoundsException()
60+
}
61+
}
62+
}
63+
64+
assertEquals(1, attempts)
65+
}
66+
67+
@Test
68+
fun `retryUnless should retry unspecified exception`() = runBlockingTest {
69+
var attempts = 0
70+
71+
retry(limitAttempts(5) + dontRetryOob) {
72+
attempts++
73+
if (attempts < 5) {
74+
throw IllegalStateException()
75+
}
76+
}
77+
78+
assertEquals(5, attempts)
79+
}
4680
}

0 commit comments

Comments
 (0)