Skip to content

Commit

Permalink
Ignore should not recover from unchecked exceptions (zio#2148)
Browse files Browse the repository at this point in the history
* Ignore should not recover from unchecked exceptions

* Remove unused imports from ignore test

* Fix ZManaged tests relying on old .ignore behaviour

* Fix StreamSpec tests relying on old ZIO.ignore behaviour
  • Loading branch information
alexvanolst authored and ghostdogpr committed Nov 4, 2019
1 parent 122b029 commit 7191ae6
Show file tree
Hide file tree
Showing 4 changed files with 16 additions and 4 deletions.
11 changes: 11 additions & 0 deletions core-tests/shared/src/test/scala/zio/ZIOSpec.scala
Original file line number Diff line number Diff line change
Expand Up @@ -119,6 +119,17 @@ object ZIOSpec
assertM(ZIO.fail("Fail").head.either, isLeft(isSome(equalTo("Fail"))))
}
),
suite("ignore")(
testM("return success as Unit") {
assertM(ZIO.succeed(11).ignore, equalTo(()))
},
testM("return failure as Unit") {
assertM(ZIO.fail(123).ignore, equalTo(()))
},
testM("not catch throwable") {
assertM(ZIO.die(ExampleError).ignore.run, dies(equalTo(ExampleError)))
}
),
suite("left")(
testM("on Left value") {
assertM(ZIO.succeed(Left("Left")).left, equalTo("Left"))
Expand Down
6 changes: 3 additions & 3 deletions core-tests/shared/src/test/scala/zio/ZManagedSpec.scala
Original file line number Diff line number Diff line change
Expand Up @@ -212,23 +212,23 @@ object ZManagedSpec
effects <- Ref.make[List[Int]](Nil)
res = (x: Int) => Managed.make(effects.update(x :: _).unit)(_ => effects.update(x :: _))
program = res(1).flatMap(_ => ZManaged.interrupt).foldM(_ => res(2), _ => res(3))
values <- program.use_(ZIO.unit).ignore *> effects.get
values <- program.use_(ZIO.unit).sandbox.ignore *> effects.get
} yield assert(values, equalTo(List(1, 1)))
},
testM("Invokes cleanups on interrupt - 2") {
for {
effects <- Ref.make[List[Int]](Nil)
res = (x: Int) => Managed.make(effects.update(x :: _).unit)(_ => effects.update(x :: _))
program = res(1).flatMap(_ => ZManaged.fail(())).foldM(_ => res(2), _ => res(3))
values <- program.use_(ZIO.interrupt).ignore *> effects.get
values <- program.use_(ZIO.interrupt).sandbox.ignore *> effects.get
} yield assert(values, equalTo(List(1, 2, 2, 1)))
},
testM("Invokes cleanups on interrupt - 3") {
for {
effects <- Ref.make[List[Int]](Nil)
res = (x: Int) => Managed.make(effects.update(x :: _).unit)(_ => effects.update(x :: _))
program = res(1).flatMap(_ => ZManaged.fail(())).foldM(_ => res(2) *> ZManaged.interrupt, _ => res(3))
values <- program.use_(ZIO.unit).ignore *> effects.get
values <- program.use_(ZIO.unit).sandbox.ignore *> effects.get
} yield assert(values, equalTo(List(1, 2, 2, 1)))
}
),
Expand Down
2 changes: 1 addition & 1 deletion core/shared/src/main/scala/zio/ZIO.scala
Original file line number Diff line number Diff line change
Expand Up @@ -525,7 +525,7 @@ sealed trait ZIO[-R, +E, +A] extends Serializable { self =>
/**
* Returns a new effect that ignores the success or failure of this effect.
*/
final def ignore: URIO[R, Unit] = self.foldCauseM(_ => ZIO.unit, _ => ZIO.unit)
final def ignore: URIO[R, Unit] = self.foldM(_ => ZIO.unit, _ => ZIO.unit)

/**
* Returns a new effect that ensures that any fibers that are forked by
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1594,6 +1594,7 @@ object StreamSpec
.tap(_ => ZIO.sleep(Duration.Infinity))
.timeout(Duration.Zero)
.runDrain
.sandbox
.ignore
.map(_ => true),
isTrue
Expand Down

0 comments on commit 7191ae6

Please sign in to comment.