Skip to content

Port tests to JUnit #54

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 1 commit into from
Jun 5, 2022
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
30 changes: 19 additions & 11 deletions build.sbt
Original file line number Diff line number Diff line change
Expand Up @@ -24,9 +24,6 @@ import org.scalajs.jsenv.selenium.SeleniumJSEnv

import java.util.concurrent.TimeUnit

val MUnitFramework = new TestFramework("munit.Framework")
val MUnitVersion = "0.7.29"

ThisBuild / baseVersion := "1.0"

ThisBuild / organization := "org.scala-js"
Expand Down Expand Up @@ -136,7 +133,7 @@ ThisBuild / Test / jsEnv := {
}
}

ThisBuild / Test / testOptions += Tests.Argument(MUnitFramework, "+l")
ThisBuild / testOptions += Tests.Argument(TestFrameworks.JUnit, "-a", "-s", "-v")

// project structure

Expand All @@ -148,9 +145,8 @@ lazy val core = project
.in(file("core"))
.settings(
name := "scala-js-macrotask-executor",
libraryDependencies += "org.scalameta" %%% "munit" % MUnitVersion % Test,
)
.enablePlugins(ScalaJSPlugin)
.enablePlugins(ScalaJSPlugin, ScalaJSJUnitPlugin)

// this project solely exists for testing purposes
lazy val webworker = project
Expand All @@ -161,9 +157,21 @@ lazy val webworker = project
scalaJSUseMainModuleInitializer := true,
libraryDependencies ++= Seq(
"org.scala-js" %%% "scalajs-dom" % "2.0.0",
"org.scalameta" %%% "munit" % MUnitVersion % Test,
),
(Test / test) := (Test / test).dependsOn(Compile / fastOptJS).value,
buildInfoKeys := Seq(scalaVersion, baseDirectory, BuildInfoKey("isBrowser" -> useJSEnv.value.isBrowser)),
buildInfoPackage := "org.scalajs.macrotaskexecutor")
.enablePlugins(ScalaJSPlugin, BuildInfoPlugin, NoPublishPlugin)
(Test / test) := {
if (useJSEnv.value.isBrowser)
(Test / test).dependsOn(Compile / fastOptJS).value
else
()
},
buildInfoKeys := Seq(
BuildInfoKey(
"workerDir" -> {
val outputDir = (Compile / fastLinkJS / scalaJSLinkerOutputDirectory).value
outputDir.getAbsolutePath()
}
)
),
buildInfoPackage := "org.scalajs.macrotaskexecutor",
)
.enablePlugins(ScalaJSPlugin, ScalaJSJUnitPlugin, BuildInfoPlugin, NoPublishPlugin)
Original file line number Diff line number Diff line change
Expand Up @@ -16,16 +16,18 @@

package org.scalajs.macrotaskexecutor

import munit.FunSuite
import org.junit.Test

import scala.concurrent.Future
import scala.concurrent.duration._
import scala.scalajs.js
import scala.util.Try

class MacrotaskExecutorSuite extends FunSuite {
class MacrotaskExecutorTests {
import MacrotaskExecutor.Implicits._

test("sequence a series of 10,000 recursive executions without clamping") {
@Test
def `sequence a series of 10,000 recursive executions without clamping` = {
def loop(n: Int): Future[Int] =
if (n <= 0)
Future(0)
Expand All @@ -39,20 +41,23 @@ class MacrotaskExecutorSuite extends FunSuite {
Future {
val end = System.currentTimeMillis()

assert(res == 10000)
assert((end - start).toDouble / MinimumClamp < 0.25) // we should beat the clamping by at least 4x even on slow environments
Try {
assert(res == 10000)
assert((end - start).toDouble / MinimumClamp < 0.25) // we should beat the clamping by at least 4x even on slow environments
}
}
}
}

// this test fails to terminate with a Promise-based executor
test("preserve fairness with setTimeout") {
@Test
def `preserve fairness with setTimeout` = {
var cancel = false

def loop(): Future[Unit] =
def loop(): Future[Try[Unit]] =
Future(cancel) flatMap { canceled =>
if (canceled)
Future.successful(())
Future.successful(Try(()))
else
loop()
}
Expand All @@ -64,12 +69,13 @@ class MacrotaskExecutorSuite extends FunSuite {
loop()
}

test("execute a bunch of stuff in 'parallel' and ensure it all runs") {
@Test
def `execute a bunch of stuff in 'parallel' and ensure it all runs` = {
var i = 0

Future.sequence(List.fill(10000)(Future { i += 1 })) flatMap { _ =>
Future {
assert(i == 10000)
Try(assert(i == 10000))
}
}
}
Expand Down
22 changes: 0 additions & 22 deletions webworker/src/main/scala/java/io/File.scala

This file was deleted.

This file was deleted.

Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
/*
* 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 org.scalajs.dom.DedicatedWorkerGlobalScope

import scala.scalajs.concurrent.QueueExecutionContext.timeouts
import scala.scalajs.js

object MacrotaskExecutorTestsRunner {

def main(args: Array[String]): Unit = {
val tests = new MacrotaskExecutorTests

implicit val ec = timeouts()

for {
clamping <- tests.`sequence a series of 10,000 recursive executions without clamping`
fairness <- tests.`preserve fairness with setTimeout`
parallel <- tests.`execute a bunch of stuff in 'parallel' and ensure it all runs`
} yield DedicatedWorkerGlobalScope.self.postMessage(js.Dictionary(
"clamping" -> clamping.isSuccess,
"fairness" -> fairness.isSuccess,
"parallel" -> parallel.isSuccess
))

()
}
}

This file was deleted.

Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
/*
* 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 org.junit.Test
import org.scalajs.dom.MessageEvent
import org.scalajs.dom.Worker

import scala.concurrent.ExecutionContext
import scala.concurrent.Future
import scala.concurrent.Promise
import scala.scalajs.concurrent.QueueExecutionContext.timeouts
import scala.scalajs.js
import scala.util.Failure
import scala.util.Success
import scala.util.Try

class WebWorkerMacrotaskTests {

implicit val ec: ExecutionContext = timeouts()

val worker = new Worker(s"file://${BuildInfo.workerDir}/main.js")

val testsResult = Promise[js.Dictionary[Boolean]]()
worker.onmessage = { (event: MessageEvent) =>
testsResult.success(event.data.asInstanceOf[js.Dictionary[Boolean]])
}

def getTestResult(id: String): Future[Try[Unit]] =
testsResult.future.map { results =>
if (results.getOrElse(id, false))
Success(())
else
Failure(new AssertionError)
}

@Test
def `sequence a series of 10,000 recursive executions without clamping` =
getTestResult("clamping")

@Test
def `preserve fairness with setTimeout` =
getTestResult("fairness")

@Test
def `execute a bunch of stuff in 'parallel' and ensure it all runs` =
getTestResult("parallel")

}