From 101d15f87474384da73e9a577da733085274828e Mon Sep 17 00:00:00 2001 From: Simon Cruanes Date: Tue, 20 Feb 2024 19:55:04 -0500 Subject: [PATCH] fix race conditions in tests --- test/lwt/echo_client.ml | 8 ++++++-- test/lwt/hash_client.ml | 5 ++++- test/lwt/output_echo.expected | 4 ++-- test/lwt/output_hash.expected | 12 +----------- test/lwt/run_hash.sh | 7 ++++--- 5 files changed, 17 insertions(+), 19 deletions(-) diff --git a/test/lwt/echo_client.ml b/test/lwt/echo_client.ml index 8be3a3fe..7143d8be 100644 --- a/test/lwt/echo_client.ml +++ b/test/lwt/echo_client.ml @@ -4,6 +4,7 @@ module Trace = Trace_core let spf = Printf.sprintf let ( let@ ) = ( @@ ) +let lock_stdout = M.Lock.create () let main ~port ~runner ~n ~n_conn () : unit Lwt.t = let@ _sp = Trace.with_span ~__FILE__ ~__LINE__ "main" in @@ -39,7 +40,9 @@ let main ~port ~runner ~n ~n_conn () : unit Lwt.t = (* read back something *) M_lwt.IO_in.really_input ic buf 0 (String.length s); - Printf.printf "read: %s\n%!" (Bytes.sub_string buf 0 (String.length s)); + (let@ () = M.Lock.with_ lock_stdout in + Printf.printf "read: %s\n%!" + (Bytes.sub_string buf 0 (String.length s))); Trace.exit_manual_span _sp; () done; @@ -50,7 +53,8 @@ let main ~port ~runner ~n ~n_conn () : unit Lwt.t = (* if we're the last to exit, resolve the promise *) let n_already_done = Atomic.fetch_and_add all_done 1 in if n_already_done = n_conn - 1 then ( - Printf.printf "all done\n%!"; + (let@ () = M.Lock.with_ lock_stdout in + Printf.printf "all done\n%!"); M.Fut.fulfill prom_exit @@ Ok () ) ) diff --git a/test/lwt/hash_client.ml b/test/lwt/hash_client.ml index 4a927eb6..1138f789 100644 --- a/test/lwt/hash_client.ml +++ b/test/lwt/hash_client.ml @@ -9,6 +9,7 @@ module Str_tbl = Hashtbl.Make (struct end) let ( let@ ) = ( @@ ) +let lock_stdout = M.Lock.create () let main ~port ~runner ~ext ~dir ~n_conn () : unit Lwt.t = let@ _sp = Trace.with_span ~__FILE__ ~__LINE__ "main" in @@ -39,6 +40,7 @@ let main ~port ~runner ~ext ~dir ~n_conn () : unit Lwt.t = let res = M_lwt.run_in_lwt_and_await (fun () -> Lwt_io.read_line ic) in + let@ () = M.Lock.with_ lock_stdout in Printf.printf "%s\n%!" res ) else if Sys.is_directory file then ( @@ -47,7 +49,8 @@ let main ~port ~runner ~ext ~dir ~n_conn () : unit Lwt.t = ~data:(fun () -> [ "d", `String file ]) in - Printf.printf "explore %S\n%!" file; + (let@ () = M.Lock.with_ lock_stdout in + Printf.printf "explore %S\n%!" file); Str_tbl.add seen file (); let d = Sys.readdir file in Array.sort String.compare d; diff --git a/test/lwt/output_echo.expected b/test/lwt/output_echo.expected index 58156d4b..746ebae5 100644 --- a/test/lwt/output_echo.expected +++ b/test/lwt/output_echo.expected @@ -2,7 +2,8 @@ run echo client -p 1235 -n 10 --n-conn=2 -j=4 listening on port 1235 all done connecting to port 1235 -hello 1 +read: hello 1 +read: hello 1 read: hello 1 read: hello 1 read: hello 1 @@ -101,4 +102,3 @@ read: hello 9 read: hello 9 read: hello 9 read: hello 9 -read: read: hello 1 diff --git a/test/lwt/output_hash.expected b/test/lwt/output_hash.expected index 2fca1843..f1bab218 100644 --- a/test/lwt/output_hash.expected +++ b/test/lwt/output_hash.expected @@ -1,4 +1,4 @@ -run hash client -d ../../src --n-conn=2 -j=4 --ext .ml +run hash client -p 1234 -d ../../src --n-conn=2 -j=4 --ext .ml listening on port 1234 ../../src/core/background_thread.ml: 6d5c624dac304cf82ffa6f738516ec9a97aba297 ../../src/core/background_thread.ml: 6d5c624dac304cf82ffa6f738516ec9a97aba297 @@ -82,8 +82,6 @@ listening on port 1234 ../../src/fib/handle.ml: d3c9c3c2c414372e8ce6cd7eb113955292cdda5e ../../src/fib/handle.pp.ml: d3c9c3c2c414372e8ce6cd7eb113955292cdda5e ../../src/fib/handle.pp.ml: d3c9c3c2c414372e8ce6cd7eb113955292cdda5e -../../src/forkjoin/moonpool_forkjoin.ml: bc38ed5d228a85465bbd4e67d1e1f227d57b5fe3 -../../src/forkjoin/moonpool_forkjoin.ml: bc38ed5d228a85465bbd4e67d1e1f227d57b5fe3 ../../src/lwt/IO.ml: 48fae1816121700314aebbdfcca562979dc3ff06 ../../src/lwt/IO.ml: 48fae1816121700314aebbdfcca562979dc3ff06 ../../src/lwt/IO_in.ml: 6badd5ffb8fbbdb53f729b93a478bb007f2494f8 @@ -165,14 +163,6 @@ explore "../../src/fib/.moonpool_fib.objs/byte" explore "../../src/fib/.moonpool_fib.objs/byte" explore "../../src/fib/.moonpool_fib.objs/native" explore "../../src/fib/.moonpool_fib.objs/native" -explore "../../src/forkjoin" -explore "../../src/forkjoin" -explore "../../src/forkjoin/.merlin-conf" -explore "../../src/forkjoin/.merlin-conf" -explore "../../src/forkjoin/.moonpool_forkjoin.objs" -explore "../../src/forkjoin/.moonpool_forkjoin.objs" -explore "../../src/forkjoin/.moonpool_forkjoin.objs/byte" -explore "../../src/forkjoin/.moonpool_forkjoin.objs/byte" explore "../../src/lwt" explore "../../src/lwt" explore "../../src/lwt/.merlin-conf" diff --git a/test/lwt/run_hash.sh b/test/lwt/run_hash.sh index c6085b51..7a14bf47 100755 --- a/test/lwt/run_hash.sh +++ b/test/lwt/run_hash.sh @@ -1,11 +1,12 @@ #!/bin/bash -./hash_server.exe & +PORT=1234 +./hash_server.exe -p $PORT & -echo "run hash client $@" +echo "run hash client -p $PORT $@" export LC_LANG=C export LC_ALL=C -./hash_client.exe $@ | sort +./hash_client.exe -p $PORT $@ | sort kill %1