Skip to content

Commit

Permalink
sockopt: update tests
Browse files Browse the repository at this point in the history
  • Loading branch information
bikallem committed Dec 9, 2022
1 parent d4cef4f commit 74fbf75
Show file tree
Hide file tree
Showing 4 changed files with 20 additions and 20 deletions.
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -437,7 +437,7 @@ We can test it using a mock network:
```ocaml
# Eio_main.run @@ fun _env ->
let net = Eio_mock.Net.make "mocknet" in
let socket = Eio_mock.Flow.make "socket" in
let socket = Eio_mock.Net.stream_socket "socket" in
Eio_mock.Net.on_connect net [`Return socket];
run_client ~net ~addr:(`Tcp (Eio.Net.Ipaddr.V4.loopback, 8080));;
+Connecting to server...
Expand Down Expand Up @@ -473,7 +473,7 @@ This can also be tested on its own using a mock network:
# Eio_main.run @@ fun _env ->
let listening_socket = Eio_mock.Net.listening_socket "tcp/80" in
let mock_addr = `Tcp (Eio.Net.Ipaddr.V4.loopback, 37568) in
let connection = Eio_mock.Flow.make "connection" in
let connection = Eio_mock.Net.stream_socket "connection" in
Eio_mock.Net.on_accept listening_socket [`Return (connection, mock_addr)];
Eio_mock.Flow.on_read connection [
`Return "(packet 1)";
Expand Down
22 changes: 11 additions & 11 deletions lib_eio/mock/eio_mock.mli
Original file line number Diff line number Diff line change
Expand Up @@ -89,27 +89,27 @@ module Flow : sig
| `Read_source_buffer (** Use the {!Eio.Flow.Read_source_buffer} optimisation. *)
]

type t = <
Eio.Flow.two_way;
Eio.Flow.close;
on_read : string Handler.t;
on_copy_bytes : int Handler.t;
set_copy_method : copy_method -> unit;
attach_to_switch : Eio.Switch.t -> unit;
>
class virtual t : ?pp:string Fmt.t -> string -> object
inherit Eio.Flow.two_way
inherit Eio.Flow.close
method on_read : string Handler.t
method on_copy_bytes : int Handler.t
method set_copy_method : copy_method -> unit
method attach_to_switch : Eio.Switch.t -> unit
end

val make : ?pp:string Fmt.t -> string -> t
(** [make label] is a mock Eio flow.
It can be used as a source, sink, or two-way flow.
@param pp Printer to use to display the data. *)

val on_read : t -> string Handler.actions -> unit
val on_read : #t -> string Handler.actions -> unit
(** [on_read t actions] configures the values to return from the mock's [read] function. *)

val on_copy_bytes : t -> int Handler.actions -> unit
val on_copy_bytes : #t -> int Handler.actions -> unit
(** [on_copy_bytes t actions] configures the number of bytes to copy in each iteration. *)

val set_copy_method : t -> copy_method -> unit
val set_copy_method : #t -> copy_method -> unit
(** [set_copy_method t m] configures [t] to use the given method to read from
a source during a copy operation. *)
end
Expand Down
2 changes: 1 addition & 1 deletion tests/mocks.md
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ The server handles a connection:
let net = Eio_mock.Net.make "mocknet" in
let listening_socket = Eio_mock.Net.listening_socket "tcp/80" in
Eio_mock.Net.on_listen net [`Return listening_socket];
let connection = Eio_mock.Flow.make "connection" in
let connection = Eio_mock.Net.stream_socket "connection" in
let addr = `Tcp (Eio.Net.Ipaddr.V4.loopback, 37568) in
Eio_mock.Net.on_accept listening_socket [`Return (connection, addr)];
Eio_mock.Flow.on_read connection [`Return "foo"; `Return "bar"];
Expand Down
12 changes: 6 additions & 6 deletions tests/network.md
Original file line number Diff line number Diff line change
Expand Up @@ -355,7 +355,7 @@ On success, we close the connection immediately:
```ocaml
# Eio_mock.Backend.run @@ fun () ->
let socket = Eio_mock.Net.listening_socket "tcp/80" in
let flow = Eio_mock.Flow.make "connection" in
let flow = Eio_mock.Net.stream_socket "connection" in
let addr = `Tcp (Eio.Net.Ipaddr.V4.loopback, 1234) in
Eio_mock.Net.on_accept socket [`Return (flow, addr)];
Switch.run @@ fun sw ->
Expand All @@ -372,7 +372,7 @@ If the forked fiber fails, we close immediately:
```ocaml
# Eio_mock.Backend.run @@ fun () ->
let socket = Eio_mock.Net.listening_socket "tcp/80" in
let flow = Eio_mock.Flow.make "connection" in
let flow = Eio_mock.Net.stream_socket "connection" in
let addr = `Tcp (Eio.Net.Ipaddr.V4.loopback, 1234) in
Eio_mock.Net.on_accept socket [`Return (flow, addr)];
Switch.run @@ fun sw ->
Expand All @@ -389,7 +389,7 @@ If the fork itself fails, we still close the connection:
```ocaml
# Eio_mock.Backend.run @@ fun () ->
let socket = Eio_mock.Net.listening_socket "tcp/80" in
let flow = Eio_mock.Flow.make "connection" in
let flow = Eio_mock.Net.stream_socket "connection" in
let addr = `Tcp (Eio.Net.Ipaddr.V4.loopback, 1234) in
Eio_mock.Net.on_accept socket [`Return (flow, addr)];
Switch.run @@ fun sw ->
Expand Down Expand Up @@ -630,7 +630,7 @@ First address works:
```ocaml
# Eio_mock.Backend.run @@ fun () ->
Eio_mock.Net.on_getaddrinfo net [`Return [addr1; addr2]];
let mock_flow = Eio_mock.Flow.make "flow" in
let mock_flow = Eio_mock.Net.stream_socket "flow" in
Eio_mock.Net.on_connect net [`Return mock_flow];
Eio.Net.with_tcp_connect ~host:"www.example.com" ~service:"http" net (fun conn ->
let req = "GET / HTTP/1.1\r\nHost:www.example.com:80\r\n\r\n" in
Expand All @@ -650,7 +650,7 @@ Second address works:
```ocaml
# Eio_mock.Backend.run @@ fun () ->
Eio_mock.Net.on_getaddrinfo net [`Return [addr1; addr2]];
let mock_flow = Eio_mock.Flow.make "flow" in
let mock_flow = Eio_mock.Net.stream_socket "flow" in
Eio_mock.Net.on_connect net [`Raise connection_failure;
`Return mock_flow];
Eio.Net.with_tcp_connect ~host:"www.example.com" ~service:"http" net (fun conn ->
Expand Down Expand Up @@ -690,7 +690,7 @@ First attempt times out:
let clock = Eio_mock.Clock.Mono.make () in
let timeout = Eio.Time.Timeout.seconds clock 10. in
Eio_mock.Net.on_getaddrinfo net [`Return [addr1; addr2]];
let mock_flow = Eio_mock.Flow.make "flow" in
let mock_flow = Eio_mock.Net.stream_socket "flow" in
Eio_mock.Net.on_connect net [`Run Fiber.await_cancel; `Return mock_flow];
Fiber.both
(fun () ->
Expand Down

0 comments on commit 74fbf75

Please sign in to comment.