Skip to content

Commit

Permalink
Fix bug, add tests
Browse files Browse the repository at this point in the history
  • Loading branch information
nomisRev committed May 10, 2024
1 parent c89e059 commit 2dce5ec
Show file tree
Hide file tree
Showing 3 changed files with 53 additions and 16 deletions.
Expand Up @@ -484,7 +484,8 @@ public suspend inline fun <Input, Output, A, reified E : Throwable> Schedule<E,
}

is Done ->
if (NonFatal(e)) Either.Left(orElse(e, decision.output)) else throw e
if (NonFatal(e)) return Either.Left(orElse(e, decision.output))
else throw e
}
}
}
Expand Down
Expand Up @@ -79,12 +79,5 @@ class FlowTest {
}
}

inline fun <A> assertThrowable(executable: () -> A): Throwable {
val a = try {
executable.invoke()
} catch (e: Throwable) {
e
}

return if (a is Throwable) a else fail("Expected an exception but found: $a")
}
inline fun assertThrowable(executable: () -> Unit): Throwable =
assertThrows<Throwable>(executable)
Expand Up @@ -9,7 +9,6 @@ import arrow.resilience.Schedule.Decision.Continue
import arrow.resilience.Schedule.Decision.Done
import kotlinx.coroutines.test.TestResult
import kotlinx.coroutines.test.runTest
import kotlinx.coroutines.withTimeout
import kotlin.math.pow
import kotlin.test.Test
import kotlin.test.assertContentEquals
Expand Down Expand Up @@ -269,15 +268,49 @@ class ScheduleTest {
.doUntil { _, output -> output > 50.0.milliseconds }
.doUntil { input, _ -> input is IllegalStateException }

val result: Either<Throwable, Unit> = withTimeout(10.seconds) {
schedule.retryOrElseEither({
throw ex
}, { t, _ -> t })
}
val result: Either<Throwable, Unit> = schedule.retryOrElseEither({
throw ex
}, { t, _ -> t })

result.fold({ assertEquals(ex, it) }, { fail("The impossible happened") })
}

@Test
fun rethrowsUnmatchedException(): TestResult = runTest {
val ex = Throwable("Hello")

val e = assertThrowable {
Schedule.forever<IllegalStateException>()
.retry { throw ex }
}
assertEquals(ex, e)
}

@Test
fun captureException(): TestResult = runTest {
val ex = IllegalStateException("Hello")
val buffer = mutableListOf<IllegalStateException>()

assertThrows<IllegalStateException> {
Schedule.recurs<IllegalStateException>(2)
.log { e, _ -> buffer.add(e) }
.retry { throw ex }
}

assertEquals(listOf(ex, ex), buffer)
}

@Test
fun retriesMatchedException(): TestResult = runTest {
val ex = IllegalStateException("Hello")
var count = 0

val i = Schedule.forever<IllegalStateException>()
.retry { if (count++ == 0) throw ex else 1 }

assertEquals(1, i)
}

@Test
fun retryRaiseIsStackSafe(): TestResult = runTest {
val count = AtomicLong(0)
Expand Down Expand Up @@ -401,3 +434,13 @@ private suspend fun <B> checkRepeat(schedule: Schedule<Long, List<B>>, expected:
}

private object CustomError

inline fun <reified A : Throwable> assertThrows(executable: () -> Unit): A {
val a = try {
executable.invoke()
} catch (e: Throwable) {
e
}

return if (a is A) a else fail("Expected an exception but found: $a")
}

0 comments on commit 2dce5ec

Please sign in to comment.