Skip to content

Commit

Permalink
flambda-backend: Tweak lib-threads/sockets.ml (flaky test) (#2544)
Browse files Browse the repository at this point in the history
* Collect lines and print them at the end.

* Protect reference access.
  • Loading branch information
xclerc authored May 9, 2024
1 parent e90e6c4 commit 8442329
Showing 1 changed file with 12 additions and 4 deletions.
16 changes: 12 additions & 4 deletions testsuite/tests/lib-threads/sockets.ml
Original file line number Diff line number Diff line change
Expand Up @@ -27,16 +27,21 @@ let server sock =
ignore(Thread.create serve_connection s)
done

let mutex = Mutex.create ()
let lines = ref []

let client (addr, msg) =
let sock =
Unix.socket (Unix.domain_of_sockaddr addr) Unix.SOCK_STREAM 0 in
Unix.connect sock addr;
let buf = Bytes.make 1024 ' ' in
ignore(Unix.write_substring sock msg 0 (String.length msg));
let n = Unix.read sock buf 0 (Bytes.length buf) in
print_bytes (Bytes.sub buf 0 n); flush stdout
Mutex.lock mutex;
lines := (Bytes.sub buf 0 n) :: !lines;
Mutex.unlock mutex

let _ =
let () =
let addr = Unix.ADDR_INET(Unix.inet_addr_loopback, 0) in
let sock =
Unix.socket (Unix.domain_of_sockaddr addr) Unix.SOCK_STREAM 0 in
Expand All @@ -45,6 +50,9 @@ let _ =
let addr = Unix.getsockname sock in
Unix.listen sock 5;
ignore (Thread.create server sock);
ignore (Thread.create client (addr, "Client #1\n"));
let client1 = Thread.create client (addr, "Client #1\n") in
Thread.delay 0.05;
client (addr, "Client #2\n")
client (addr, "Client #2\n");
Thread.join client1;
List.iter print_bytes (List.sort Bytes.compare !lines);
flush stdout

0 comments on commit 8442329

Please sign in to comment.