Skip to content

Commit e99fe19

Browse files
committed
Port tests to JUnit
1 parent 7733bff commit e99fe19

File tree

7 files changed

+141
-175
lines changed

7 files changed

+141
-175
lines changed

build.sbt

Lines changed: 19 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -24,9 +24,6 @@ import org.scalajs.jsenv.selenium.SeleniumJSEnv
2424

2525
import java.util.concurrent.TimeUnit
2626

27-
val MUnitFramework = new TestFramework("munit.Framework")
28-
val MUnitVersion = "0.7.29"
29-
3027
ThisBuild / baseVersion := "1.0"
3128

3229
ThisBuild / organization := "org.scala-js"
@@ -136,7 +133,7 @@ ThisBuild / Test / jsEnv := {
136133
}
137134
}
138135

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

141138
// project structure
142139

@@ -148,9 +145,8 @@ lazy val core = project
148145
.in(file("core"))
149146
.settings(
150147
name := "scala-js-macrotask-executor",
151-
libraryDependencies += "org.scalameta" %%% "munit" % MUnitVersion % Test,
152148
)
153-
.enablePlugins(ScalaJSPlugin)
149+
.enablePlugins(ScalaJSPlugin, ScalaJSJUnitPlugin)
154150

155151
// this project solely exists for testing purposes
156152
lazy val webworker = project
@@ -161,9 +157,21 @@ lazy val webworker = project
161157
scalaJSUseMainModuleInitializer := true,
162158
libraryDependencies ++= Seq(
163159
"org.scala-js" %%% "scalajs-dom" % "2.0.0",
164-
"org.scalameta" %%% "munit" % MUnitVersion % Test,
165160
),
166-
(Test / test) := (Test / test).dependsOn(Compile / fastOptJS).value,
167-
buildInfoKeys := Seq(scalaVersion, baseDirectory, BuildInfoKey("isBrowser" -> useJSEnv.value.isBrowser)),
168-
buildInfoPackage := "org.scalajs.macrotaskexecutor")
169-
.enablePlugins(ScalaJSPlugin, BuildInfoPlugin, NoPublishPlugin)
161+
(Test / test) := {
162+
if (useJSEnv.value.isBrowser)
163+
(Test / test).dependsOn(Compile / fastOptJS).value
164+
else
165+
()
166+
},
167+
buildInfoKeys := Seq(
168+
BuildInfoKey(
169+
"workerDir" -> {
170+
val outputDir = (Compile / fastLinkJS / scalaJSLinkerOutputDirectory).value
171+
outputDir.getAbsolutePath()
172+
}
173+
)
174+
),
175+
buildInfoPackage := "org.scalajs.macrotaskexecutor",
176+
)
177+
.enablePlugins(ScalaJSPlugin, ScalaJSJUnitPlugin, BuildInfoPlugin, NoPublishPlugin)

core/src/test/scala/org/scalajs/macrotaskexecutor/MacrotaskExecutorSuite.scala renamed to core/src/test/scala/org/scalajs/macrotaskexecutor/MacrotaskExecutorTests.scala

Lines changed: 16 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -16,16 +16,18 @@
1616

1717
package org.scalajs.macrotaskexecutor
1818

19-
import munit.FunSuite
19+
import org.junit.Test
2020

2121
import scala.concurrent.Future
2222
import scala.concurrent.duration._
2323
import scala.scalajs.js
24+
import scala.util.Try
2425

25-
class MacrotaskExecutorSuite extends FunSuite {
26+
class MacrotaskExecutorTests {
2627
import MacrotaskExecutor.Implicits._
2728

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

42-
assert(res == 10000)
43-
assert((end - start).toDouble / MinimumClamp < 0.25) // we should beat the clamping by at least 4x even on slow environments
44+
Try {
45+
assert(res == 10000)
46+
assert((end - start).toDouble / MinimumClamp < 0.25) // we should beat the clamping by at least 4x even on slow environments
47+
}
4448
}
4549
}
4650
}
4751

