Skip to content

Commit 8442329

Browse files
authored
flambda-backend: Tweak lib-threads/sockets.ml (flaky test) (#2544)
* Collect lines and print them at the end. * Protect reference access.
1 parent e90e6c4 commit 8442329

File tree

1 file changed

+12
-4
lines changed

1 file changed

+12
-4
lines changed

testsuite/tests/lib-threads/sockets.ml

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -27,16 +27,21 @@ let server sock =
2727
ignore(Thread.create serve_connection s)
2828
done
2929

30+
let mutex = Mutex.create ()
31+
let lines = ref []
32+
3033
let client (addr, msg) =
3134
let sock =
3235
Unix.socket (Unix.domain_of_sockaddr addr) Unix.SOCK_STREAM 0 in
3336
Unix.connect sock addr;
3437
let buf = Bytes.make 1024 ' ' in
3538
ignore(Unix.write_substring sock msg 0 (String.length msg));
3639
let n = Unix.read sock buf 0 (Bytes.length buf) in
37-
print_bytes (Bytes.sub buf 0 n); flush stdout
40+
Mutex.lock mutex;
41+
lines := (Bytes.sub buf 0 n) :: !lines;
42+
Mutex.unlock mutex
3843

39-
let _ =
44+
let () =
4045
let addr = Unix.ADDR_INET(Unix.inet_addr_loopback, 0) in
4146
let sock =
4247
Unix.socket (Unix.domain_of_sockaddr addr) Unix.SOCK_STREAM 0 in
@@ -45,6 +50,9 @@ let _ =
4550
let addr = Unix.getsockname sock in
4651
Unix.listen sock 5;
4752
ignore (Thread.create server sock);
48-
ignore (Thread.create client (addr, "Client #1\n"));
53+
let client1 = Thread.create client (addr, "Client #1\n") in
4954
Thread.delay 0.05;
50-
client (addr, "Client #2\n")
55+
client (addr, "Client #2\n");
56+
Thread.join client1;
57+
List.iter print_bytes (List.sort Bytes.compare !lines);
58+
flush stdout

0 commit comments

Comments
 (0)