Skip to content

Commit 1c903c0

Browse files
authored
Merge pull request #608 from Dwolla/resource-acquisition-exception
Tweak Specs2 CatsResource to rethrow exceptions raised during resource acquisition
2 parents 3e85bd5 + 4a70db2 commit 1c903c0

File tree

2 files changed

+48
-5
lines changed

2 files changed

+48
-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._
21+
import cats.effect.syntax.all._
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 =>
Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
/*
2+
* Copyright 2020 Typelevel
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
package cats.effect.testing.specs2
18+
19+
import cats.effect.{IO, Resource}
20+
import org.specs2.execute.Result
21+
import org.specs2.mutable.SpecificationLike
22+
23+
case class BlowUpResourceException() extends RuntimeException("boom")
24+
25+
class CatsResourceErrorSpecs
26+
extends CatsResource[IO, Unit]
27+
with SpecificationLike {
28+
29+
private val expectedException = BlowUpResourceException()
30+
31+
val resource: Resource[IO, Unit] =
32+
Resource.eval(IO.raiseError(expectedException))
33+
34+
"cats resource support" should {
35+
"report failure when the resource acquisition fails" in withResource[Result] { _ =>
36+
IO(failure("we shouldn't get here if an exception was raised"))
37+
}
38+
.recover[Result] {
39+
case ex: RuntimeException =>
40+
ex must beEqualTo(expectedException)
41+
}
42+
}
43+
}

0 commit comments

Comments
 (0)