File: timer_tests.ml

package info (click to toggle)
ocaml-dune 3.20.2-3
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 33,564 kB
  • sloc: ml: 175,178; asm: 28,570; ansic: 5,251; sh: 1,096; lisp: 625; makefile: 148; python: 125; cpp: 48; javascript: 10
file content (61 lines) | stat: -rw-r--r-- 1,602 bytes parent folder | download
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 |}]
;;