Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Tweak Specs2 CatsResource to rethrow exceptions raised during resource acquisition #608

Merged
merged 2 commits into from
Nov 27, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
bpholt marked this conversation as resolved.
Show resolved Hide resolved
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
bpholt marked this conversation as resolved.
Show resolved Hide resolved
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)
}
}
}
Loading