From 8a123cd8e9cb806de69cfc1f9cd2a39ac4a2c34f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=C3=93lafur=20P=C3=A1ll=20Geirsson?= Date: Mon, 11 Oct 2021 12:33:59 +0200 Subject: [PATCH] Optimization: don't run timeout logic for non-async tests --- .../scala/munit/internal/PlatformCompat.scala | 35 +++++++++++-------- 1 file changed, 20 insertions(+), 15 deletions(-) diff --git a/munit/jvm/src/main/scala/munit/internal/PlatformCompat.scala b/munit/jvm/src/main/scala/munit/internal/PlatformCompat.scala index 38684e79..4eb2918e 100644 --- a/munit/jvm/src/main/scala/munit/internal/PlatformCompat.scala +++ b/munit/jvm/src/main/scala/munit/internal/PlatformCompat.scala @@ -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 =