Skip to content

Commit d9cf848

Browse files
committed
Give example policies in tests/README better names
1 parent 3460815 commit d9cf848

File tree

2 files changed

+30
-19
lines changed

2 files changed

+30
-19
lines changed

README.md

Lines changed: 13 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,9 @@
2222
A multiplatform higher-order function for retrying operations that may fail.
2323

2424
```kotlin
25-
retry(constantDelay(delayMillis = 50L) + stopAtAttempts(10)) {
25+
val everySecondTenTimes = constantDelay(delayMillis = 1000L) + stopAtAttempts(10)
26+
27+
retry(everySecondTenTimes) {
2628
/* your code */
2729
}
2830
```
@@ -68,8 +70,10 @@ suspend fun printExchangeBetween(a: Long, b: Long) {
6870
println("$customer1 exchanged with $customer2")
6971
}
7072

73+
val fiveTimes = stopAtAttempts<Throwable>(5)
74+
7175
fun main() = runBlocking {
72-
retry(stopAtAttempts(5)) {
76+
retry(fiveTimes) {
7377
printExchangeBetween(1L, 2L)
7478
}
7579
}
@@ -92,18 +96,20 @@ import com.github.michaelbull.retry.retry
9296
import kotlinx.coroutines.runBlocking
9397
import java.sql.SQLDataException
9498

95-
val continueOnTimeout = continueIf<Throwable> { (failure) ->
96-
failure is SQLDataException
97-
}
98-
9999
suspend fun printExchangeBetween(a: Long, b: Long) {
100100
val customer1 = customers.nameFromId(a)
101101
val customer2 = customers.nameFromId(b)
102102
println("$customer1 exchanged with $customer2")
103103
}
104104

105+
val continueOnTimeout = continueIf<Throwable> { (failure) ->
106+
failure is SQLDataException
107+
}
108+
109+
val timeoutsEverySecondFiveTimes = continueOnTimeout + constantDelay(1000) + stopAtAttempts(5)
110+
105111
fun main() = runBlocking {
106-
retry(continueOnTimeout + constantDelay(20) + stopAtAttempts(5)) {
112+
retry(timeoutsEverySecondFiveTimes) {
107113
printExchangeBetween(1L, 2L)
108114
}
109115
}

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

Lines changed: 17 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -25,9 +25,10 @@ class RetryTest {
2525

2626
@Test
2727
fun retryToAttemptLimit() = runTest {
28+
val fiveTimes = stopAtAttempts<Throwable>(5)
2829
var attempts = 0
2930

30-
retry(stopAtAttempts(5)) {
31+
retry(fiveTimes) {
3132
attempts++
3233

3334
if (attempts < 5) {
@@ -40,10 +41,11 @@ class RetryTest {
4041

4142
@Test
4243
fun retryExhaustingAttemptLimit() = runTest {
44+
val tenTimes = stopAtAttempts<Throwable>(10)
4345
var attempts = 0
4446

4547
val exception = assertFailsWith<AttemptsException> {
46-
retry(stopAtAttempts(10)) {
48+
retry(tenTimes) {
4749
attempts++
4850

4951
if (attempts < 15) {
@@ -57,19 +59,22 @@ class RetryTest {
5759

5860
@Test
5961
fun retryThrowsCancellationException() = runTest {
62+
val tenTimes = stopAtAttempts<Throwable>(10)
63+
6064
assertFailsWith<CancellationException> {
61-
retry(stopAtAttempts(10)) {
65+
retry(tenTimes) {
6266
throw CancellationException()
6367
}
6468
}
6569
}
6670

6771
@Test
6872
fun retryStopsAfterCancellation() = runTest {
73+
val fiveTimes = stopAtAttempts<Throwable>(5)
6974
var attempts = 0
7075

7176
assertFailsWith<CancellationException> {
72-
retry(stopAtAttempts(5)) {
77+
retry(fiveTimes) {
7378
attempts++
7479

7580
if (attempts == 2) {
@@ -89,13 +94,13 @@ class RetryTest {
8994
failure is AttemptsException
9095
}
9196

92-
val policy = customPolicy + stopAtAttempts(15)
97+
val uptoFifteenTimes = customPolicy + stopAtAttempts(15)
9398

9499
var attempts = 0
95100
lateinit var mostRecentException: Exception
96101

97102
try {
98-
retry(policy) {
103+
retry(uptoFifteenTimes) {
99104
attempts++
100105
throw AttemptsException(attempts)
101106
}
@@ -108,11 +113,11 @@ class RetryTest {
108113

109114
@Test
110115
fun cancelRetryFromJob() = runTest {
111-
val policy = constantDelay<Throwable>(100)
116+
val every100ms = constantDelay<Throwable>(100)
112117
var attempts = 0
113118

114119
val job = backgroundScope.launch {
115-
retry(policy) {
120+
retry(every100ms) {
116121
attempts++
117122
throw AttemptsException(attempts)
118123
}
@@ -137,11 +142,11 @@ class RetryTest {
137142

138143
@Test
139144
fun cancelRetryWithinJob() = runTest {
140-
val policy = constantDelay<Throwable>(20)
145+
val every20ms = constantDelay<Throwable>(20)
141146
var attempts = 0
142147

143148
val job = launch {
144-
retry(policy) {
149+
retry(every20ms) {
145150
attempts++
146151

147152
if (attempts == 15) {
@@ -166,14 +171,14 @@ class RetryTest {
166171

167172
@Test
168173
fun cancelRetryFromWithinChildJob() = runTest {
169-
val policy = constantDelay<Throwable>(20)
174+
val every20ms = constantDelay<Throwable>(20)
170175
var attempts = 0
171176

172177
lateinit var childJobOne: Deferred<Int>
173178
lateinit var childJobTwo: Deferred<Int>
174179

175180
val parentJob = launch {
176-
retry(policy) {
181+
retry(every20ms) {
177182
childJobOne = async {
178183
delay(100)
179184
attempts

0 commit comments

Comments
 (0)