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

Attempt to have fatal errors lead to the test process ending #653

Draft
wants to merge 1 commit into
base: main
Choose a base branch
from
Draft
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
@@ -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 = ???

}
17 changes: 15 additions & 2 deletions modules/core/cats/src-jvm/weaver/CatsUnsafeRunPlatformCompat.scala
Original file line number Diff line number Diff line change
Expand Up @@ -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
}

}
6 changes: 2 additions & 4 deletions modules/core/cats/src/weaver/CatsUnsafeRun.scala
Original file line number Diff line number Diff line change
Expand Up @@ -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()

Expand Down
3 changes: 2 additions & 1 deletion modules/core/src/weaver/UnsafeRun.scala
Original file line number Diff line number Diff line change
Expand Up @@ -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]
Expand Down Expand Up @@ -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
Expand Down
7 changes: 3 additions & 4 deletions modules/framework/src-jvm/RunnerCompat.scala
Original file line number Diff line number Diff line change
Expand Up @@ -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] = {
Expand Down