concraft_test.ml 1.26 KB
let wait_for_server () =
  let rec wait s a =
    try Unix.connect s a
    with _ -> Unix.sleep 1; wait s a in
  let s = Unix.socket Unix.PF_INET Unix.SOCK_STREAM 6 in
  let a = Unix.ADDR_INET (Unix.inet_addr_loopback, 10089) in
  wait s a;
  Unix.shutdown s Unix.SHUTDOWN_SEND;
  Unix.close s

let start_server () =
  let client_out, server_out = Unix.pipe () in
  let client_err, server_err = Unix.pipe () in
  let pid = Unix.create_process "concraft-pl" [|"concraft-pl"; "server"; "--inmodel"; "nkjp-model-0.2.gz"|]
    Unix.stdin server_out server_err in
  List.iter Unix.close [client_out; server_out; client_err; server_err];
  wait_for_server ();
  pid

let stop_server pid =
  Unix.kill pid Sys.sigint

let tag s =
  Unix.open_process_full ("echo \"" ^ s ^ "\" | concraft-pl client")
    [|"PATH=" ^ Sys.getenv "PATH"; "LANG=en_GB.UTF-8"|]

let _ =
  let pid = start_server () in
  let concraft_in, concraft_out, concraft_err = tag "Ala ma kota." in
  try
    while true do
      print_endline @@ input_line concraft_in
    done
  with End_of_file -> ();
  try
    while true do
      print_endline @@ "concraft error message: " ^ input_line concraft_err
    done
  with End_of_file -> ();
  ignore @@ Unix.close_process_full (concraft_in, concraft_out, concraft_err);
  stop_server pid