Skip to content

Commit

Permalink
Run Future tests sequentially, not concurrently (#359)
Browse files Browse the repository at this point in the history
Fixes #358.
  • Loading branch information
armanbilge authored Jan 9, 2025
1 parent 1520fe2 commit 7cc0a8e
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 11 deletions.
13 changes: 3 additions & 10 deletions utest/src-native/utest/PlatformShims.scala
Original file line number Diff line number Diff line change
Expand Up @@ -2,22 +2,15 @@ package utest

// Taken from the implementation for JS

import scala.concurrent.Future
import scala.concurrent.{Await, Future}
import concurrent.duration._
import scala.scalanative.reflect.Reflect

/**
* Platform specific stuff that differs between JVM, JS and Native
*/
object PlatformShims {
def await[T](f: Future[T]): T = {
scala.scalanative.runtime.loop()
f.value match {
case Some(v) => v.get
case None => throw new IllegalStateException(
"Test that returns Future must be run asynchronously in Scala Native, see TestTreeSeq::runAsync"
)
}
}
def await[T](f: Future[T]): T = Await.result(f, Duration.Inf)

type EnableReflectiveInstantiation =
scala.scalanative.reflect.annotation.EnableReflectiveInstantiation
Expand Down
4 changes: 3 additions & 1 deletion utest/src/utest/TestRunner.scala
Original file line number Diff line number Diff line change
Expand Up @@ -171,7 +171,9 @@ object TestRunner {
case HTree.Leaf(f) => f().map(HTree.Leaf(_))
case HTree.Node(v, children @ _*) =>
for{
childValues <- Future.traverse(children.toSeq)(evaluateFutureTree(_))
childValues <- children.foldLeft(Future.successful(List.newBuilder[HTree[N, L]])) { (fb, c) =>
fb.flatMap(b => evaluateFutureTree(c).map(b += _))
}.map(_.result())
} yield HTree.Node(v, childValues:_*)
}

Expand Down
17 changes: 17 additions & 0 deletions utest/test/src/test/utest/FutureTest.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
package test.utest
import utest._
import concurrent.{Future, ExecutionContext}

object FutureTest extends TestSuite {
implicit val ec: ExecutionContext = ExecutionContext.global
@volatile var flag = false

def tests = TestSuite {
test("runs before next test"){
Future { flag = true }
}
test("previous test ran first"){
assert(flag)
}
}
}

0 comments on commit 7cc0a8e

Please sign in to comment.