Skip to content

Commit

Permalink
Optimization: don't run timeout logic for non-async tests
Browse files Browse the repository at this point in the history
  • Loading branch information
olafurpg committed Oct 11, 2021
1 parent cfc64be commit 8a123cd
Showing 1 changed file with 20 additions and 15 deletions.
35 changes: 20 additions & 15 deletions munit/jvm/src/main/scala/munit/internal/PlatformCompat.scala
Original file line number Diff line number Diff line change
Expand Up @@ -33,21 +33,26 @@ object PlatformCompat {
duration: Duration,
ec: ExecutionContext
): Future[T] = {
val onComplete = Promise[T]()
var onCancel: () => Unit = () => ()
future.onComplete { result =>
onComplete.tryComplete(result)
}(ec)
val timeout = sh.schedule[Unit](
() =>
onComplete.tryFailure(
new TimeoutException(s"test timed out after $duration")
),
duration.toMillis,
TimeUnit.MILLISECONDS
)
onCancel = () => timeout.cancel(false)
onComplete.future
if (future.value.isDefined) {
// Avoid heavy timeout overhead for non-async tests.
future
} else {
val onComplete = Promise[T]()
var onCancel: () => Unit = () => ()
future.onComplete { result =>
onComplete.tryComplete(result)
}(ec)
val timeout = sh.schedule[Unit](
() =>
onComplete.tryFailure(
new TimeoutException(s"test timed out after $duration")
),
duration.toMillis,
TimeUnit.MILLISECONDS
)
onCancel = () => timeout.cancel(false)
onComplete.future
}
}

def isIgnoreSuite(cls: Class[_]): Boolean =
Expand Down

0 comments on commit 8a123cd

Please sign in to comment.