Skip to content

Commit 8e59882

Browse files
committed
tweak Specs2 CatsResource to rethrow any exceptions raised during resource acquisition
1 parent e741496 commit 8e59882

File tree

1 file changed

+5
-5
lines changed

1 file changed

+5
-5
lines changed

specs2/shared/src/main/scala/cats/effect/testing/specs2/CatsResource.scala

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -17,9 +17,9 @@
1717
package cats.effect.testing
1818
package specs2
1919

20-
import cats.effect.{Async, Deferred, Resource, Spawn, Sync}
20+
import cats.effect.syntax.all._
21+
import cats.effect._
2122
import cats.syntax.all._
22-
2323
import org.specs2.specification.BeforeAfterAll
2424

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

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

52-
pair <- resource.allocated
52+
pair <- resource.attempt.allocated
5353
(a, shutdownAction) = pair
5454

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

8080
// specs2's runtime should prevent this case
8181
case None =>

0 commit comments

Comments
 (0)