diff --git a/arrow-libs/fx/arrow-fx-coroutines/src/commonTest/kotlin/arrow/fx/coroutines/GuaranteeCaseTest.kt b/arrow-libs/fx/arrow-fx-coroutines/src/commonTest/kotlin/arrow/fx/coroutines/GuaranteeCaseTest.kt index 2f636a7b381..1795c98fc0f 100644 --- a/arrow-libs/fx/arrow-fx-coroutines/src/commonTest/kotlin/arrow/fx/coroutines/GuaranteeCaseTest.kt +++ b/arrow-libs/fx/arrow-fx-coroutines/src/commonTest/kotlin/arrow/fx/coroutines/GuaranteeCaseTest.kt @@ -1,7 +1,6 @@ package arrow.fx.coroutines import arrow.core.Either -import io.kotest.core.spec.style.StringSpec import io.kotest.matchers.shouldBe import io.kotest.matchers.types.shouldBeInstanceOf import io.kotest.property.Arb @@ -10,55 +9,59 @@ import io.kotest.property.checkAll import kotlinx.coroutines.CompletableDeferred import kotlinx.coroutines.async import kotlinx.coroutines.awaitCancellation +import kotlinx.coroutines.test.runTest +import kotlin.test.Test -class GuaranteeCaseTest : StringSpec({ +class GuaranteeCaseTest { - "release for success was invoked" { - checkAll(Arb.int()) { i -> - val p = CompletableDeferred() - - val res = guaranteeCase( - fa = { i }, - finalizer = { ex -> require(p.complete(ex)) } - ) - - p.await() shouldBe ExitCase.Completed - res shouldBe i - } - } + @Test + fun releaseForSuccessWasInvoked() = runTest { + checkAll(Arb.int()) { i -> + val p = CompletableDeferred() - "release for error was invoked" { - checkAll(Arb.throwable()) { e -> - val p = CompletableDeferred() - val attempted = Either.catch { - guaranteeCase( - fa = { throw e }, - finalizer = { ex -> require(p.complete(ex)) } - ) - } + val res = guaranteeCase( + fa = { i }, + finalizer = { ex -> require(p.complete(ex)) } + ) - p.await() shouldBe ExitCase.Failure(e) - attempted shouldBe Either.Left(e) - } + p.await() shouldBe ExitCase.Completed + res shouldBe i } + } - "release for never was invoked" { + @Test + fun releaseForErrorWasInvoked() = runTest { + checkAll(Arb.throwable()) { e -> val p = CompletableDeferred() - val start = CompletableDeferred() - - val fiber = async { - guaranteeCase( - fa = { - start.complete(Unit) - awaitCancellation() - }, + val attempted = Either.catch { + guaranteeCase( + fa = { throw e }, finalizer = { ex -> require(p.complete(ex)) } ) } - start.await() - fiber.cancel() - p.await().shouldBeInstanceOf() + p.await() shouldBe ExitCase.Failure(e) + attempted shouldBe Either.Left(e) } } -) + + @Test + fun releaseForNeverWasInvoked() = runTest { + val p = CompletableDeferred() + val start = CompletableDeferred() + + val fiber = async { + guaranteeCase( + fa = { + start.complete(Unit) + awaitCancellation() + }, + finalizer = { ex -> require(p.complete(ex)) } + ) + } + + start.await() + fiber.cancel() + p.await().shouldBeInstanceOf() + } +}