From 4b64c2a6f1362b65ad94f6f5ac9e1b6ad33fc007 Mon Sep 17 00:00:00 2001 From: Bikal Lem Date: Tue, 8 Nov 2022 15:16:45 +0000 Subject: [PATCH] sockopt: update tests --- README.md | 4 ++-- lib_eio/mock/eio_mock.mli | 22 +++++++++++----------- tests/mocks.md | 2 +- tests/network.md | 12 ++++++------ 4 files changed, 20 insertions(+), 20 deletions(-) diff --git a/README.md b/README.md index cd11482d0..b9f80bbef 100644 --- a/README.md +++ b/README.md @@ -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... @@ -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)"; diff --git a/lib_eio/mock/eio_mock.mli b/lib_eio/mock/eio_mock.mli index 8c2396874..6ad02896c 100644 --- a/lib_eio/mock/eio_mock.mli +++ b/lib_eio/mock/eio_mock.mli @@ -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 diff --git a/tests/mocks.md b/tests/mocks.md index 4b0a7ffcd..14652134f 100644 --- a/tests/mocks.md +++ b/tests/mocks.md @@ -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"]; diff --git a/tests/network.md b/tests/network.md index a2f0c2c0e..efdd08402 100644 --- a/tests/network.md +++ b/tests/network.md @@ -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 -> @@ -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 -> @@ -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 -> @@ -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 @@ -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 -> @@ -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 () ->