Skip to content

Instantly share code, notes, and snippets.

@djspiewak
Created May 28, 2024 20:59

Revisions

  1. djspiewak created this gist May 28, 2024.
    84 changes: 84 additions & 0 deletions new-specs.scala
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,84 @@
    "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)
    }
    }