update fiber tests to try to be more deterministic

This commit is contained in:
Simon Cruanes 2024-04-02 11:26:44 -04:00
parent ef6811e062
commit cf6b20a979
No known key found for this signature in database
GPG key ID: EBFFF6F283F3A2B4
2 changed files with 28 additions and 14 deletions

View file

@ -52,12 +52,11 @@ start
12: await fiber 4
13: res 4 = 4
14: await fiber 5
15: other fib done
16: res 5 = 5
17: await fiber 6
18: res 6 = 6
19: await fiber 7
20: main fiber cancelled with Failure("oh no!")
21: main fiber result: error Failure("oh no!")
22: main fib failed with "oh no!"
23: main fiber exited
15: res 5 = 5
16: await fiber 6
17: res 6 = 6
18: await fiber 7
19: main fiber cancelled with Failure("oh no!")
20: main fiber result: error Failure("oh no!")
21: main fib failed with "oh no!"
22: main fiber exited

View file

@ -86,6 +86,7 @@ let () =
()
let () =
let@ _r = Moonpool_fib.main in
(* same but now, cancel one of the sub-fibers *)
Printf.printf "============\nstart\n";
@ -98,6 +99,9 @@ let () =
@@ Exn_bt.show ebt)
in
let chans_unblock = Array.init 10 (fun _i -> Chan.create ()) in
let chan_progress = Chan.create () in
logf (TS.tick_get clock) "start fibers";
let subs =
List.init 10 (fun i ->
@ -107,12 +111,19 @@ let () =
F.with_on_self_cancel (fun _ ->
logf (TS.tick_get clock) "sub-fiber %d was cancelled" i)
in
Thread.delay (float i *. 0.05);
F.yield ();
Thread.delay 0.002;
(* sync for determinism *)
Chan.pop_await chans_unblock.(i);
Chan.push chan_progress i;
if i = 7 then (
logf (TS.tick_get clock) "I'm fiber %d and I'm about to fail…" i;
failwith "oh no!"
);
F.check_if_cancelled ();
i)
in
@ -124,9 +135,13 @@ let () =
| Error _ -> logf (i :: post) "fiber %d resolved as error" i))
subs;
F.spawn_ignore ~protect:false (fun _n ->
Thread.delay 0.2;
logf (TS.tick_get clock) "other fib done");
(* sequentialize the fibers, for determinism *)
F.spawn_ignore (fun () ->
for j = 0 to 9 do
Chan.push chans_unblock.(j) ();
let j' = Chan.pop_await chan_progress in
assert (j = j')
done);
logf (TS.tick_get clock) "wait for subs";
List.iteri