Skip to content

Commit

Permalink
move test from IOSpec to IOAppSpec and add missing error handling for…
Browse files Browse the repository at this point in the history
… attempt case in runloop
  • Loading branch information
scott-thomson239 committed Sep 10, 2023
1 parent f549ec0 commit 7ddf0d1
Show file tree
Hide file tree
Showing 4 changed files with 38 additions and 17 deletions.
9 changes: 6 additions & 3 deletions core/shared/src/main/scala/cats/effect/IOFiber.scala
Original file line number Diff line number Diff line change
Expand Up @@ -241,10 +241,11 @@ private final class IOFiber[A](

case 1 =>
val cur = cur0.asInstanceOf[Error]
if (!NonFatal(cur.t))
onFatalFailure(cur.t)
val ex = cur.t
if (!NonFatal(ex))
onFatalFailure(ex)

runLoop(failed(cur.t, 0), nextCancelation, nextAutoCede)
runLoop(failed(ex, 0), nextCancelation, nextAutoCede)

case 2 =>
val cur = cur0.asInstanceOf[Delay[Any]]
Expand Down Expand Up @@ -437,6 +438,8 @@ private final class IOFiber[A](
case 1 =>
val error = ioa.asInstanceOf[Error]
val t = error.t
if (!NonFatal(t))
onFatalFailure(t)
// We need to augment the exception here because it doesn't get
// forwarded to the `failed` path.
Tracing.augmentThrowable(runtime.enhancedExceptions, t, tracingEvents)
Expand Down
14 changes: 14 additions & 0 deletions tests/jvm/src/test/scala/cats/effect/IOAppSpec.scala
Original file line number Diff line number Diff line change
Expand Up @@ -187,6 +187,20 @@ class IOAppSpec extends Specification {
h.stderr() must contain("Boom!")
}

"exit on raising a fatal error with attempt" in {
val h = platform(RaiseFatalErrorAttempt, List.empty)
h.awaitStatus() mustEqual 1
h.stderr() must contain ("Boom!")
h.stdout() must not(contain("sadness"))
}

"exit on raising a fatal error with handleError" in {
val h = platform(RaiseFatalErrorHandle, List.empty)
h.awaitStatus() mustEqual 1
h.stderr() must contain ("Boom!")
h.stdout() must not(contain("sadness"))
}

"warn on global runtime collision" in {
val h = platform(GlobalRacingInit, List.empty)
h.awaitStatus() mustEqual 0
Expand Down
18 changes: 18 additions & 0 deletions tests/shared/src/main/scala/catseffect/examples.scala
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,24 @@ package examples {
}
}

object RaiseFatalErrorAttempt extends IOApp {
def run(args: List[String]): IO[ExitCode] = {
IO.raiseError[Unit](new OutOfMemoryError("Boom!"))
.attempt
.flatMap(_ => IO.println("sadness"))
.as(ExitCode.Success)
}
}

object RaiseFatalErrorHandle extends IOApp {
def run(args: List[String]): IO[ExitCode] = {
IO.raiseError[Unit](new OutOfMemoryError("Boom!"))
.handleError(_ => ())
.flatMap(_ => IO.println("sadness"))
.as(ExitCode.Success)
}
}

object Canceled extends IOApp {
def run(args: List[String]): IO[ExitCode] =
IO.canceled.as(ExitCode.Success)
Expand Down
14 changes: 0 additions & 14 deletions tests/shared/src/test/scala/cats/effect/IOSpec.scala
Original file line number Diff line number Diff line change
Expand Up @@ -345,20 +345,6 @@ class IOSpec extends BaseSpec with Discipline with IOPlatformSpecification {
} yield reported
test must completeAs(true)
}

"immediately surface fatal errors" in ticked { implicit ticker =>
val error = new VirtualMachineError {}
val io = IO.raiseError[Unit](error).voidError

val fatalThrown =
try {
unsafeRun[Unit](io)
false
} catch {
case t: Throwable => t eq error
}
IO(fatalThrown) must completeAs(true)
}
}

"suspension of side effects" should {
Expand Down

0 comments on commit 7ddf0d1

Please sign in to comment.