Skip to content

Commit

Permalink
Non-terminating cancel if acquire fails (#112)
Browse files Browse the repository at this point in the history
* Non-terminating cancel if acquire fails

Port of monix/monix#1175

* Non-terminating terminate if acquire fails

Port of monix/monix#1175

* Test for terminate in acquire
  • Loading branch information
CucumisSativus authored May 10, 2020
1 parent 9a9148b commit 1aff73a
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -205,10 +205,13 @@ private[monix] object TaskBracket {
}
}

def onError(ex: E): Unit =
def onError(ex: E): Unit = {
deferredRelease.complete(BIO.unit)(ctx.scheduler)
cb.onError(ex)
}

override def onTermination(e: Throwable): Unit = {
deferredRelease.complete(BIO.unit)(ctx.scheduler)
cb.onTermination(e)
}
}
Expand Down
27 changes: 27 additions & 0 deletions core/shared/src/test/scala/monix/bio/TaskBracketSuite.scala
Original file line number Diff line number Diff line change
Expand Up @@ -396,4 +396,31 @@ object TaskBracketSuite extends BaseTestSuite {
assertEquals(f.value, Some(Success(())))
}

test("bracket can be canceled while failing to acquire") { implicit sc =>

val task = (BIO.sleep(2.second) >> BIO.raiseError[Unit](DummyException("BOOM")))
.bracket(_ => Task.unit)(_ => BIO.unit)

val cancelToken = task.runAsyncF(_ => ())

sc.tick(1.second)
val f = cancelToken.runToFuture

sc.tick(1.second)
assertEquals(f.value, Some(Success(())))
}

test("bracket can be canceled while terminating") { implicit sc =>

val task = (BIO.sleep(2.second) >> BIO.terminate(DummyException("BOOM")))
.bracket(_ => Task.unit)(_ => BIO.unit)

val cancelToken = task.runAsyncF(_ => ())

sc.tick(1.second)
val f = cancelToken.runToFuture

sc.tick(1.second)
assertEquals(f.value, Some(Success(())))
}
}

0 comments on commit 1aff73a

Please sign in to comment.