diff --git a/README.md b/README.md index eeafd9091..640baec96 100644 --- a/README.md +++ b/README.md @@ -449,7 +449,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... @@ -485,7 +485,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 3a69d1173..d0f23fba1 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 40a4d68df..44a75e509 100644 --- a/tests/network.md +++ b/tests/network.md @@ -353,7 +353,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 -> @@ -370,7 +370,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 -> @@ -387,7 +387,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 -> @@ -688,7 +688,7 @@ First attempt times out: let clock = Eio_mock.Clock.make () in let timeout = Eio.Time.Timeout.of_s 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 () ->