add test for await

This commit is contained in:
Simon Cruanes 2023-06-19 15:50:40 -04:00
parent 62777e1112
commit f98bcf2f08
No known key found for this signature in database
GPG key ID: EBFFF6F283F3A2B4
2 changed files with 30 additions and 1 deletions

View file

@ -1,5 +1,5 @@
(tests
(names t_fib1 t_futs1)
(names t_fib1 t_futs1 t_many)
(enabled_if (>= %{ocaml_version} 5.0))
(libraries moonpool trace tracy-client.trace))

29
test/await/t_many.ml Normal file
View file

@ -0,0 +1,29 @@
open Moonpool
let pool = Pool.create ~min:4 ()
let run () =
let t1 = Unix.gettimeofday () in
let n = 1_000_000 in
let n_tasks = 10 in
let task () =
let l = List.init n (fun x -> Fut.spawn ~on:pool (fun () -> x)) in
Fut.spawn ~on:pool (fun () ->
List.fold_left
(fun n x ->
let _res = Fut.await_exn x in
n + 1)
0 l)
in
let futs =
List.init n_tasks (fun _ -> Fut.spawn ~on:pool task |> Fut.join ~on:pool)
in
let lens = List.map Fut.wait_block_exn futs in
Printf.printf "awaited %d items (%d times)\n%!" (List.hd lens) n_tasks;
Printf.printf "in %.4fs\n%!" (Unix.gettimeofday () -. t1);
assert (List.for_all (fun s -> s = n) lens)
let () = run ()