Skip to content

Commit

Permalink
clocks: update tests
Browse files Browse the repository at this point in the history
  • Loading branch information
bikallem committed Sep 21, 2022
1 parent 9891542 commit 543bcd7
Show file tree
Hide file tree
Showing 6 changed files with 17 additions and 14 deletions.
10 changes: 5 additions & 5 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -832,12 +832,12 @@ The standard environment provides a [clock][Eio.Time] with the usual POSIX time:

```ocaml
# Eio_main.run @@ fun env ->
let clock = Eio.Stdenv.clock env in
traceln "The time is now %f" (Eio.Time.now clock);
let clock = Eio.Stdenv.real_clock env in
traceln "The time is now %a" Ptime.pp (Eio.Time.now clock);
Eio.Time.sleep clock 1.0;
traceln "The time is now %f" (Eio.Time.now clock);;
+The time is now 1623940778.270336
+The time is now 1623940779.270336
traceln "The time is now %a" Ptime.pp (Eio.Time.now clock);;
+The time is now 2021-06-17 14:39:38 +00:00
+The time is now 2021-06-17 14:39:39 +00:00
- : unit = ()
```

Expand Down
2 changes: 1 addition & 1 deletion doc/dune
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
(mdx
(package eio_main)
(packages eio_main)
(packages eio_main ptime)
(files multicore.md))
11 changes: 7 additions & 4 deletions doc/prelude.ml
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,8 @@ module Eio_main = struct

let now = ref 1623940778.27033591

let fake_clock real_clock = object (_ : #Eio.Time.clock)
method now = !now
let fake_clock real_clock = object (_ : 'a #Eio.Time.clock)
method now = Ptime.of_float_s !now |> Option.get
method sleep_until time =
(* The fake times are all in the past, so we just ask to wait until the
fake time is due and it will happen immediately. If we wait for
Expand All @@ -15,7 +15,9 @@ module Eio_main = struct
empty, so this is a convenient way to wait for the system to be idle.
TODO: This is no longer true (since #213). *)
Eio.Time.sleep_until real_clock time;
now := max !now time
now := max !now (Ptime.to_float_s time)
method add_seconds = real_clock#add_seconds
method to_seconds = real_clock#to_seconds
end

(* To avoid non-deterministic output, we run the examples a single domain. *)
Expand All @@ -32,7 +34,8 @@ module Eio_main = struct
method stdout = env#stdout
method cwd = env#cwd
method domain_mgr = fake_domain_mgr
method clock = fake_clock env#clock
method real_clock = fake_clock env#real_clock
method mono_clock = env#mono_clock
end
end

Expand Down
2 changes: 1 addition & 1 deletion lib_eio_linux/eio_linux.ml
Original file line number Diff line number Diff line change
Expand Up @@ -514,7 +514,7 @@ let rec schedule ({run_q; sleep_q; mem_q; uring; _} as st) : [`Exit_scheduler] =
| Some Failed_thread (k, ex) -> Suspended.discontinue k ex
| Some IO -> (* Note: be sure to re-inject the IO task before continuing! *)
(* This is not a fair scheduler: timers always run before all other IO *)
let now = Eio_unix.(mono_clock#now |> mono_clock#to_seconds) in
let now = Eio_unix.(real_clock#now |> real_clock#to_seconds) in
match Zzz.pop ~now sleep_q with
| `Due k ->
Lf_queue.push run_q IO; (* Re-inject IO job in the run queue *)
Expand Down
2 changes: 1 addition & 1 deletion lib_eio_luv/eio_luv.ml
Original file line number Diff line number Diff line change
Expand Up @@ -520,7 +520,7 @@ module Low_level = struct
module Poll = Poll

let sleep_until due st (k: unit Suspended.t) =
let now = Eio_unix.(mono_clock#now |> mono_clock#to_seconds) in
let now = Eio_unix.(real_clock#now |> real_clock#to_seconds) in
let delay = 1000. *. (due -. now) |> ceil |> truncate |> max 0 in
let timer = Luv.Timer.init ~loop:st.loop () |> or_raise in
Fiber_context.set_cancel_fn k.fiber (fun ex ->
Expand Down
4 changes: 2 additions & 2 deletions tests/time.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,9 @@
```ocaml
open Eio.Std
let run (fn : clock:Eio.Time.clock -> unit) =
let run (fn : clock:'a Eio.Time.clock -> unit) =
Eio_main.run @@ fun env ->
let clock = Eio.Stdenv.clock env in
let clock = Eio.Stdenv.real_clock env in
fn ~clock
```

Expand Down

0 comments on commit 543bcd7

Please sign in to comment.