Skip to content

Commit

Permalink
Refactor GuaranteeCaseTest to use kotlin test (#3226)
Browse files Browse the repository at this point in the history
Closes #3190
  • Loading branch information
Gosunet authored Oct 26, 2023
1 parent edd0dd5 commit f384c5d
Showing 1 changed file with 43 additions and 40 deletions.
Original file line number Diff line number Diff line change
@@ -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
Expand All @@ -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<ExitCase>()

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<ExitCase>()

"release for error was invoked" {
checkAll(Arb.throwable()) { e ->
val p = CompletableDeferred<ExitCase>()
val attempted = Either.catch {
guaranteeCase<Int>(
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<ExitCase>()
val start = CompletableDeferred<Unit>()

val fiber = async {
guaranteeCase(
fa = {
start.complete(Unit)
awaitCancellation()
},
val attempted = Either.catch {
guaranteeCase<Int>(
fa = { throw e },
finalizer = { ex -> require(p.complete(ex)) }
)
}

start.await()
fiber.cancel()
p.await().shouldBeInstanceOf<ExitCase.Cancelled>()
p.await() shouldBe ExitCase.Failure(e)
attempted shouldBe Either.Left(e)
}
}
)

@Test
fun releaseForNeverWasInvoked() = runTest {
val p = CompletableDeferred<ExitCase>()
val start = CompletableDeferred<Unit>()

val fiber = async {
guaranteeCase(
fa = {
start.complete(Unit)
awaitCancellation()
},
finalizer = { ex -> require(p.complete(ex)) }
)
}

start.await()
fiber.cancel()
p.await().shouldBeInstanceOf<ExitCase.Cancelled>()
}
}

0 comments on commit f384c5d

Please sign in to comment.