4852
// this test fails to terminate with a Promise-based executor
49-
test("preserve fairness with setTimeout") {
53+
@Test
54+
def `preserve fairness with setTimeout` = {
5055
var cancel = false
5156

52-
def loop(): Future[Unit] =
57+
def loop(): Future[Try[Unit]] =
5358
Future(cancel) flatMap { canceled =>
5459
if (canceled)
55-
Future.successful(())
60+
Future.successful(Try(()))
5661
else
5762
loop()
5863
}
@@ -64,12 +69,13 @@ class MacrotaskExecutorSuite extends FunSuite {
6469
loop()
6570
}
6671

67-
test("execute a bunch of stuff in 'parallel' and ensure it all runs") {
72+
@Test
73+
def `execute a bunch of stuff in 'parallel' and ensure it all runs` = {
6874
var i = 0
6975

7076
Future.sequence(List.fill(10000)(Future { i += 1 })) flatMap { _ =>
7177
Future {
72-
assert(i == 10000)
78+
Try(assert(i == 10000))
7379
}
7480
}
7581
}

webworker/src/main/scala/java/io/File.scala

Lines changed: 0 additions & 22 deletions
This file was deleted.

webworker/src/main/scala/org/scalajs/macrotaskexecutor/MacrotaskExecutorSuiteRunner.scala

Lines changed: 0 additions & 75 deletions
This file was deleted.
Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
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 org.scalajs.dom.DedicatedWorkerGlobalScope
20+
21+
import scala.scalajs.concurrent.QueueExecutionContext.timeouts
22+
import scala.scalajs.js
23+
24+
object MacrotaskExecutorTestsRunner {
25+
26+
def main(args: Array[String]): Unit = {
27+
val tests = new MacrotaskExecutorTests
28+
29+
implicit val ec = timeouts()
30+
31+
for {
32+
clamping <- tests.`sequence a series of 10,000 recursive executions without clamping`
33+
fairness <- tests.`preserve fairness with setTimeout`
34+
parallel <- tests.`execute a bunch of stuff in 'parallel' and ensure it all runs`
35+
} yield DedicatedWorkerGlobalScope.self.postMessage(js.Dictionary(
36+
"clamping" -> clamping.isSuccess,
37+
"fairness" -> fairness.isSuccess,
38+
"parallel" -> parallel.isSuccess
39+
))
40+
41+
()
42+
}
43+
}

webworker/src/test/scala/org/scalajs/macrotaskexecutor/WebWorkerMacrotaskSuite.scala

Lines changed: 0 additions & 57 deletions
This file was deleted.
Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
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 org.junit.Test
20+
import org.scalajs.dom.MessageEvent
21+
import org.scalajs.dom.Worker
22+
23+
import scala.concurrent.ExecutionContext
24+
import scala.concurrent.Future
25+
import scala.concurrent.Promise
26+
import scala.scalajs.concurrent.QueueExecutionContext.timeouts
27+
import scala.scalajs.js
28+
import scala.util.Failure
29+
import scala.util.Success
30+
import scala.util.Try
31+
32+
class WebWorkerMacrotaskTests {
33+
34+
implicit val ec: ExecutionContext = timeouts()
35+
36+
val worker = new Worker(s"file://${BuildInfo.workerDir}/main.js")
37+
38+
val testsResult = Promise[js.Dictionary[Boolean]]()
39+
worker.onmessage = { (event: MessageEvent) =>
40+
testsResult.success(event.data.asInstanceOf[js.Dictionary[Boolean]])
41+
}
42+
43+
def getTestResult(id: String): Future[Try[Unit]] =
44+
testsResult.future.map { results =>
45+
if (results.getOrElse(id, false))
46+
Success(())
47+
else
48+
Failure(new AssertionError)
49+
}
50+
51+
@Test
52+
def `sequence a series of 10,000 recursive executions without clamping` =
53+
getTestResult("clamping")
54+
55+
@Test
56+
def `preserve fairness with setTimeout` =
57+
getTestResult("fairness")
58+
59+
@Test
60+
def `execute a bunch of stuff in 'parallel' and ensure it all runs` =
61+
getTestResult("parallel")
62+
63+
}

0 commit comments

Comments
 (0)