Skip to content

Commit 7cc0a8e

Browse files
authored
Run Future tests sequentially, not concurrently (#359)
Fixes #358.
1 parent 1520fe2 commit 7cc0a8e

File tree

3 files changed

+23
-11
lines changed

3 files changed

+23
-11
lines changed

utest/src-native/utest/PlatformShims.scala

Lines changed: 3 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -2,22 +2,15 @@ package utest
22

33
// Taken from the implementation for JS
44

5-
import scala.concurrent.Future
5+
import scala.concurrent.{Await, Future}
6+
import concurrent.duration._
67
import scala.scalanative.reflect.Reflect
78

89
/**
910
* Platform specific stuff that differs between JVM, JS and Native
1011
*/
1112
object PlatformShims {
12-
def await[T](f: Future[T]): T = {
13-
scala.scalanative.runtime.loop()
14-
f.value match {
15-
case Some(v) => v.get
16-
case None => throw new IllegalStateException(
17-
"Test that returns Future must be run asynchronously in Scala Native, see TestTreeSeq::runAsync"
18-
)
19-
}
20-
}
13+
def await[T](f: Future[T]): T = Await.result(f, Duration.Inf)
2114

2215
type EnableReflectiveInstantiation =
2316
scala.scalanative.reflect.annotation.EnableReflectiveInstantiation

utest/src/utest/TestRunner.scala

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -171,7 +171,9 @@ object TestRunner {
171171
case HTree.Leaf(f) => f().map(HTree.Leaf(_))
172172
case HTree.Node(v, children @ _*) =>
173173
for{
174-
childValues <- Future.traverse(children.toSeq)(evaluateFutureTree(_))
174+
childValues <- children.foldLeft(Future.successful(List.newBuilder[HTree[N, L]])) { (fb, c) =>
175+
fb.flatMap(b => evaluateFutureTree(c).map(b += _))
176+
}.map(_.result())
175177
} yield HTree.Node(v, childValues:_*)
176178
}
177179

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
package test.utest
2+
import utest._
3+
import concurrent.{Future, ExecutionContext}
4+
5+
object FutureTest extends TestSuite {
6+
implicit val ec: ExecutionContext = ExecutionContext.global
7+
@volatile var flag = false
8+
9+
def tests = TestSuite {
10+
test("runs before next test"){
11+
Future { flag = true }
12+
}
13+
test("previous test ran first"){
14+
assert(flag)
15+
}
16+
}
17+
}

0 commit comments

Comments
 (0)