Skip to content

Commit

Permalink
test
Browse files Browse the repository at this point in the history
  • Loading branch information
c-cube committed Apr 2, 2024
1 parent 4fb5a13 commit 6ad8c68
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 21 deletions.
3 changes: 1 addition & 2 deletions test/fiber/t_fib1.expected
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,7 @@ start
1.4.2: cur fiber[4] is some: true
1.4.3: res 4 = 4
2: main fiber done
3: other fib done
4: main fiber exited
3: main fiber exited
============
start
1: start fibers
Expand Down
49 changes: 30 additions & 19 deletions test/fiber/t_fib1.ml
Original file line number Diff line number Diff line change
Expand Up @@ -52,31 +52,42 @@ let () =
let clock = ref TS.init in
let fib =
F.spawn_top ~on:runner @@ fun () ->
let chan_progress = Chan.create () in
let chans = Array.init 5 (fun _ -> Chan.create ()) in

let subs =
List.init 5 (fun i ->
F.spawn ~protect:false @@ fun _n ->
Thread.delay (float i *. 0.01);
Chan.pop_await chans.(i);
Chan.push chan_progress i;
F.check_if_cancelled ();
i)
in

F.spawn_ignore ~protect:false (fun _n ->
Thread.delay 0.4;
TS.tick clock;
logf !clock "other fib done");

logf (TS.tick_get clock) "wait for subs";
List.iteri
(fun i f ->
let clock = ref (0 :: i :: !clock) in
logf !clock "await fiber %d" i;
logf (TS.tick_get clock) "cur fiber[%d] is some: %b" i
(Option.is_some @@ F.Private_.get_cur ());
let res = F.await f in
logf (TS.tick_get clock) "cur fiber[%d] is some: %b" i
(Option.is_some @@ F.Private_.get_cur ());
F.yield ();
logf (TS.tick_get clock) "res %d = %d" i res)
subs;

F.spawn_ignore (fun () ->
for i = 0 to 4 do
Chan.push chans.(i) ();
let i' = Chan.pop_await chan_progress in
assert (i = i')
done);

(let clock0 = !clock in
List.iteri
(fun i f ->
let clock = ref (0 :: i :: clock0) in
logf !clock "await fiber %d" i;
logf (TS.tick_get clock) "cur fiber[%d] is some: %b" i
(Option.is_some @@ F.Private_.get_cur ());
let res = F.await f in
logf (TS.tick_get clock) "cur fiber[%d] is some: %b" i
(Option.is_some @@ F.Private_.get_cur ());
F.yield ();
logf (TS.tick_get clock) "res %d = %d" i res)
subs);

logf (TS.tick_get clock) "main fiber done"
in

Expand Down Expand Up @@ -104,8 +115,9 @@ let () =

logf (TS.tick_get clock) "start fibers";
let subs =
let clock0 = !clock in
List.init 10 (fun i ->
let clock = ref (0 :: i :: !clock) in
let clock = ref (0 :: i :: clock0) in
F.spawn ~protect:false @@ fun _n ->
let@ () =
F.with_on_self_cancel (fun _ ->
Expand All @@ -123,7 +135,6 @@ let () =
);

F.check_if_cancelled ();

i)
in

Expand Down

0 comments on commit 6ad8c68

Please sign in to comment.