Skip to content

Commit

Permalink
added echo test original
Browse files Browse the repository at this point in the history
  • Loading branch information
yminsky committed Jul 19, 2021
1 parent a5a6592 commit 944db32
Show file tree
Hide file tree
Showing 7 changed files with 82 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
(packages core async ppx_jane)
4 changes: 4 additions & 0 deletions book/testing/examples/erroneous/echo_test_original/bin/dune
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
(executable
(name echo)
(libraries core async)
(preprocess (pps ppx_jane)))
27 changes: 27 additions & 0 deletions book/testing/examples/erroneous/echo_test_original/bin/echo.ml
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
open Core
open Async

let run ~uppercase ~port =
let%bind () = Clock.after (Time.Span.of_sec 0.1) in
let host_and_port =
Tcp.Server.create ~on_handler_error:`Raise
(Tcp.Where_to_listen.of_port port) (fun _addr r w ->
Pipe.transfer (Reader.pipe r) (Writer.pipe w)
~f:(if uppercase then String.uppercase else Fn.id))
in
ignore (host_and_port : (Socket.Address.Inet.t, int) Tcp.Server.t Deferred.t);
Deferred.never ()

let () =
Command.async ~summary:"Start an echo server"
Command.Let_syntax.(
let%map_open uppercase =
flag "-uppercase" no_arg
~doc:" Convert to uppercase before echoing back"
and port =
flag "-port"
(optional_with_default 8765 int)
~doc:" Port to listen on (default 8765)"
in
fun () -> run ~uppercase ~port)
|> Command.run
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
(lang dune 2.8)
(name rwo-example)
Empty file.
7 changes: 7 additions & 0 deletions book/testing/examples/erroneous/echo_test_original/test/dune
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
(library
(name echo_test)
(libraries core async expect_test_helpers_async expect_test_helpers_core)
(preprocess
(pps ppx_jane))
(inline_tests (deps ../bin/echo.exe))
)
41 changes: 41 additions & 0 deletions book/testing/examples/erroneous/echo_test_original/test/test.ml
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
open! Core
open! Async

let launch_echo_server ~port =
Process.create_exn
~prog:"../bin/echo.exe"
~args:["-port";Int.to_string port;"-uppercase"]
()

let connect ~port =
let%map (_sock,r,w) =
Tcp.connect
(Tcp.Where_to_connect.of_host_and_port {host="localhost";port})
in
(r,w)

let send_data r w text =
Writer.write w text;
let%bind () = Writer.flushed w in
let%bind line = Reader.read_line r in
(match line with
| `Eof -> print_endline "EOF"
| `Ok line -> print_endline line);
return ()

let cleanup process =
let () = Process.send_signal process Signal.kill in
let%bind (_ : Unix.Exit_or_signal.t) = Process.wait process in
return ()

let%expect_test "test uppercase echo" =
let port = 8081 in
let%bind process = launch_echo_server ~port in
Monitor.protect (fun () ->
let%bind (r,w) = connect ~port in
let%bind () = send_data r w "one two three\n" in
let%bind () = [%expect] in
let%bind () = send_data r w "one 2 three\n" in
let%bind () = [%expect] in
return ())
~finally:(fun () -> cleanup process)

0 comments on commit 944db32

Please sign in to comment.