-
Notifications
You must be signed in to change notification settings - Fork 175
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
7 changed files
with
82 additions
and
0 deletions.
There are no files selected for viewing
1 change: 1 addition & 0 deletions
1
book/testing/examples/erroneous/echo_test_original/.rwo-example
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
(packages core async ppx_jane) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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
27
book/testing/examples/erroneous/echo_test_original/bin/echo.ml
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
2 changes: 2 additions & 0 deletions
2
book/testing/examples/erroneous/echo_test_original/dune-project
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
(lang dune 2.8) | ||
(name rwo-example) |
Empty file.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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
41
book/testing/examples/erroneous/echo_test_original/test/test.ml
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) |