Skip to content

Instantly share code, notes, and snippets.

@djspiewak
Created May 28, 2024 20:59
Show Gist options
  • Save djspiewak/0e974d47e6daa0a30f0d3041c69c6055 to your computer and use it in GitHub Desktop.
Save djspiewak/0e974d47e6daa0a30f0d3041c69c6055 to your computer and use it in GitHub Desktop.
"core PC state machine" should {
import cats.effect.kernel.{GenConcurrent, Outcome}
import cats.effect.kernel.implicits._
import cats.syntax.all._
type F[A] = PureConc[Int, A]
val F = GenConcurrent[F]
"run finalizers when canceling never" in {
val t = for {
c <- F.ref(0)
latch <- F.deferred[Unit]
fib <- F.start((latch.complete(()) *> F.never[Unit]).onCancel(c.update(_ + 1)))
_ <- latch.get
_ <- fib.cancel
v <- c.get
} yield v
pure.run(t) mustEqual Outcome.Succeeded(Some(1))
}
"run finalizers when canceling Deferred#get" in {
val t = for {
c <- F.ref(0)
latch <- F.deferred[Unit]
hang <- F.deferred[Unit]
fib <- F.start((latch.complete(()) *> hang.get).onCancel(c.update(_ + 1)))
_ <- latch.get
_ <- fib.cancel
v <- c.get
} yield v
pure.run(t) mustEqual Outcome.Succeeded(Some(1))
}
"run finalizers when canceling Fiber#join" in {
val t = for {
c <- F.ref(0)
latch <- F.deferred[Unit]
hang <- F.start(F.never[Unit])
fib <- F.start((latch.complete(()) *> hang.join).onCancel(c.update(_ + 1)))
_ <- latch.get
_ <- fib.cancel
v <- c.get
} yield v
pure.run(t) mustEqual Outcome.Succeeded(Some(1))
}
"hang when canceling uncancelable never" in {
val t = for {
latch <- F.deferred[Unit]
f <- F.start((latch.complete(()) *> F.never[Unit]).uncancelable)
_ <- latch.get
_ <- f.cancel
} yield ()
pure.run(t) mustEqual Outcome.Succeeded(None)
}
"hang when canceling uncancelable Deferred#get" in {
val t = for {
latch <- F.deferred[Unit]
hang <- F.deferred[Unit]
f <- F.start((latch.complete(()) *> hang.get).uncancelable)
_ <- latch.get
_ <- f.cancel
} yield ()
pure.run(t) mustEqual Outcome.Succeeded(None)
}
"hang when canceling uncancelable Fiber#join" in {
val t = for {
latch <- F.deferred[Unit]
hang <- F.start(F.never[Unit])
f <- F.start((latch.complete(()) *> hang.join).uncancelable)
_ <- latch.get
_ <- f.cancel
} yield ()
pure.run(t) mustEqual Outcome.Succeeded(None)
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment