Skip to content

Commit b714c2b

Browse files
authored
Merge pull request #8 from djspiewak/feature/tests
Added tests for MacrotaskExecutor
2 parents 63d35b3 + 60fed09 commit b714c2b

File tree

4 files changed

+567
-3
lines changed

4 files changed

+567
-3
lines changed

build.sbt

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,9 @@ 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+
2730
ThisBuild / baseVersion := "0.1"
2831

2932
ThisBuild / organization := "org.scala-js"
@@ -123,6 +126,8 @@ ThisBuild / Test / jsEnv := {
123126
}
124127
}
125128

129+
ThisBuild / Test / testOptions += Tests.Argument(MUnitFramework, "+l")
130+
126131
// project structure
127132

128133
lazy val root = project
@@ -133,7 +138,7 @@ lazy val core = project
133138
.in(file("core"))
134139
.settings(
135140
name := "scala-js-macrotask-executor",
136-
libraryDependencies += "org.scalameta" %%% "munit" % "0.7.29" % Test,
141+
libraryDependencies += "org.scalameta" %%% "munit" % MUnitVersion % Test,
137142
)
138143
.enablePlugins(ScalaJSPlugin)
139144

@@ -146,7 +151,7 @@ lazy val webworker = project
146151
scalaJSUseMainModuleInitializer := true,
147152
libraryDependencies ++= Seq(
148153
("org.scala-js" %%% "scalajs-dom" % "1.2.0").cross(CrossVersion.for3Use2_13),
149-
"org.scalameta" %%% "munit" % "0.7.29" % Test,
154+
"org.scalameta" %%% "munit" % MUnitVersion % Test,
150155
),
151156
(Test / test) := (Test / test).dependsOn(Compile / fastOptJS).value,
152157
buildInfoKeys := Seq[BuildInfoKey](scalaVersion, baseDirectory),

core/src/main/scala/org/scalajs/macrotaskexecutor/MacrotaskExecutor.scala

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -150,4 +150,8 @@ object MacrotaskExecutor extends ExecutionContext {
150150
}
151151
}
152152
}
153+
154+
object Implicits {
155+
implicit def global: ExecutionContext = MacrotaskExecutor
156+
}
153157
}

core/src/test/scala/org/scalajs/macrotaskexecutor/MacrotaskExecutorSuite.scala

Lines changed: 54 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,59 @@ package org.scalajs.macrotaskexecutor
1818

1919
import munit.FunSuite
2020

21+
import scala.concurrent.Future
22+
import scala.concurrent.duration._
23+
import scala.scalajs.js
24+
2125
class MacrotaskExecutorSuite extends FunSuite {
22-
26+
import MacrotaskExecutor.Implicits._
27+
28+
test("sequence a series of 10,000 recursive executions without clamping") {
29+
def loop(n: Int): Future[Int] =
30+
if (n <= 0)
31+
Future(0)
32+
else
33+
Future.unit.flatMap(_ => loop(n - 1)).map(_ + 1)
34+
35+
// val start = System.currentTimeMillis()
36+
// val MinimumClamp = 10000 * 2 * 4 // HTML5 specifies a 4ms clamp (https://developer.mozilla.org/en-US/docs/Web/API/WindowTimers.setTimeout#Minimum.2F_maximum_delay_and_timeout_nesting)
37+
38+
loop(10000) flatMap { res =>
39+
Future {
40+
// val end = System.currentTimeMillis()
41+
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+
}
45+
}
46+
}
47+
48+
// this test fails to terminate with a Promise-based executor
49+
test("preserve fairness with setTimeout") {
50+
var cancel = false
51+
52+
def loop(): Future[Unit] =
53+
Future(cancel) flatMap { canceled =>
54+
if (canceled)
55+
Future.unit
56+
else
57+
loop()
58+
}
59+
60+
js.timers.setTimeout(100.millis) {
61+
cancel = true
62+
}
63+
64+
loop()
65+
}
66+
67+
test("execute a bunch of stuff in 'parallel' and ensure it all runs") {
68+
var i = 0
69+
70+
Future.sequence(List.fill(10000)(Future { i += 1 })) flatMap { _ =>
71+
Future {
72+
assert(i == 10000)
73+
}
74+
}
75+
}
2376
}

0 commit comments

Comments
 (0)