Skip to content

Commit 56cf1ff

Browse files
authored
Merge pull request #12 from armanbilge/feature/webworker-tests
Add webworker test
2 parents 072a52b + c96fd16 commit 56cf1ff

File tree

4 files changed

+113
-4
lines changed

4 files changed

+113
-4
lines changed

Diff for: build.sbt

+1-1
Original file line numberDiff line numberDiff line change
@@ -155,5 +155,5 @@ lazy val webworker = project
155155
),
156156
(Test / test) := (Test / test).dependsOn(Compile / fastOptJS).value,
157157
buildInfoKeys := Seq[BuildInfoKey](scalaVersion, baseDirectory),
158-
buildInfoPackage := "org.scalajs")
158+
buildInfoPackage := "org.scalajs.macrotaskexecutor")
159159
.enablePlugins(ScalaJSPlugin, BuildInfoPlugin, NoPublishPlugin)

Diff for: webworker/src/main/scala/org/scalajs/macrotaskexecutor/WebworkerSuiteRunner.scala renamed to webworker/src/main/scala/java/io/File.scala

+4-3
Original file line numberDiff line numberDiff line change
@@ -14,8 +14,9 @@
1414
* limitations under the License.
1515
*/
1616

17-
package org.scalajs.macrotaskexecutor
17+
package java.io
1818

19-
object WebworkerSuiteRunner {
20-
def main(args: Array[String]): Unit = ()
19+
// hack hack buildinfo hack
20+
class File(path: String) {
21+
override def toString() = path
2122
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
/*
2+
* Copyright 2021 Scala.js (https://www.scala-js.org/)
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
package org.scalajs.macrotaskexecutor
18+
19+
import munit.MUnitRunner
20+
import org.junit.runner.Description
21+
import org.junit.runner.notification.Failure
22+
import org.junit.runner.notification.RunNotifier
23+
import org.scalajs.dom.webworkers.DedicatedWorkerGlobalScope
24+
25+
import scala.scalajs.js
26+
import scala.util
27+
28+
object MacrotaskExecutorSuiteRunner {
29+
30+
import MacrotaskExecutor.Implicits._
31+
32+
def postMessage(msg: js.Any): Unit =
33+
DedicatedWorkerGlobalScope.self.postMessage(msg)
34+
35+
def main(args: Array[String]): Unit =
36+
new MUnitRunner(
37+
classOf[MacrotaskExecutorSuite],
38+
() => new MacrotaskExecutorSuite
39+
).runAsync(new RunNotifier {
40+
def fireTestStarted(description: Description): Unit = ()
41+
def fireTestSuiteStarted(description: Description): Unit = ()
42+
def fireTestSuiteFinished(description: Description): Unit = ()
43+
def fireTestIgnored(description: Description): Unit = ()
44+
def fireTestFinished(description: Description): Unit = ()
45+
def fireTestFailure(failure: Failure): Unit = postMessage(false)
46+
def fireTestAssumptionFailed(failure: Failure): Unit = postMessage(false)
47+
}).onComplete {
48+
case util.Success(_) => postMessage(true)
49+
case util.Failure(_) => postMessage(false)
50+
}
51+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
/*
2+
* Copyright 2021 Scala.js (https://www.scala-js.org/)
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
package org.scalajs.macrotaskexecutor
18+
19+
import munit.FunSuite
20+
import org.scalajs.dom.webworkers.Worker
21+
22+
import scala.concurrent.Promise
23+
import scala.scalajs.js
24+
import scala.util.Try
25+
26+
class WebWorkerMacrotaskSuite extends FunSuite {
27+
28+
import MacrotaskExecutor.Implicits._
29+
30+
def scalaVersion = if (BuildInfo.scalaVersion.startsWith("2"))
31+
BuildInfo.scalaVersion.split("\\.").init.mkString(".")
32+
else
33+
BuildInfo.scalaVersion
34+
35+
def targetDir = s"${BuildInfo.baseDirectory}/target/scala-${scalaVersion}"
36+
37+
Try(js.isUndefined(js.Dynamic.global.window.Worker)).toOption
38+
.filterNot(identity)
39+
.foreach { _ =>
40+
test("macrotask executor should pass the suite on a webworker") {
41+
val p = Promise[Boolean]()
42+
43+
val worker = new Worker(s"file://${targetDir}/scala-js-macrotask-executor-webworker-fastopt/main.js")
44+
45+
worker.onmessage = { event =>
46+
event.data match {
47+
case log: String => println(log)
48+
case success: Boolean => p.success(success)
49+
case _ => ()
50+
}
51+
}
52+
53+
p.future.map(assert(_))
54+
55+
}
56+
}
57+
}

0 commit comments

Comments
 (0)