Skip to content

Commit

Permalink
Merge pull request #477 from lukestephenson/lstephenson/wait-for-reso…
Browse files Browse the repository at this point in the history
…urce-completion

Wait for resources release before ending tests
  • Loading branch information
Baccata authored Feb 16, 2022
2 parents 864a8f9 + e18af77 commit 5eda2f2
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 4 deletions.
24 changes: 20 additions & 4 deletions modules/framework/src-jvm/RunnerCompat.scala
Original file line number Diff line number Diff line change
Expand Up @@ -45,13 +45,18 @@ trait RunnerCompat[F[_]] { self: sbt.testing.Runner =>

private def runBackground(
globalResources: List[GlobalResourceF[F]],
waitForResourcesShutdown: java.util.concurrent.Semaphore,
tasks: List[IOTask],
gate: Promise[Unit]): Unit = {
cancelToken = Some(unsafeRun.background(run(globalResources, tasks, gate)))
cancelToken = Some(unsafeRun.background(run(globalResources,
waitForResourcesShutdown,
tasks,
gate)))
}

def tasks(taskDefs: Array[TaskDef]): Array[Task] = {
val stillRunning = new AtomicInteger(0)
val stillRunning = new AtomicInteger(0)
val waitForResourcesShutdown = new java.util.concurrent.Semaphore(0)

val tasksAndSuites = taskDefs.toList.map { taskDef =>
taskDef -> suiteLoader(taskDef)
Expand Down Expand Up @@ -86,6 +91,7 @@ trait RunnerCompat[F[_]] { self: sbt.testing.Runner =>
new SbtTask(taskDef,
isDone,
stillRunning,
waitForResourcesShutdown,
promise,
queue,
loggerPermit,
Expand All @@ -107,7 +113,10 @@ trait RunnerCompat[F[_]] { self: sbt.testing.Runner =>
// Passing a promise to the FP side that needs to be fulfilled
// when the global resources have been allocated.
val gate = Promise[Unit]()
runBackground(globalResources, ioTasks.toList, gate)
runBackground(globalResources,
waitForResourcesShutdown,
ioTasks.toList,
gate)
stillRunning.set(sbtTasks.size)

// Waiting for the resources to be allocated.
Expand All @@ -128,6 +137,7 @@ trait RunnerCompat[F[_]] { self: sbt.testing.Runner =>

private def run(
globalResources: List[GlobalResourceF[F]],
waitForResourcesShutdown: java.util.concurrent.Semaphore,
tasks: List[IOTask],
gate: Promise[Unit]): F[Unit] = {

Expand All @@ -140,7 +150,13 @@ trait RunnerCompat[F[_]] { self: sbt.testing.Runner =>
}
}

preventDeadlock(resourceMap(globalResources)).use { read =>
val cleanupResource = Resource.make(effect.unit)(_ =>
effect.delay(waitForResourcesShutdown.release()))

val globalResourcesWithCompletion =
cleanupResource.flatMap(_ => resourceMap(globalResources))

preventDeadlock(globalResourcesWithCompletion).use { read =>
for {
_ <- effect.delay(gate.success(()))
ref <- Ref.of[F, Chain[(SuiteName, TestOutcome)]](Chain.empty)
Expand Down
2 changes: 2 additions & 0 deletions modules/framework/src-jvm/SbtTask.scala
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ private[framework] class SbtTask(
val taskDef: TaskDef,
isDone: AtomicBoolean,
stillRunning: AtomicInteger,
waitForResourcesShutdown: java.util.concurrent.Semaphore,
start: scala.concurrent.Promise[Unit],
queue: java.util.concurrent.ConcurrentLinkedQueue[SuiteEvent],
loggerPermit: java.util.concurrent.Semaphore,
Expand All @@ -36,6 +37,7 @@ private[framework] class SbtTask(
case SuiteFinished(_) =>
finished = true
if (stillRunning.decrementAndGet == 0) {
waitForResourcesShutdown.acquire()
log(RunFinished(readFailed()))
}
case t @ TestFinished(outcome) =>
Expand Down

0 comments on commit 5eda2f2

Please sign in to comment.