Skip to content

Add webworker test #12

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

Merged
merged 4 commits into from
Sep 6, 2021
Merged
Show file tree
Hide file tree
Changes from 2 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
2 changes: 1 addition & 1 deletion build.sbt
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Original file line number Diff line number Diff line change
@@ -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 = ()
def fireTestAssumptionFailed(failure: Failure): Unit = ()
}).onComplete {
case util.Success(_) => postMessage(true)
case util.Failure(_) => postMessage(false)
}
}
Original file line number Diff line number Diff line change
@@ -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(_))

}
}
}