Skip to content

Tweak lib-threads/sockets.ml (flaky test) #2544

New issue

Have a question about this project? # for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “#”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? # to your account

Merged
merged 2 commits into from
May 9, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 12 additions & 4 deletions ocaml/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
Loading