-
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.
- Loading branch information
Showing
2 changed files
with
66 additions
and
0 deletions.
There are no files selected for viewing
30 changes: 30 additions & 0 deletions
30
...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,30 @@ | ||
import arrow.core.Either | ||
import arrow.core.raise.Raise | ||
import arrow.core.raise.either | ||
import arrow.resilience.Schedule | ||
import arrow.resilience.ScheduleStep | ||
import kotlinx.coroutines.currentCoroutineContext | ||
import kotlinx.coroutines.delay | ||
import kotlinx.coroutines.ensureActive | ||
import kotlin.time.Duration | ||
|
||
public suspend fun <Error, Result, Output> Schedule<Error, Output>.retry( | ||
action: suspend Raise<Error>.() -> Result, | ||
): Either<Error, Result> { | ||
var step: ScheduleStep<Error, Output> = step | ||
|
||
while (true) { | ||
currentCoroutineContext().ensureActive() | ||
when (val result = either { action() }) { | ||
is Either.Right -> return result | ||
is Either.Left -> when (val decision = step(result.value)) { | ||
is Schedule.Decision.Continue -> { | ||
if (decision.delay != Duration.ZERO) delay(decision.delay) | ||
step = decision.step | ||
} | ||
|
||
is Schedule.Decision.Done -> return result | ||
} | ||
} | ||
} | ||
} |
36 changes: 36 additions & 0 deletions
36
...esilience/arrow-resilience/src/commonTest/kotlin/arrow/resilience/ScheduleEitherKtTest.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,36 @@ | ||
package arrow.resilience | ||
|
||
import arrow.atomic.AtomicLong | ||
import arrow.core.Either | ||
import kotlinx.coroutines.test.TestResult | ||
import kotlinx.coroutines.test.runTest | ||
import retry | ||
import kotlin.test.Test | ||
import kotlin.test.assertEquals | ||
import kotlin.test.assertTrue | ||
|
||
class ScheduleEitherKtTest { | ||
|
||
@Test | ||
fun retryIsStackSafe(): TestResult = runTest { | ||
val count = AtomicLong(0) | ||
val iterations = 20_000L | ||
|
||
val result = Schedule.recurs<CustomError>(iterations).retry { | ||
count.incrementAndGet() | ||
raise(CustomError) | ||
} | ||
|
||
assertTrue { result is Either.Left } | ||
assertEquals(iterations + 1, count.get()) | ||
} | ||
|
||
@Test | ||
fun retrySucceedsIfRightIsReturned(): TestResult = runTest { | ||
val result = Schedule.recurs<CustomError>(0).retry { 1 } | ||
|
||
assertTrue { result is Either.Right && result.value == 1 } | ||
} | ||
} | ||
|
||
private object CustomError |