summaryrefslogtreecommitdiff
path: root/testsuite/tests/lib-threads/pr4466.ml
blob: 1e0e36a72c9cd288aa77360168114052993a64c5 (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
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
(* TEST
 include systhreads;
 hassysthreads;
 native;
*)

open Printf

(* Regression test for PR#4466: select timeout with simultaneous read
   and write on socket in Windows. *)

(* Scenario:
     - thread [server] implements a simple 'echo' server on a socket
     - thread [reader] selects then 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
  for i = 1 to 3 do
    let n = Unix.recv s buf 2 (Bytes.length buf - 2) [] in
    if n = 0 then begin
      Unix.close s; raise Thread.Exit
    end else begin
      ignore (Unix.send s buf 0 (n + 2) [])
    end
  done

let reader s =
  let buf = Bytes.make 16 ' ' in
  match Unix.select [s] [] [] 10.0 with
  | (_::_, _, _) ->
      printf "Selected\n%!";
      let n = Unix.recv s buf 0 (Bytes.length buf) [] in
      printf "Data read: %s\n%!" (Bytes.sub_string buf 0 n)
  | ([], _, _) ->
      printf "TIMEOUT\n%!"

let writer s msg =
  ignore (Unix.send_substring s msg 0 (String.length msg) [])

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.2;
  let client =
    Unix.socket (Unix.domain_of_sockaddr addr) Unix.SOCK_STREAM 0 in
  Unix.connect client addr;
  (* Send before select & read *)
  writer client "1111";
  let a = Thread.create reader client in
  Thread.delay 0.1;
  Thread.join a;
  (* Select then send *)
  let a = Thread.create reader client in
  Thread.delay 0.1;
  writer client "2222";
  Thread.join a;
  (* Select then send again *)
  let a = Thread.create reader client in
  Thread.delay 0.1;
  writer client "3333";
  Thread.join a;
  (* Cleanup before exiting *)
  Thread.join tserv