summaryrefslogtreecommitdiff
path: root/testsuite/tests/lib-unix/win-socketpair/test.ml
blob: cecd4a54c93636d5a4e22df68754a46d970cb168 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
(* TEST
 script = "sh ${test_source_directory}/has-afunix.sh";
 libwin32unix;
 include systhreads;
 hassysthreads;
 script;
 {
   output = "${test_build_directory}/program-output";
   stdout = "${output}";
   bytecode;
 }{
   output = "${test_build_directory}/program-output";
   stdout = "${output}";
   native;
 }
*)

let peer id fd =
  let msg = Bytes.of_string (Printf.sprintf "%d" id) in
  ignore (Unix.write fd msg 0 (Bytes.length msg));
  ignore (Unix.read fd msg 0 (Bytes.length msg));
  let expected = Bytes.of_string (Printf.sprintf "%d" (if id = 0 then 1 else 0)) in
  if msg = expected then
    Printf.printf "Ok\n%!"
  else
    Printf.printf "%d: %s\n%!" id (Bytes.to_string msg);
  flush_all ()

let () =
  let fd0, fd1 = Unix.socketpair Unix.PF_UNIX Unix.SOCK_STREAM 0 in
  let t0, t1 = Thread.create (peer 0) fd0, Thread.create (peer 1) fd1 in
  Thread.join t0; Thread.join t1;
  Unix.close fd0; Unix.close fd1