From 8eb2d7389d5367d6bea91df0975747aa942f15a1 Mon Sep 17 00:00:00 2001 From: Ethan Atkins Date: Wed, 21 Mar 2018 10:16:01 -0700 Subject: [PATCH] Add test for async utest TestSuites Sometimes when utest runs async tests (i.e. tests that return a future) the test suite will fail even when none of the individual tests do. This is due to a data race in sbt. Most, but not all, of the time, this test will induce that race. --- sbt/src/sbt-test/tests/fork-async/build.sbt | 8 +++++++ .../src/test/scala/ForkAsyncTest.scala | 21 +++++++++++++++++++ sbt/src/sbt-test/tests/fork-async/test | 1 + 3 files changed, 30 insertions(+) create mode 100644 sbt/src/sbt-test/tests/fork-async/build.sbt create mode 100644 sbt/src/sbt-test/tests/fork-async/src/test/scala/ForkAsyncTest.scala create mode 100644 sbt/src/sbt-test/tests/fork-async/test diff --git a/sbt/src/sbt-test/tests/fork-async/build.sbt b/sbt/src/sbt-test/tests/fork-async/build.sbt new file mode 100644 index 000000000..bb42b0c17 --- /dev/null +++ b/sbt/src/sbt-test/tests/fork-async/build.sbt @@ -0,0 +1,8 @@ +testFrameworks += new TestFramework("utest.runner.Framework") + +lazy val root = (project in file(".")). + settings( + scalaVersion := "2.12.4", + libraryDependencies += "com.lihaoyi" %% "utest" % "0.6.4" % Test, + fork in Test := true + ) diff --git a/sbt/src/sbt-test/tests/fork-async/src/test/scala/ForkAsyncTest.scala b/sbt/src/sbt-test/tests/fork-async/src/test/scala/ForkAsyncTest.scala new file mode 100644 index 000000000..a8bc4c0f2 --- /dev/null +++ b/sbt/src/sbt-test/tests/fork-async/src/test/scala/ForkAsyncTest.scala @@ -0,0 +1,21 @@ +import utest._ + +import utest._ +import utest.framework._ + +import scala.concurrent.{ ExecutionContext, Promise } +import scala.util.Success + +object ForkAsyncTest extends TestSuite { + val g = ExecutionContext.global + val n = 10 + val (testNames, promises) = (1 to n).map(i => Tree(s"$i") -> Promise[Unit]).unzip + val testTrees = promises.zipWithIndex.map { case (p, i) => + new TestCallTree(Left { + if (i == (n - 1)) promises.foreach(p => g.execute(() => p.tryComplete(Success(())))) + p.future + }) + } + val tests = + Tests(nameTree = Tree("async", testNames: _*), callTree = new TestCallTree(Right(testTrees))) +} diff --git a/sbt/src/sbt-test/tests/fork-async/test b/sbt/src/sbt-test/tests/fork-async/test new file mode 100644 index 000000000..dfffb838b --- /dev/null +++ b/sbt/src/sbt-test/tests/fork-async/test @@ -0,0 +1 @@ +> test