-
Notifications
You must be signed in to change notification settings - Fork 451
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
added retryRaise and retryEither functions
- Loading branch information
Showing
3 changed files
with
139 additions
and
0 deletions.
There are no files selected for viewing
6 changes: 6 additions & 0 deletions
6
arrow-libs/resilience/arrow-resilience/api/arrow-resilience.api
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
64 changes: 64 additions & 0 deletions
64
...libs/resilience/arrow-resilience/src/commonMain/kotlin/arrow/resilience/ScheduleEither.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,64 @@ | ||
@file:OptIn(ExperimentalTypeInference::class) | ||
|
||
import arrow.core.Either | ||
import arrow.core.raise.Raise | ||
import arrow.core.raise.either | ||
import arrow.core.raise.fold | ||
import arrow.resilience.Schedule | ||
import arrow.resilience.ScheduleStep | ||
import kotlinx.coroutines.currentCoroutineContext | ||
import kotlinx.coroutines.delay | ||
import kotlinx.coroutines.ensureActive | ||
import kotlin.experimental.ExperimentalTypeInference | ||
import kotlin.time.Duration | ||
|
||
/** | ||
* Retries [action] using any [Error] that occurred as the input to the [Schedule]. | ||
* It will return the last [Error] if the [Schedule] is exhausted, and ignores the output of the [Schedule]. | ||
*/ | ||
public suspend inline fun <Error, Result, Output> Schedule<Error, Output>.retryRaise( | ||
@BuilderInference action: Raise<Error>.() -> Result, | ||
): Either<Error, Result> = either { | ||
retry(this@retryRaise, action) | ||
} | ||
|
||
/** | ||
* Retries [action] using any [Error] that occurred as the input to the [Schedule]. | ||
* It will return the last [Error] if the [Schedule] is exhausted, and ignores the output of the [Schedule]. | ||
*/ | ||
public suspend inline fun <Error, Result, Output> Schedule<Error, Output>.retryEither( | ||
@BuilderInference action: () -> Either<Error, Result>, | ||
): Either<Error, Result> = retryRaise { | ||
action().bind() | ||
} | ||
|
||
/** | ||
* Retries [action] using any [Error] that occurred as the input to the [Schedule]. | ||
* It will return the last [Error] if the [Schedule] is exhausted, and ignores the output of the [Schedule]. | ||
*/ | ||
public suspend inline fun <Error, Result, Output> Raise<Error>.retry( | ||
schedule: Schedule<Error, Output>, | ||
@BuilderInference action: Raise<Error>.() -> Result, | ||
): Result { | ||
var step: ScheduleStep<Error, Output> = schedule.step | ||
|
||
while (true) { | ||
currentCoroutineContext().ensureActive() | ||
fold( | ||
action, | ||
recover = { error -> | ||
when (val decision = step(error)) { | ||
is Schedule.Decision.Continue -> { | ||
if (decision.delay != Duration.ZERO) delay(decision.delay) | ||
step = decision.step | ||
} | ||
|
||
is Schedule.Decision.Done -> raise(error) | ||
} | ||
}, | ||
transform = { result -> | ||
return result | ||
}, | ||
) | ||
} | ||
} |
69 changes: 69 additions & 0 deletions
69
.../resilience/arrow-resilience/src/commonTest/kotlin/arrow/resilience/ScheduleEitherTest.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,69 @@ | ||
package arrow.resilience | ||
|
||
import arrow.atomic.AtomicLong | ||
import arrow.core.Either | ||
import arrow.core.left | ||
import arrow.core.right | ||
import kotlinx.coroutines.test.TestResult | ||
import kotlinx.coroutines.test.runTest | ||
import retryEither | ||
import retryRaise | ||
import kotlin.test.Test | ||
import kotlin.test.assertEquals | ||
import kotlin.test.assertTrue | ||
import kotlin.time.Duration.Companion.seconds | ||
|
||
class ScheduleEitherTest { | ||
|
||
@Test | ||
fun retryRaiseIsStackSafe(): TestResult = runTest(timeout = 1.seconds) { | ||
val count = AtomicLong(0) | ||
val iterations = stackSafeIteration().toLong() | ||
|
||
suspend fun increment() { | ||
count.incrementAndGet() | ||
} | ||
|
||
val result = Schedule.recurs<CustomError>(iterations).retryRaise { | ||
increment() | ||
raise(CustomError) | ||
} | ||
|
||
assertTrue { result is Either.Left } | ||
assertEquals(iterations + 1, count.get()) | ||
} | ||
|
||
@Test | ||
fun retryRaiseSucceedsIfErrorIsNotRaised(): TestResult = runTest { | ||
val result = Schedule.recurs<CustomError>(0).retryRaise { 1 } | ||
|
||
assertTrue { result is Either.Right && result.value == 1 } | ||
} | ||
|
||
@Test | ||
fun retryEitherIsStackSafe(): TestResult = runTest { | ||
val count = AtomicLong(0) | ||
val iterations = stackSafeIteration().toLong() | ||
|
||
suspend fun increment() { | ||
count.incrementAndGet() | ||
} | ||
|
||
val result = Schedule.recurs<CustomError>(iterations).retryEither { | ||
increment() | ||
CustomError.left() | ||
} | ||
|
||
assertTrue { result is Either.Left } | ||
assertEquals(iterations + 1, count.get()) | ||
} | ||
|
||
@Test | ||
fun retryEitherSucceedsIfErrorIsNotRaised(): TestResult = runTest { | ||
val result = Schedule.recurs<CustomError>(0).retryEither { 1.right() } | ||
|
||
assertTrue { result is Either.Right && result.value == 1 } | ||
} | ||
} | ||
|
||
private object CustomError |