Skip to content

Added tests for MacrotaskExecutor #8

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 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
9 changes: 7 additions & 2 deletions build.sbt
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,9 @@ import org.scalajs.jsenv.selenium.SeleniumJSEnv

import java.util.concurrent.TimeUnit

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

ThisBuild / baseVersion := "0.1"

ThisBuild / organization := "org.scala-js"
Expand Down Expand Up @@ -123,6 +126,8 @@ ThisBuild / Test / jsEnv := {
}
}

ThisBuild / Test / testOptions += Tests.Argument(MUnitFramework, "+l")

// project structure

lazy val root = project
Expand All @@ -133,7 +138,7 @@ lazy val core = project
.in(file("core"))
.settings(
name := "scala-js-macrotask-executor",
libraryDependencies += "org.scalameta" %%% "munit" % "0.7.29" % Test,
libraryDependencies += "org.scalameta" %%% "munit" % MUnitVersion % Test,
)
.enablePlugins(ScalaJSPlugin)

Expand All @@ -146,7 +151,7 @@ lazy val webworker = project
scalaJSUseMainModuleInitializer := true,
libraryDependencies ++= Seq(
("org.scala-js" %%% "scalajs-dom" % "1.2.0").cross(CrossVersion.for3Use2_13),
"org.scalameta" %%% "munit" % "0.7.29" % Test,
"org.scalameta" %%% "munit" % MUnitVersion % Test,
),
(Test / test) := (Test / test).dependsOn(Compile / fastOptJS).value,
buildInfoKeys := Seq[BuildInfoKey](scalaVersion, baseDirectory),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -150,4 +150,8 @@ object MacrotaskExecutor extends ExecutionContext {
}
}
}

object Implicits {
implicit def global: ExecutionContext = MacrotaskExecutor
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,59 @@ package org.scalajs.macrotaskexecutor

import munit.FunSuite

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

class MacrotaskExecutorSuite extends FunSuite {

import MacrotaskExecutor.Implicits._

test("sequence a series of 10,000 recursive executions without clamping") {
def loop(n: Int): Future[Int] =
if (n <= 0)
Future(0)
else
Future.unit.flatMap(_ => loop(n - 1)).map(_ + 1)

// val start = System.currentTimeMillis()
// 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)

loop(10000) flatMap { res =>
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
}
}
}

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

def loop(): Future[Unit] =
Future(cancel) flatMap { canceled =>
if (canceled)
Future.unit
else
loop()
}

js.timers.setTimeout(100.millis) {
cancel = true
}

loop()
}

test("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)
}
}
}
}
Loading