summaryrefslogtreecommitdiff
path: root/testsuite/tests/lib-scanf-2/tscanf2_master.ml
blob: d364ceabcdfcfbce6dd4ab8d796ec97bba0b851f (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
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
(* TEST

modules = "tscanf2_io.ml"
* hasunix
include unix
readonly_files = "tscanf2_worker.ml"
reference = "${test_source_directory}/tscanf2.reference"

(* The bytcode test *)

** setup-ocamlc.byte-build-env

program = "${test_build_directory}/master.byte"

*** ocamlc.byte (* Compiles the master *)

**** ocamlc.byte (* Compiles the worker *)

all_modules = "tscanf2_io.cmo tscanf2_worker.ml"

program = "${test_build_directory}/worker.byte"

***** check-ocamlc.byte-output

****** run

program = "${test_build_directory}/master.byte"

arguments = "${test_build_directory}/worker.byte"

******* check-program-output

(* The native test *)

** setup-ocamlopt.byte-build-env

program = "${test_build_directory}/master.opt"

*** ocamlopt.byte (* Compiles the master *)

**** ocamlopt.byte (* Compiles the worker *)

all_modules = "tscanf2_io.cmx tscanf2_worker.ml"

program = "${test_build_directory}/worker.opt"

***** check-ocamlopt.byte-output

****** run

program = "${test_build_directory}/master.opt"

arguments = "${test_build_directory}/worker.opt"

******* check-program-output

*)

(* A very simple master:
   - first launch a worker process,
   - then repeat a random number of times:
     + print the string " Ping" on stderr,
     + send it to the worker,
     + and wait for its answer "-pong",
   - finally send the string "stop" to the worker
     and wait for its answer "OK, bye!"
     and die.

   Use the communication module Tscanf2_io.

   Usage: test_master <worker_name> *)

open Tscanf2_io;;

let worker = Sys.argv.(1);;
let ic, oc = Unix.open_process worker;;
let ib = Scanf.Scanning.from_channel ic;;
let ob = Buffer.create 1024;;

let send_string_ping ob = send_string ob oc " Ping";;
let send_string_stop ob = send_string ob oc "stop";;

let interact i =
  Printf.eprintf " Ping"; flush stderr;
  send_string_ping ob;
  let s = receive_string ib in
  if s <> "-pong" then failwith ("Master: unbound string " ^ s)
;;

begin
(*
  Random.self_init ();
  let n = max (Random.int 8) 1 in
*)
  let n = 8 in
  let rec loop i =
    if i > 0 then (interact i; loop (i - 1)) in
  loop n
end
;;

begin
  send_string_stop ob;
  let ack = receive_string ib in
  if ack = "OK, bye!"
  then (print_endline "Test OK."; exit 0)
  else (print_endline "Test Failed!"; exit 2)
end
;;