summaryrefslogtreecommitdiff
path: root/testsuite/tests/lib-threads/pr5325.ml
blob: 9f27f038fddb7dfe33a8abe7d95c29575463f56e (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
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
(* TEST

* hassysthreads
include systhreads
** bytecode
** native

*)

open Printf

(* Regression test for PR#5325: simultaneous read and write on socket
   in Windows. *)

(* Scenario:
     - thread [server] implements a simple 'echo' server on a socket
     - thread [reader] reads from a socket connected to the echo server
       and copies to standard output
     - main program executes [writer], which writes to the same socket
       (the one connected to the echo server)
*)

let server sock =
  let (s, _) = Unix.accept sock in
  let buf = Bytes.make 1024 '>' in
  let n = Unix.read s buf 2 (Bytes.length buf - 2) in
  ignore (Unix.write s buf 0 (n + 2));
  Unix.close s

let reader s =
  let buf = Bytes.make 1024 ' ' in
  let n = Unix.read s buf 0 (Bytes.length buf) in
  print_bytes (Bytes.sub buf 0 n); flush stdout

let writer s msg =
  ignore (Unix.write_substring s msg 0 (String.length msg));
  Unix.shutdown s Unix.SHUTDOWN_SEND

let _ =
  let addr = Unix.ADDR_INET(Unix.inet_addr_loopback, 0) in
  let serv =
    Unix.socket (Unix.domain_of_sockaddr addr) Unix.SOCK_STREAM 0 in
  Unix.setsockopt serv Unix.SO_REUSEADDR true;
  Unix.bind serv addr;
  let addr = Unix.getsockname serv in
  Unix.listen serv 5;
  let tserv = Thread.create server serv in
  Thread.delay 0.5;
  let client =
    Unix.socket (Unix.domain_of_sockaddr addr) Unix.SOCK_STREAM 0 in
  Unix.connect client addr;
  let rd = Thread.create reader client in
  Thread.delay 0.5;
  writer client "Client data\n";
  Thread.join rd;
  Thread.join tserv