test: add more fork-join tests

This commit is contained in:
Simon Cruanes 2023-06-24 15:04:15 -04:00
parent f46cc4f12c
commit 27ec0f85e6
2 changed files with 39 additions and 1 deletions

View file

@ -1,7 +1,7 @@
(tests (tests
(names t_fib1 t_futs1 t_many t_fib_fork_join (names t_fib1 t_futs1 t_many t_fib_fork_join
t_fib_fork_join_all t_sort) t_fib_fork_join_all t_sort t_fork_join)
(enabled_if (>= %{ocaml_version} 5.0)) (enabled_if (>= %{ocaml_version} 5.0))
(libraries moonpool trace ;tracy-client.trace (libraries moonpool trace ;tracy-client.trace
)) ))

View file

@ -0,0 +1,38 @@
open Moonpool
let pool = Pool.create ~min:4 ()
let () =
let x =
Pool.run_wait_block pool (fun () ->
let x, y =
Fork_join.both
(fun () ->
Thread.delay 0.005;
1)
(fun () ->
Thread.delay 0.005;
2)
in
x + y)
in
assert (x = 3)
let () =
try
Pool.run_wait_block pool (fun () ->
Fork_join.both_ignore
(fun () -> Thread.delay 0.005)
(fun () ->
Thread.delay 0.02;
raise Exit));
failwith "should fail"
with Exit -> ()
let () =
let par_sum =
Pool.run_wait_block pool (fun () ->
Fork_join.all_init 42 (fun i -> i * i) |> List.fold_left ( + ) 0)
in
let exp_sum = List.init 42 (fun x -> x * x) |> List.fold_left ( + ) 0 in
assert (par_sum = exp_sum)