Skip to content

Commit

Permalink
Merge pull request #608 from Dwolla/resource-acquisition-exception
Browse files Browse the repository at this point in the history
Tweak Specs2 CatsResource to rethrow exceptions raised during resource acquisition
  • Loading branch information
djspiewak authored Nov 27, 2024
2 parents 3e85bd5 + 4a70db2 commit 1c903c0
Show file tree
Hide file tree
Showing 2 changed files with 48 additions and 5 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,9 @@
package cats.effect.testing
package specs2

import cats.effect.{Async, Deferred, Resource, Spawn, Sync}
import cats.effect._
import cats.effect.syntax.all._
import cats.syntax.all._

import org.specs2.specification.BeforeAfterAll

import scala.concurrent.duration._
Expand All @@ -39,7 +39,7 @@ abstract class CatsResource[F[_]: Async: UnsafeRun, A] extends BeforeAfterAll wi
// but it does work on scalajs
@volatile
private var gate: Option[Deferred[F, Unit]] = None
private var value: Option[A] = None
private var value: Option[Either[Throwable, A]] = None
private var shutdown: F[Unit] = ().pure[F]

override def beforeAll(): Unit = {
Expand All @@ -49,7 +49,7 @@ abstract class CatsResource[F[_]: Async: UnsafeRun, A] extends BeforeAfterAll wi
gate = Some(d)
}

pair <- resource.allocated
pair <- resource.attempt.allocated
(a, shutdownAction) = pair

_ <- Sync[F] delay {
Expand All @@ -75,7 +75,7 @@ abstract class CatsResource[F[_]: Async: UnsafeRun, A] extends BeforeAfterAll wi
def withResource[R](f: A => F[R]): F[R] =
gate match {
case Some(g) =>
g.get *> Sync[F].delay(value.get).flatMap(f)
finiteResourceTimeout.foldl(g.get)(_.timeout(_)) *> Sync[F].delay(value.get).rethrow.flatMap(f)

// specs2's runtime should prevent this case
case None =>
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
/*
* Copyright 2020 Typelevel
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package cats.effect.testing.specs2

import cats.effect.{IO, Resource}
import org.specs2.execute.Result
import org.specs2.mutable.SpecificationLike

case class BlowUpResourceException() extends RuntimeException("boom")

class CatsResourceErrorSpecs
extends CatsResource[IO, Unit]
with SpecificationLike {

private val expectedException = BlowUpResourceException()

val resource: Resource[IO, Unit] =
Resource.eval(IO.raiseError(expectedException))

"cats resource support" should {
"report failure when the resource acquisition fails" in withResource[Result] { _ =>
IO(failure("we shouldn't get here if an exception was raised"))
}
.recover[Result] {
case ex: RuntimeException =>
ex must beEqualTo(expectedException)
}
}
}

0 comments on commit 1c903c0

Please sign in to comment.