<commit_before>(* This file is part of Lwt, released under the MIT license. See LICENSE.md for
   details, or visit https://github.com/ocsigen/lwt/blob/master/LICENSE.md. *)

open Test
open Lwt.Infix

(* None of the APIs make promises about how much larger the elapsed time will
 * be, but they all promise that it won't be less than the expected time. *)
let cmp_elapsed_time start_time expected_time =
  let elapsed_time = Unix.gettimeofday () -. start_time in
  let diff = elapsed_time -. expected_time in
  diff >= 0. && diff <= 0.1

let suite = suite "Lwt_unix sleep and timeout" [
    test "sleep" begin fun () ->
      let start_time = Unix.gettimeofday () in
      let duration = 1.0 in
      Lwt_unix.sleep duration
      >>= fun () ->
      let check = cmp_elapsed_time start_time duration in
      Lwt.return check
    end;

    test "timeout" begin fun () ->
      let start_time = Unix.gettimeofday () in
      let duration = 1.0 in
      Lwt.catch
        (fun () ->
           Lwt_unix.timeout duration
           >>= fun () -> Lwt.return_false
        )
        (function
          | Lwt_unix.Timeout ->
            let check = cmp_elapsed_time start_time duration in
            Lwt.return check
          | exn -> Lwt.fail exn
        )
    end;

    test "with_timeout : no timeout" begin fun () ->
      let duration = 1.0 in
      Lwt_unix.with_timeout duration Lwt_unix.yield
      >>= fun () -> Lwt.return_true
    end;

    test "with_timeout : timeout" begin fun () ->
      let start_time = Unix.gettimeofday () in
      let duration = 1.0 in
      let f () = Lwt_unix.sleep 2.0 in
      Lwt.catch
        (fun () ->
           Lwt_unix.with_timeout duration f
           >>= fun () ->
           Lwt.return false
        )
        (function
          | Lwt_unix.Timeout ->
            let check = cmp_elapsed_time start_time duration in
            Lwt.return check
          | exn -> Lwt.fail exn
        )
    end;

    test "yield" begin fun () ->
      let bind_callback_ran = ref false in
      Lwt.async (fun () -> Lwt.return () >|= fun () -> bind_callback_ran := true);
      let bind_is_immediate = !bind_callback_ran in
      let yield_callback_ran = ref false in
      Lwt.async (fun () -> Lwt_unix.yield () >|= fun () -> yield_callback_ran := true);
      let yield_is_immediate = !yield_callback_ran in
      Lwt.return (bind_is_immediate && not yield_is_immediate)
    end;

    test "auto_yield" begin fun () ->
      let f = Lwt_unix.auto_yield 1.0 in
      let run_auto_yield () =
        let callback_ran = ref false in
        Lwt.async (fun () -> f () >|= fun () -> callback_ran := true);
        !callback_ran;
      in
      let check1 = run_auto_yield () in
      let check2 = run_auto_yield () in<commit_msg>Instrument a timeout test<commit_after>(* This file is part of Lwt, released under the MIT license. See LICENSE.md for
   details, or visit https://github.com/ocsigen/lwt/blob/master/LICENSE.md. *)

open Test
open Lwt.Infix

(* None of the APIs make promises about how much larger the elapsed time will
 * be, but they all promise that it won't be less than the expected time. *)
let cmp_elapsed_time start_time expected_time =
  let elapsed_time = Unix.gettimeofday () -. start_time in
  let diff = elapsed_time -. expected_time in
  diff >= 0. && diff <= 0.1

let suite = suite "Lwt_unix sleep and timeout" [
    test "sleep" begin fun () ->
      let start_time = Unix.gettimeofday () in
      let duration = 1.0 in
      Lwt_unix.sleep duration
      >>= fun () ->
      let check = cmp_elapsed_time start_time duration in
      Lwt.return check
    end;

    test "timeout" begin fun () ->
      let start_time = Unix.gettimeofday () in
      let duration = 1.0 in
      Lwt.catch
        (fun () ->
           Lwt_unix.timeout duration
           >>= fun () -> Lwt.return_false
        )
        (function
          | Lwt_unix.Timeout ->
            let check = cmp_elapsed_time start_time duration in
            Lwt.return check
          | exn -> Lwt.fail exn
        )
    end;

    test "with_timeout : no timeout" begin fun () ->
      let duration = 1.0 in
      Lwt_unix.with_timeout duration Lwt_unix.yield
      >>= fun () -> Lwt.return_true
    end;

    test "with_timeout : timeout" begin fun () ->
      let start_time = Unix.gettimeofday () in
      let duration = 1.0 in
      let f () = Lwt_unix.sleep 2.0 in
      Lwt.catch
        (fun () ->
           Lwt_unix.with_timeout duration f
           >>= fun () ->
           Printf.eprintf "\nno timeout\n";
           Lwt.return false
        )
        (function
          | Lwt_unix.Timeout ->
            let check = cmp_elapsed_time start_time duration in
            if not check then
              Printf.eprintf "\nstart_time: %f, duration: %f\n"
                start_time duration;
            Lwt.return check
          | exn -> Lwt.fail exn
        )
    end;

    test "yield" begin fun () ->
      let bind_callback_ran = ref false in
      Lwt.async (fun () -> Lwt.return () >|= fun () -> bind_callback_ran := true);
      let bind_is_immediate = !bind_callback_ran in
      let yield_callback_ran = ref false in
      Lwt.async (fun () -> Lwt_unix.yield () >|= fun () -> yield_callback_ran := true);
      let yield_is_immediate = !yield_callback_ran in
      Lwt.return (bind_is_immediate && not yield_is_immediate)
    end;

    test "auto_yield" begin fun () ->
      let f = Lwt_unix.auto_yield 1.0 in
      let run_auto_yield () =
        let callback_ran = ref false in
        Lwt.async (fun () -> f () >|= fun () -> callback_ran := true);
        !callback_ran;
      in
      let check1 = run_auto_yield () in
      let check2 = run_auto_yield () in
{'fraction_non_alphanumeric': 0.1018762055058741, 'fraction_numerical': 0.005961774504646677, 'mean_word_length': 2.0324295587453483, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 71, 'https://': 2, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '6522351', 'n_tokens_mistral': 1866, 'n_tokens_neox': 1788, 'n_words': 534}