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 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61
|
open Stdune
open Fiber.O
module Scheduler = Dune_engine.Scheduler
let config =
Dune_engine.Clflags.display := Short;
{ Scheduler.Config.concurrency = 1
; stats = None
; print_ctrl_c_warning = false
; watch_exclusions = []
}
;;
let%expect_test "create and wait for timer" =
Scheduler.Run.go
~on_event:(fun _ _ -> ())
config
(fun () ->
let now () = Unix.gettimeofday () in
let start = now () in
let duration = 0.2 in
let+ () = Scheduler.sleep ~seconds:duration in
assert (now () -. start >= duration);
print_endline "timer finished successfully");
[%expect {| timer finished successfully |}]
;;
let%expect_test "multiple timers" =
Scheduler.Run.go
~on_event:(fun _ _ -> ())
config
(fun () ->
[ 0.3; 0.2; 0.1 ]
|> Fiber.parallel_iter ~f:(fun duration ->
let+ () = Scheduler.sleep ~seconds:duration in
printfn "finished %0.2f" duration));
[%expect
{|
finished 0.10
finished 0.20
finished 0.30 |}]
;;
let%expect_test "run process with timeout" =
Scheduler.Run.go
~on_event:(fun _ _ -> ())
config
(fun () ->
let pid =
let prog =
let path = Env.get Env.initial "PATH" |> Option.value_exn |> Bin.parse_path in
Bin.which ~path "sleep" |> Option.value_exn |> Path.to_string
in
Spawn.spawn ~prog ~argv:[ prog; "100000" ] () |> Pid.of_int
in
let+ _ = Scheduler.wait_for_process ~timeout_seconds:0.1 pid in
print_endline "sleep timed out");
[%expect
{|
sleep timed out |}]
;;
|