Skip to content

Commit

Permalink
Merge pull request #700 from armanbilge/issue/695
Browse files Browse the repository at this point in the history
Move `scalanative.runtime.loop()` invocation to correct place
  • Loading branch information
valencik authored Sep 15, 2023
2 parents f755d89 + 817a324 commit bc67c6c
Show file tree
Hide file tree
Showing 7 changed files with 82 additions and 5 deletions.
6 changes: 6 additions & 0 deletions munit/js/src/main/scala/munit/internal/PlatformCompat.scala
Original file line number Diff line number Diff line change
Expand Up @@ -9,11 +9,17 @@ import sbt.testing.EventHandler
import sbt.testing.Logger
import scala.concurrent.Promise
import scala.concurrent.duration.Duration
import scala.concurrent.Await
import scala.concurrent.Awaitable
import scala.concurrent.ExecutionContext
import scala.scalajs.js.timers
import java.util.concurrent.TimeoutException

object PlatformCompat {
def awaitResult[T](awaitable: Awaitable[T]): T = {
Await.result(awaitable, Duration.Inf)
}

def executeAsync(
task: Task,
eventHandler: EventHandler,
Expand Down
7 changes: 7 additions & 0 deletions munit/jvm/src/main/scala/munit/internal/PlatformCompat.scala
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@ import java.util.concurrent.{
TimeUnit,
TimeoutException
}
import scala.concurrent.Await
import scala.concurrent.Awaitable
import scala.concurrent.Promise
import scala.concurrent.ExecutionContext
import java.util.concurrent.atomic.AtomicInteger
Expand All @@ -28,6 +30,11 @@ object PlatformCompat {
}
}
)

def awaitResult[T](awaitable: Awaitable[T]): T = {
Await.result(awaitable, Duration.Inf)
}

def executeAsync(
task: Task,
eventHandler: EventHandler,
Expand Down
11 changes: 8 additions & 3 deletions munit/native/src/main/scala/munit/internal/PlatformCompat.scala
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,17 @@ import scala.scalanative.reflect.Reflect
import sbt.testing.Task
import sbt.testing.EventHandler
import sbt.testing.Logger
import scala.concurrent.Await
import scala.concurrent.Awaitable
import scala.concurrent.duration.Duration
import scala.concurrent.ExecutionContext

object PlatformCompat {
def awaitResult[T](awaitable: Awaitable[T]): T = {
scalanative.runtime.loop()
Await.result(awaitable, Duration.Inf)
}

def executeAsync(
task: Task,
eventHandler: EventHandler,
Expand All @@ -24,9 +31,7 @@ object PlatformCompat {
duration: Duration,
ec: ExecutionContext
): Future[T] = {
val f = startFuture()
scala.scalanative.runtime.loop()
f
startFuture()
}
def setTimeout(ms: Int)(body: => Unit): () => Unit = {
Thread.sleep(ms)
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
/*
* Adapted from https://github.com/scala-js/scala-js, see NOTICE.md.
*/

package munit.internal.junitinterface

import munit.internal.PlatformCompat
import org.junit.runner.notification.RunNotifier
import sbt.testing._
import scala.concurrent.ExecutionContext.Implicits.global

/* Implementation note: In JUnitTask we use Future[Try[Unit]] instead of simply
* Future[Unit]. This is to prevent Scala's Future implementation to box/wrap
* fatal errors (most importantly AssertionError) in ExecutionExceptions. We
* need to prevent the wrapping in order to hide the fact that we use async
* under the hood and stay consistent with JVM JUnit.
*/
final class JUnitTask(
_taskDef: TaskDef,
runSettings: RunSettings,
classLoader: ClassLoader
) extends Task {

override def taskDef(): TaskDef = _taskDef
override def tags(): Array[String] = Array.empty

def execute(
eventHandler: EventHandler,
loggers: Array[Logger]
): Array[Task] = {
PlatformCompat.newRunner(taskDef(), classLoader) match {
case None =>
case Some(runner) =>
runner.filter(runSettings.tags)
val reporter =
new JUnitReporter(eventHandler, loggers, runSettings, taskDef())
val notifier: RunNotifier = new MUnitRunNotifier(reporter)
runner.run(notifier)
}
Array()
}

}
3 changes: 1 addition & 2 deletions munit/shared/src/main/scala/munit/MUnitRunner.scala
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@ import org.junit.runner.notification.RunNotifier

import java.lang.reflect.Modifier
import scala.collection.mutable
import scala.concurrent.Await
import scala.concurrent.ExecutionContext
import scala.concurrent.Future
import scala.concurrent.duration.Duration
Expand Down Expand Up @@ -113,7 +112,7 @@ class MUnitRunner(val cls: Class[_ <: Suite], newInstance: () => Suite)
}

override def run(notifier: RunNotifier): Unit = {
Await.result(runAsync(notifier), Duration.Inf)
PlatformCompat.awaitResult(runAsync(notifier))
}
def runAsync(notifier: RunNotifier): Future[Unit] = {
val description = getDescription()
Expand Down
17 changes: 17 additions & 0 deletions tests/native/src/test/scala/munit/Issue695Suite.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
package munit

import scala.concurrent._

class Issue695Suite extends FunSuite {
override def munitExecutionContext = ExecutionContext.global

test("await task on global EC") {
val p = Promise[Unit]()
ExecutionContext.global.execute { () =>
Thread.sleep(1000)
p.success(())
}
p.future
}

}

0 comments on commit bc67c6c

Please sign in to comment.