From f9a3d92a676396e392dc75a8448b0445fa58daf0 Mon Sep 17 00:00:00 2001 From: Arman Bilge Date: Mon, 6 Sep 2021 21:38:55 +0000 Subject: [PATCH 1/4] Add webworker test --- build.sbt | 2 +- .../io/File.scala} | 7 ++- .../MacrotaskExecutorSuiteRunner.scala | 52 +++++++++++++++++ .../WebWorkerMacrotaskSuite.scala | 57 +++++++++++++++++++ 4 files changed, 114 insertions(+), 4 deletions(-) rename webworker/src/main/scala/{org/scalajs/macrotaskexecutor/WebworkerSuiteRunner.scala => java/io/File.scala} (84%) create mode 100644 webworker/src/main/scala/org/scalajs/macrotaskexecutor/MacrotaskExecutorSuiteRunner.scala create mode 100644 webworker/src/test/scala/org/scalajs/macrotaskexecutor/WebWorkerMacrotaskSuite.scala 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..687b270 --- /dev/null +++ b/webworker/src/main/scala/org/scalajs/macrotaskexecutor/MacrotaskExecutorSuiteRunner.scala @@ -0,0 +1,52 @@ +/* + * 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.scalajs.js.annotation.JSGlobal +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 = () + def fireTestAssumptionFailed(failure: Failure): Unit = () + }).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(_)) + + } + } +} From a26bc0a83a0b9c696ca508db1104d6fe5429aff3 Mon Sep 17 00:00:00 2001 From: Arman Bilge Date: Mon, 6 Sep 2021 21:42:37 +0000 Subject: [PATCH 2/4] Fix compile --- .../scalajs/macrotaskexecutor/MacrotaskExecutorSuiteRunner.scala | 1 - 1 file changed, 1 deletion(-) diff --git a/webworker/src/main/scala/org/scalajs/macrotaskexecutor/MacrotaskExecutorSuiteRunner.scala b/webworker/src/main/scala/org/scalajs/macrotaskexecutor/MacrotaskExecutorSuiteRunner.scala index 687b270..70b3d6a 100644 --- a/webworker/src/main/scala/org/scalajs/macrotaskexecutor/MacrotaskExecutorSuiteRunner.scala +++ b/webworker/src/main/scala/org/scalajs/macrotaskexecutor/MacrotaskExecutorSuiteRunner.scala @@ -23,7 +23,6 @@ import org.junit.runner.notification.RunNotifier import org.scalajs.dom.webworkers.DedicatedWorkerGlobalScope import scala.scalajs.js -import scala.scalajs.js.annotation.JSGlobal import scala.util object MacrotaskExecutorSuiteRunner { From 5bec778864c21402ef8c4b44eefdcc56d9b890e8 Mon Sep 17 00:00:00 2001 From: Arman Bilge Date: Mon, 6 Sep 2021 21:52:56 +0000 Subject: [PATCH 3/4] Correctly detect failure --- .../macrotaskexecutor/MacrotaskExecutorSuiteRunner.scala | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/webworker/src/main/scala/org/scalajs/macrotaskexecutor/MacrotaskExecutorSuiteRunner.scala b/webworker/src/main/scala/org/scalajs/macrotaskexecutor/MacrotaskExecutorSuiteRunner.scala index 70b3d6a..4a4d6c5 100644 --- a/webworker/src/main/scala/org/scalajs/macrotaskexecutor/MacrotaskExecutorSuiteRunner.scala +++ b/webworker/src/main/scala/org/scalajs/macrotaskexecutor/MacrotaskExecutorSuiteRunner.scala @@ -42,7 +42,7 @@ object MacrotaskExecutorSuiteRunner { def fireTestSuiteFinished(description: Description): Unit = () def fireTestIgnored(description: Description): Unit = () def fireTestFinished(description: Description): Unit = () - def fireTestFailure(failure: Failure): Unit = () + def fireTestFailure(failure: Failure): Unit = postMessage(false) def fireTestAssumptionFailed(failure: Failure): Unit = () }).onComplete { case util.Success(_) => postMessage(true) From c96fd16eef74db24ca78ac7e1c7bc8a0e06ef9e1 Mon Sep 17 00:00:00 2001 From: Arman Bilge Date: Mon, 6 Sep 2021 21:54:56 +0000 Subject: [PATCH 4/4] Once more fix for errors --- .../macrotaskexecutor/MacrotaskExecutorSuiteRunner.scala | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/webworker/src/main/scala/org/scalajs/macrotaskexecutor/MacrotaskExecutorSuiteRunner.scala b/webworker/src/main/scala/org/scalajs/macrotaskexecutor/MacrotaskExecutorSuiteRunner.scala index 4a4d6c5..4c96c4f 100644 --- a/webworker/src/main/scala/org/scalajs/macrotaskexecutor/MacrotaskExecutorSuiteRunner.scala +++ b/webworker/src/main/scala/org/scalajs/macrotaskexecutor/MacrotaskExecutorSuiteRunner.scala @@ -43,7 +43,7 @@ object MacrotaskExecutorSuiteRunner { def fireTestIgnored(description: Description): Unit = () def fireTestFinished(description: Description): Unit = () def fireTestFailure(failure: Failure): Unit = postMessage(false) - def fireTestAssumptionFailed(failure: Failure): Unit = () + def fireTestAssumptionFailed(failure: Failure): Unit = postMessage(false) }).onComplete { case util.Success(_) => postMessage(true) case util.Failure(_) => postMessage(false)