diff --git a/build.sbt b/build.sbt index 3586e3d..af02b3c 100644 --- a/build.sbt +++ b/build.sbt @@ -155,5 +155,5 @@ lazy val webworker = project ), (Test / test) := (Test / test).dependsOn(Compile / fastOptJS).value, buildInfoKeys := Seq[BuildInfoKey](scalaVersion, baseDirectory), - buildInfoPackage := "org.scalajs") + buildInfoPackage := "org.scalajs.macrotaskexecutor") .enablePlugins(ScalaJSPlugin, BuildInfoPlugin, NoPublishPlugin) diff --git a/webworker/src/main/scala/org/scalajs/macrotaskexecutor/WebworkerSuiteRunner.scala b/webworker/src/main/scala/java/io/File.scala similarity index 84% rename from webworker/src/main/scala/org/scalajs/macrotaskexecutor/WebworkerSuiteRunner.scala rename to webworker/src/main/scala/java/io/File.scala index 337d6b6..1126746 100644 --- a/webworker/src/main/scala/org/scalajs/macrotaskexecutor/WebworkerSuiteRunner.scala +++ b/webworker/src/main/scala/java/io/File.scala @@ -14,8 +14,9 @@ * limitations under the License. */ -package org.scalajs.macrotaskexecutor +package java.io -object WebworkerSuiteRunner { - def main(args: Array[String]): Unit = () +// hack hack buildinfo hack +class File(path: String) { + override def toString() = path } diff --git a/webworker/src/main/scala/org/scalajs/macrotaskexecutor/MacrotaskExecutorSuiteRunner.scala b/webworker/src/main/scala/org/scalajs/macrotaskexecutor/MacrotaskExecutorSuiteRunner.scala new file mode 100644 index 0000000..4c96c4f --- /dev/null +++ b/webworker/src/main/scala/org/scalajs/macrotaskexecutor/MacrotaskExecutorSuiteRunner.scala @@ -0,0 +1,51 @@ +/* + * Copyright 2021 Scala.js (https://www.scala-js.org/) + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package org.scalajs.macrotaskexecutor + +import munit.MUnitRunner +import org.junit.runner.Description +import org.junit.runner.notification.Failure +import org.junit.runner.notification.RunNotifier +import org.scalajs.dom.webworkers.DedicatedWorkerGlobalScope + +import scala.scalajs.js +import scala.util + +object MacrotaskExecutorSuiteRunner { + + import MacrotaskExecutor.Implicits._ + + def postMessage(msg: js.Any): Unit = + DedicatedWorkerGlobalScope.self.postMessage(msg) + + def main(args: Array[String]): Unit = + new MUnitRunner( + classOf[MacrotaskExecutorSuite], + () => new MacrotaskExecutorSuite + ).runAsync(new RunNotifier { + def fireTestStarted(description: Description): Unit = () + def fireTestSuiteStarted(description: Description): Unit = () + def fireTestSuiteFinished(description: Description): Unit = () + def fireTestIgnored(description: Description): Unit = () + def fireTestFinished(description: Description): Unit = () + def fireTestFailure(failure: Failure): Unit = postMessage(false) + def fireTestAssumptionFailed(failure: Failure): Unit = postMessage(false) + }).onComplete { + case util.Success(_) => postMessage(true) + case util.Failure(_) => postMessage(false) + } +} diff --git a/webworker/src/test/scala/org/scalajs/macrotaskexecutor/WebWorkerMacrotaskSuite.scala b/webworker/src/test/scala/org/scalajs/macrotaskexecutor/WebWorkerMacrotaskSuite.scala new file mode 100644 index 0000000..db87b98 --- /dev/null +++ b/webworker/src/test/scala/org/scalajs/macrotaskexecutor/WebWorkerMacrotaskSuite.scala @@ -0,0 +1,57 @@ +/* + * Copyright 2021 Scala.js (https://www.scala-js.org/) + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package org.scalajs.macrotaskexecutor + +import munit.FunSuite +import org.scalajs.dom.webworkers.Worker + +import scala.concurrent.Promise +import scala.scalajs.js +import scala.util.Try + +class WebWorkerMacrotaskSuite extends FunSuite { + + import MacrotaskExecutor.Implicits._ + + def scalaVersion = if (BuildInfo.scalaVersion.startsWith("2")) + BuildInfo.scalaVersion.split("\\.").init.mkString(".") + else + BuildInfo.scalaVersion + + def targetDir = s"${BuildInfo.baseDirectory}/target/scala-${scalaVersion}" + + Try(js.isUndefined(js.Dynamic.global.window.Worker)).toOption + .filterNot(identity) + .foreach { _ => + test("macrotask executor should pass the suite on a webworker") { + val p = Promise[Boolean]() + + val worker = new Worker(s"file://${targetDir}/scala-js-macrotask-executor-webworker-fastopt/main.js") + + worker.onmessage = { event => + event.data match { + case log: String => println(log) + case success: Boolean => p.success(success) + case _ => () + } + } + + p.future.map(assert(_)) + + } + } +}