From 6e03a2cd53d4e542356ebd507b263380c34f2b3a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Olivier=20M=C3=A9lois?= Date: Mon, 24 Apr 2023 13:32:34 +0200 Subject: [PATCH] Attempt to have fatal errors lead to the test process ending --- .../weaver/CatsUnsafeRunPlatformCompat.scala | 6 +++++- .../weaver/CatsUnsafeRunPlatformCompat.scala | 17 +++++++++++++++-- .../core/cats/src/weaver/CatsUnsafeRun.scala | 6 ++---- modules/core/src/weaver/UnsafeRun.scala | 3 ++- modules/framework/src-jvm/RunnerCompat.scala | 7 +++---- 5 files changed, 27 insertions(+), 12 deletions(-) diff --git a/modules/core/cats/src-js/weaver/CatsUnsafeRunPlatformCompat.scala b/modules/core/cats/src-js/weaver/CatsUnsafeRunPlatformCompat.scala index 4275e7de..0a3a323e 100644 --- a/modules/core/cats/src-js/weaver/CatsUnsafeRunPlatformCompat.scala +++ b/modules/core/cats/src-js/weaver/CatsUnsafeRunPlatformCompat.scala @@ -1,12 +1,16 @@ package weaver import cats.effect.IO +import java.util.concurrent.atomic.AtomicBoolean private[weaver] trait CatsUnsafeRunPlatformCompat { self: CatsUnsafeRun => def unsafeRunSync(task: IO[Unit]): Unit = ??? - def background(task: IO[Unit]): CancelToken = ??? + def background(task: IO[Unit], atomicBoolean: AtomicBoolean): CancelToken = + ??? + + def cancel(token: CancelToken): Unit = ??? } diff --git a/modules/core/cats/src-jvm/weaver/CatsUnsafeRunPlatformCompat.scala b/modules/core/cats/src-jvm/weaver/CatsUnsafeRunPlatformCompat.scala index 2c3ad99d..86f09568 100644 --- a/modules/core/cats/src-jvm/weaver/CatsUnsafeRunPlatformCompat.scala +++ b/modules/core/cats/src-jvm/weaver/CatsUnsafeRunPlatformCompat.scala @@ -2,12 +2,25 @@ package weaver import cats.effect.IO import cats.effect.unsafe.implicits.global +import scala.concurrent.duration._ +import scala.util.Failure +import scala.util.Success +import java.util.concurrent.atomic.AtomicBoolean private[weaver] trait CatsUnsafeRunPlatformCompat { self: CatsUnsafeRun => def unsafeRunSync(task: IO[Unit]): Unit = task.unsafeRunSync() - def background(task: IO[Unit]): CancelToken = - task.start.unsafeRunSync() + def cancel(token: CancelToken): Unit = + scala.concurrent.Await.result(token(), 10.seconds) + + def background(task: IO[Unit], isDone: AtomicBoolean): CancelToken = { + val (future, cancelToken) = task.unsafeToFutureCancelable() + future.onComplete { + case Failure(_) => isDone.set(true) + case Success(_) => () + }(scala.concurrent.ExecutionContext.global) + cancelToken + } } diff --git a/modules/core/cats/src/weaver/CatsUnsafeRun.scala b/modules/core/cats/src/weaver/CatsUnsafeRun.scala index 72b67a3d..fa3aecb4 100644 --- a/modules/core/cats/src/weaver/CatsUnsafeRun.scala +++ b/modules/core/cats/src/weaver/CatsUnsafeRun.scala @@ -3,19 +3,17 @@ package weaver import scala.concurrent.Future import cats.effect.unsafe.implicits.global -import cats.effect.{ FiberIO, IO } +import cats.effect.IO object CatsUnsafeRun extends CatsUnsafeRun trait CatsUnsafeRun extends UnsafeRun[IO] with CatsUnsafeRunPlatformCompat { - type CancelToken = FiberIO[Unit] + type CancelToken = () => Future[Unit] override implicit val parallel = IO.parallelForIO override implicit val effect = IO.asyncForIO - def cancel(token: CancelToken): Unit = unsafeRunSync(token.cancel) - def unsafeRunAndForget(task: IO[Unit]): Unit = task.unsafeRunAndForget() def unsafeRunToFuture(task: IO[Unit]): Future[Unit] = task.unsafeToFuture() diff --git a/modules/core/src/weaver/UnsafeRun.scala b/modules/core/src/weaver/UnsafeRun.scala index 56767644..155557aa 100644 --- a/modules/core/src/weaver/UnsafeRun.scala +++ b/modules/core/src/weaver/UnsafeRun.scala @@ -6,6 +6,7 @@ import scala.concurrent.duration.FiniteDuration import cats.Parallel import cats.effect.{ Async, Resource } import cats.syntax.all._ +import java.util.concurrent.atomic.AtomicBoolean trait EffectCompat[F[_]] { implicit def parallel: Parallel[F] @@ -35,7 +36,7 @@ trait UnsafeRun[F[_]] extends EffectCompat[F] { type CancelToken - def background(task: F[Unit]): CancelToken + def background(task: F[Unit], isDone: AtomicBoolean): CancelToken def cancel(token: CancelToken): Unit def unsafeRunSync(task: F[Unit]): Unit diff --git a/modules/framework/src-jvm/RunnerCompat.scala b/modules/framework/src-jvm/RunnerCompat.scala index 8494d466..a89e81dd 100644 --- a/modules/framework/src-jvm/RunnerCompat.scala +++ b/modules/framework/src-jvm/RunnerCompat.scala @@ -47,10 +47,9 @@ trait RunnerCompat[F[_]] { self: sbt.testing.Runner => waitForResourcesShutdown: java.util.concurrent.Semaphore, tasks: List[IOTask], gate: Promise[Unit]): Unit = { - cancelToken = Some(unsafeRun.background(run(globalResources, - waitForResourcesShutdown, - tasks, - gate))) + cancelToken = Some(unsafeRun.background( + run(globalResources, waitForResourcesShutdown, tasks, gate), + isDone)) } def tasks(taskDefs: Array[TaskDef]): Array[Task] = {