From c878b1a198aadbd57d1bbbb210cd44a0a7781802 Mon Sep 17 00:00:00 2001 From: Simon Cruanes Date: Mon, 4 Mar 2024 15:49:20 -0500 Subject: [PATCH] test for `Moonpool_fib.main` --- test/fiber/dune | 2 +- test/fiber/t_main.ml | 43 +++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 44 insertions(+), 1 deletion(-) create mode 100644 test/fiber/t_main.ml diff --git a/test/fiber/dune b/test/fiber/dune index 58edc229..bfd6ef88 100644 --- a/test/fiber/dune +++ b/test/fiber/dune @@ -1,7 +1,7 @@ (tests (names - t_fib1 t_fls) + t_fib1 t_fls t_main) (enabled_if (>= %{ocaml_version} 5.0)) (libraries diff --git a/test/fiber/t_main.ml b/test/fiber/t_main.ml new file mode 100644 index 00000000..e0216c55 --- /dev/null +++ b/test/fiber/t_main.ml @@ -0,0 +1,43 @@ +open Moonpool +module F = Moonpool_fib + +let ( let@ ) = ( @@ ) + +let () = + let r = + F.main @@ fun runner -> + let f1 = F.spawn (fun () -> 1) in + let f2 = F.spawn_top ~on:runner (fun () -> 2) in + let f3 = F.spawn (fun () -> F.await f1 + 10) in + let r = F.await f2 + F.await f3 in + assert (r = 13); + r + in + assert (r = 13) + +let () = + (* run fibers in the background, await them in the main thread *) + let@ bg = Fifo_pool.with_ ~num_threads:4 () in + let r = + F.main @@ fun runner -> + let f1 = F.spawn_top ~on:bg (fun () -> 1) in + let f2 = F.spawn_top ~on:runner (fun () -> 2) in + let f3 = F.spawn_top ~on:bg (fun () -> F.await f1 + 10) in + let r = F.await f2 + F.await f3 in + assert (r = 13); + r + in + assert (r = 13) + +let () = + try + let _r = + F.main @@ fun _r -> + let fib = F.spawn (fun () -> failwith "oops") in + F.await fib + in + + assert false + with Failure msg -> + (* Printf.eprintf "got %S\n%!" msg; *) + assert (msg = "oops")