Skip to content

Commit

Permalink
Merge pull request #3064 from armanbilge/fix/syncstep-uncancelable
Browse files Browse the repository at this point in the history
Handle `Uncancelable` and `OnCancel` in `syncStep` interpreter
  • Loading branch information
djspiewak authored Jul 7, 2022
2 parents 7887b34 + 173c899 commit 07359bb
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 0 deletions.
7 changes: 7 additions & 0 deletions core/shared/src/main/scala/cats/effect/IO.scala
Original file line number Diff line number Diff line change
Expand Up @@ -869,6 +869,13 @@ sealed abstract class IO[+A] private () extends IOPlatform[A] {
}
.handleErrorWith(t => interpret(f(t)))

case IO.Uncancelable(body, _) =>
interpret(body(new Poll[IO] {
def apply[C](ioc: IO[C]): IO[C] = ioc
}))

case IO.OnCancel(ioa, _) => interpret(ioa)

case _ => SyncIO.pure(Left(io))
}

Expand Down
25 changes: 25 additions & 0 deletions tests/shared/src/test/scala/cats/effect/IOSpec.scala
Original file line number Diff line number Diff line change
Expand Up @@ -1454,6 +1454,31 @@ class IOSpec extends BaseSpec with Discipline with IOPlatformSpecification {
io must completeAs(())
i must beEqualTo(1)
}

"handle uncancelable" in {
val sio = IO.unit.uncancelable.syncStep
sio.map(_.bimap(_ => (), _ => ())) must completeAsSync(Right(()))
}

"handle onCancel" in {
val sio = IO.unit.onCancel(IO.unit).syncStep
sio.map(_.bimap(_ => (), _ => ())) must completeAsSync(Right(()))
}

"synchronously allocate a vanilla resource" in {
val sio = Resource.make(IO.unit)(_ => IO.unit).allocated.map(_._1).syncStep
sio.map(_.bimap(_ => (), _ => ())) must completeAsSync(Right(()))
}

"synchronously allocate a evalMapped resource" in {
val sio = Resource
.make(IO.unit)(_ => IO.unit)
.evalMap(_ => IO.unit)
.allocated
.map(_._1)
.syncStep
sio.map(_.bimap(_ => (), _ => ())) must completeAsSync(Right(()))
}
}

"fiber repeated yielding test" in real {
Expand Down

0 comments on commit 07359bb

Please sign in to comment.