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

Move scalanative.runtime.loop() invocation to correct place #700

Merged
merged 2 commits into from
Sep 15, 2023
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
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)
}
Comment on lines +16 to +19
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.


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)
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

JS uses runAsync here but actually we want to use run (sync) on Native.

}
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
}

}