Skip to content

Commit

Permalink
Merge pull request #768 from mzuehlke/infinite-timeouts
Browse files Browse the repository at this point in the history
Handle infinite timeouts
  • Loading branch information
tgodzik authored Apr 19, 2024
2 parents 7197bea + 067c717 commit cd435cb
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 15 deletions.
16 changes: 10 additions & 6 deletions munit/js/src/main/scala/munit/internal/PlatformCompat.scala
Original file line number Diff line number Diff line change
Expand Up @@ -36,16 +36,20 @@ object PlatformCompat {
ec: ExecutionContext
): Future[T] = {
val onComplete = Promise[T]()
val timeoutHandle = timers.setTimeout(duration.toMillis) {
onComplete.tryFailure(
new TimeoutException(s"test timed out after $duration")
)
}
val timeoutHandle =
if (duration.isFinite)
Some(timers.setTimeout(duration.toMillis) {
onComplete.tryFailure(
new TimeoutException(s"test timed out after $duration")
)
})
else
None
ec.execute(new Runnable {
def run(): Unit = {
startFuture().onComplete { result =>
onComplete.tryComplete(result)
timers.clearTimeout(timeoutHandle)
timeoutHandle.foreach(timers.clearTimeout(_))
}(ec)
}
})
Expand Down
24 changes: 15 additions & 9 deletions munit/jvm/src/main/scala/munit/internal/PlatformCompat.scala
Original file line number Diff line number Diff line change
Expand Up @@ -56,19 +56,25 @@ object PlatformCompat {
ec: ExecutionContext
): Future[T] = {
val onComplete = Promise[T]()
val timeout = sh.schedule[Unit](
() =>
onComplete.tryFailure(
new TimeoutException(s"test timed out after $duration")
),
duration.toMillis,
TimeUnit.MILLISECONDS
)
val timeout =
if (duration.isFinite)
Some(
sh.schedule[Unit](
() =>
onComplete.tryFailure(
new TimeoutException(s"test timed out after $duration")
),
duration.toMillis,
TimeUnit.MILLISECONDS
)
)
else
None
ec.execute(new Runnable {
def run(): Unit = {
startFuture().onComplete { result =>
onComplete.tryComplete(result)
timeout.cancel(false)
timeout.foreach(_.cancel(false))
}(ec)
}
})
Expand Down

0 comments on commit cd435cb

Please sign in to comment.