1717package cats .effect .testing
1818package specs2
1919
20- import cats .effect .{Async , Deferred , Resource , Spawn , Sync }
20+ import cats .effect .syntax .all ._
21+ import cats .effect ._
2122import cats .syntax .all ._
22-
2323import org .specs2 .specification .BeforeAfterAll
2424
2525import 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