summaryrefslogtreecommitdiff
path: root/testsuite/tests/lib-scanf-2/tscanf2_master.ml
blob: 7d1e8ee7f8cd62d3fee1e67311ae6441cf410022 (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
(* TEST
 modules = "tscanf2_io.ml";
 include unix;
 readonly_files = "tscanf2_worker.ml";
 reference = "${test_source_directory}/tscanf2.reference";
 hasunix;
 {
   (* The bytecode test *)
   program = "${test_build_directory}/master.byte";
   setup-ocamlc.byte-build-env;
   ocamlc.byte; (* Compiles the master *)
   all_modules = "tscanf2_io.cmo tscanf2_worker.ml";
   program = "${test_build_directory}/worker.byte";
   ocamlc.byte; (* Compiles the worker *)
   check-ocamlc.byte-output;
   program = "${test_build_directory}/master.byte";
   arguments = "${test_build_directory}/worker.byte";
   run;
   check-program-output;
 }{
   (* The native test *)
   program = "${test_build_directory}/master.opt";
   setup-ocamlopt.byte-build-env;
   ocamlopt.byte; (* Compiles the master *)
   all_modules = "tscanf2_io.cmx tscanf2_worker.ml";
   program = "${test_build_directory}/worker.opt";
   ocamlopt.byte; (* Compiles the worker *)
   check-ocamlopt.byte-output;
   program = "${test_build_directory}/master.opt";
   arguments = "${test_build_directory}/worker.opt";
   run;
   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
;;