mirror of
https://github.com/c-cube/moonpool.git
synced 2025-12-05 19:00:33 -05:00
test: wip: tests for Fut+TLS
This commit is contained in:
parent
ed369b6f29
commit
753534b62e
3 changed files with 43 additions and 0 deletions
|
|
@ -12,6 +12,7 @@
|
||||||
t_unfair
|
t_unfair
|
||||||
t_ws_deque
|
t_ws_deque
|
||||||
t_ws_wait
|
t_ws_wait
|
||||||
|
t_fut_tls
|
||||||
t_bounded_queue)
|
t_bounded_queue)
|
||||||
(libraries
|
(libraries
|
||||||
moonpool
|
moonpool
|
||||||
|
|
|
||||||
0
test/t_fut_tls.expected
Normal file
0
test/t_fut_tls.expected
Normal file
42
test/t_fut_tls.ml
Normal file
42
test/t_fut_tls.ml
Normal file
|
|
@ -0,0 +1,42 @@
|
||||||
|
open Moonpool
|
||||||
|
|
||||||
|
let ( let@ ) = ( @@ )
|
||||||
|
|
||||||
|
let k_ints : int list Task_local_storage.key =
|
||||||
|
Task_local_storage.new_key ~init:(fun () -> []) ()
|
||||||
|
|
||||||
|
let rec task idx i : unit Fut.t =
|
||||||
|
let open Fut.Infix in
|
||||||
|
if i = 0 then
|
||||||
|
Fut.return ()
|
||||||
|
else (
|
||||||
|
let l = Task_local_storage.get k_ints in
|
||||||
|
Task_local_storage.set k_ints @@ (((idx * 100) + i) :: l);
|
||||||
|
let* () = task idx (i - 1) in
|
||||||
|
|
||||||
|
let l = Task_local_storage.get k_ints in
|
||||||
|
Task_local_storage.set k_ints @@ (((idx * 1000) + i) :: l);
|
||||||
|
Fut.return ()
|
||||||
|
)
|
||||||
|
|
||||||
|
let run ~on idx = Fut.spawn ~on (fun () -> task idx 10)
|
||||||
|
|
||||||
|
let () =
|
||||||
|
let@ runner = Ws_pool.with_ ~num_threads:4 () in
|
||||||
|
let tasks =
|
||||||
|
List.init 8 (fun idx ->
|
||||||
|
let open Fut.Infix in
|
||||||
|
let+ () = run ~on:runner idx |> Fut.join in
|
||||||
|
(* return final value of TLS *)
|
||||||
|
let l = Task_local_storage.get k_ints in
|
||||||
|
l)
|
||||||
|
in
|
||||||
|
|
||||||
|
let res = List.map Fut.wait_block_exn tasks in
|
||||||
|
List.iteri
|
||||||
|
(fun i l ->
|
||||||
|
Printf.printf "res(%d)=[%s]\n" i
|
||||||
|
(String.concat "," @@ List.map string_of_int l))
|
||||||
|
res;
|
||||||
|
|
||||||
|
()
|
||||||
Loading…
Add table
Reference in a new issue