File: time.md

package info (click to toggle)
ocaml-eio 1.3-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 2,548 kB
  • sloc: ml: 14,608; ansic: 1,237; makefile: 25
file content (251 lines) | stat: -rw-r--r-- 4,931 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
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
# Setting up the environment

```ocaml
# #require "eio_main";;
# #require "eio.mock";;
```

```ocaml
open Eio.Std

let run (fn : clock:float Eio.Time.clock_ty r -> unit) =
  Eio_main.run @@ fun env ->
  let clock = Eio.Stdenv.clock env in
  fn ~clock
```

# Test cases

Check sleep works:

```ocaml
# run @@ fun ~clock ->
  let t0 = Unix.gettimeofday () in
  Eio.Time.sleep clock 0.01;
  let t1 = Unix.gettimeofday () in
  assert (t1 -. t0 >= 0.01);;
- : unit = ()
```

Cancelling sleep:

```ocaml
# run @@ fun ~clock ->
  Fiber.both
    (fun () -> Eio.Time.sleep clock 1200.; assert false)
    (fun () -> failwith "Simulated cancel");;
Exception: Failure "Simulated cancel".
```

Switch is already off:

```ocaml
# run @@ fun ~clock ->
  Switch.run @@ fun sw ->
  Switch.fail sw (Failure "Simulated failure");
  Eio.Time.sleep clock 1200.0;
  assert false;;
Exception: Failure "Simulated failure".
```

Scheduling a timer that's already due:

```ocaml
# run @@ fun ~clock ->
  Switch.run @@ fun sw ->
  Fiber.both
    (fun () -> traceln "First fiber runs"; Eio.Time.sleep clock (-1.0); traceln "Sleep done")
    (fun () -> traceln "Second fiber runs");;
+First fiber runs
+Second fiber runs
+Sleep done
- : unit = ()
```

Check ordering works:

```ocaml
# run @@ fun ~clock ->
  Switch.run @@ fun sw ->
  Fiber.both
    (fun () ->
      Eio.Time.sleep clock 1200.0;
      assert false
    )
    (fun () ->
      Eio.Time.sleep clock 0.1;
      traceln "Short timer finished";
      failwith "Simulated cancel"
    );;
+Short timer finished
Exception: Failure "Simulated cancel".
```

Check Unix debug clock:
```ocaml
# Eio_main.run @@ fun _ ->
  Fiber.both
    (fun () -> traceln "First thread starts"; Eio_unix.sleep 0.001; traceln "Sleep done")
    (fun () -> traceln "Second thread starts");;
+First thread starts
+Second thread starts
+Sleep done
- : unit = ()
```

Timer and busy loop:
```ocaml
let rec loop () =
  Eio.Fiber.yield ();
  loop ()
```

```ocaml
# run @@ fun ~clock ->
  Fiber.yield ();
  Eio.Time.sleep clock 0.01;
  Fiber.first
    loop
    (fun () -> Eio.Time.sleep clock 0.01);;
- : unit = ()
```

### Timeouts

```ocaml
# Eio_main.run @@ fun env ->
  let clock = Eio.Stdenv.mono_clock env in
  Eio.Time.Timeout.(run_exn none) (fun () -> ());
  let t = Eio.Time.Timeout.seconds clock 0.0001 in
  Eio.Time.Timeout.run_exn t (fun () -> Fiber.await_cancel ());;
Exception: Eio__Time.Timeout.
```

```ocaml
# Eio_main.run @@ fun env ->
  let clock = Eio.Stdenv.mono_clock env in
  let show d =
    let t = Eio.Time.Timeout.seconds clock d in
    traceln "%g -> %a" d Eio.Time.Timeout.pp t
  in
  show 0.000000001;
  show 0.01;
  show 0.1;
  show 2.;
  show 60.;
  show 61.5;
  show 120.;
  show (30. *. 60.);
  ;;
+1e-09 -> 1e-09s
+0.01 -> 10ms
+0.1 -> 0.1s
+2 -> 2s
+60 -> 60s
+61.5 -> 62s
+120 -> 2m
+1800 -> 30m
- : unit = ()
```

### Mock clock

```ocaml
let mock = Eio_mock.Clock.make ()
let sleeper label time () = Eio.Time.sleep_until mock time; traceln "%s (%g) woken" label time
```

Advancing the time:

```ocaml
# Eio_mock.Backend.run @@ fun () ->
  Fiber.all [
    sleeper "A" 5.0;
    sleeper "B" 7.0;
    sleeper "C" 2.0;
    sleeper "D" 0.0;
    sleeper "E" 5.0;
    (fun () ->
       while true do
         Fiber.yield ();
         Eio_mock.Clock.advance mock
       done
    )
  ];;
+D (0) woken
+mock time is now 2
+C (2) woken
+mock time is now 5
+A (5) woken
+E (5) woken
+mock time is now 7
+B (7) woken
Exception: Invalid_argument "No further events scheduled on mock clock".
```

Setting the time directly:

```ocaml
# Eio_mock.Backend.run @@ fun () ->
  Eio_mock.Clock.set_time mock 0.0;
  Fiber.all [
    sleeper "A" 5.0;
    sleeper "B" 7.0;
    sleeper "C" 2.0;
    sleeper "D" 0.0;
    sleeper "E" 5.0;
    (fun () ->
       Fiber.yield ();
       Eio_mock.Clock.set_time mock 5.0;
       Fiber.yield ();
       Eio_mock.Clock.set_time mock 1.0;
       Fiber.yield ();
       Eio_mock.Clock.set_time mock 10.0;
       Fiber.yield ();
       Eio_mock.Clock.set_time mock 12.0
    )
  ];;
+mock time is now 0
+D (0) woken
+mock time is now 5
+C (2) woken
+A (5) woken
+E (5) woken
+mock time is now 1
+mock time is now 10
+B (7) woken
+mock time is now 12
- : unit = ()
```

Cancellation:

```ocaml
# Eio_mock.Backend.run @@ fun () ->
  Eio_mock.Clock.set_time mock 0.0;
  Fiber.first
    (sleeper "A" 5.0)
    (fun () -> traceln "Cancel sleeper");
  Eio_mock.Clock.advance mock;;
+mock time is now 0
+Cancel sleeper
Exception: Invalid_argument "No further events scheduled on mock clock".
```

Sleep:

```ocaml
# try
    Eio_mock.Backend.run_full @@ fun env ->
    let timeout = Eio.Time.Timeout.seconds env#mono_clock 2. in
    Eio.Time.Timeout.sleep timeout;
    traceln "Timeout done";
    Eio.Time.Timeout.(sleep none);
    assert false
  with Eio_mock.Backend.Deadlock_detected ->
    traceln "Never finished";;
+mock time is now 2
+Timeout done
+Never finished
- : unit = ()
```