open Stdune
open Fiber.O
open Dune_scheduler

let config =
  Dune_engine.Clflags.display := Short;
  { Scheduler.Config.concurrency = 1
  ; print_ctrl_c_warning = false
  ; watch_exclusions = []
  }
;;

let on_event (_ : Scheduler.Run.Event.t) = ()

let%expect_test "create and wait for timer" =
  Scheduler.Run.go ~on_event config (fun () ->
    let now () = Time.now () in
    let start = now () in
    let duration = Time.Span.of_secs 0.2 in
    let+ () = Scheduler.sleep duration in
    assert (Time.Span.to_secs (Time.diff (now ()) start) >= Time.Span.to_secs 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+ () =
           let duration = Time.Span.of_secs duration in
           Scheduler.sleep duration
         in
         printfn "finished %0.2f" duration));
  [%expect
    {|
    finished 0.10
    finished 0.20
    finished 0.30 |}]
;;

let%expect_test "simultaneous timers are deterministic" =
  Scheduler.Run.go ~on_event config (fun () ->
    [ "first"; "second"; "third" ]
    |> Fiber.parallel_iter ~f:(fun name ->
      let+ () = Scheduler.sleep Time.Span.zero in
      print_endline name));
  [%expect
    {|
    first
    second
    third |}]
;;

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+ (_ : Proc.Process_info.t) =
         Scheduler.wait_for_process
           ~timeout:(Time.Span.of_secs 0.1)
           ~is_process_group_leader:false
           pid
       in
       print_endline "sleep timed out");
  [%expect
    {|
    sleep timed out |}]
;;
