-
-
Notifications
You must be signed in to change notification settings - Fork 224
/
MacrotaskExecutorSpec.scala
40 lines (28 loc) · 1.13 KB
/
MacrotaskExecutorSpec.scala
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
package org.specs2
package concurrent
import scala.concurrent.*, duration.*
import LoopingCode.*
import specification.core.*
import runner.*
class MacrotaskExecutorSpec(env: Env) extends Specification:
def is = s2"""
This specification is a copy of the coreJS/MacrotaskExecutorSpec to check
if timeouts work ok on the JVM
An example can be timed-out when using Scala on the $timeoutExample
"""
def timeoutExample =
given ExecutionContext = env.executionContext
TextRunner.runFuture(MacrotaskExecutorSpecification())(env).map { output =>
output.messages must contain(contain("timeout after 500 milliseconds"))
}
class MacrotaskExecutorSpecification extends Specification:
def is = args.execute(timeout = 500.millis) ^ s2"""
An example can be timed-out when using ScalaJS $timeoutExample
"""
def timeoutExample =
given ExecutionContext = scala.concurrent.ExecutionContext.Implicits.global
loop.map(_ => ok)
object LoopingCode:
var cancel = false
def loop(using executionContext: ExecutionContext): Future[Unit] =
Future(cancel).flatMap(canceled => if canceled then Future.unit else loop)