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 12: await fiber 4
13: res 4 = 4 13: res 4 = 4
14: await fiber 5 14: await fiber 5
15: other fib done 15: res 5 = 5
16: res 5 = 5 16: await fiber 6
17: await fiber 6 17: res 6 = 6
18: res 6 = 6 18: await fiber 7
19: await fiber 7 19: main fiber cancelled with Failure("oh no!")
20: main fiber cancelled with Failure("oh no!") 20: main fiber result: error Failure("oh no!")
21: main fiber result: error Failure("oh no!") 21: main fib failed with "oh no!"
22: main fib failed with "oh no!" 22: main fiber exited
23: main fiber exited

View file

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