diff --git a/test/unit/tcp/accepts.ml b/test/unit/tcp/accepts.ml new file mode 100644 index 00000000..17bfc3de --- /dev/null +++ b/test/unit/tcp/accepts.ml @@ -0,0 +1,6 @@ +let () = + Helpers.with_tcp @@ fun tcp -> + + Luv.TCP.simultaneous_accepts tcp true |> ok "accepts" @@ fun () -> + + print_endline "Ok" diff --git a/test/unit/tcp/bind.ml b/test/unit/tcp/bind.ml new file mode 100644 index 00000000..fe3a386a --- /dev/null +++ b/test/unit/tcp/bind.ml @@ -0,0 +1,7 @@ +let () = + Helpers.with_tcp @@ fun tcp -> + + Luv.Sockaddr.ipv4 "127.0.0.1" 5100 |> ok "ipv4" @@ fun address -> + Luv.TCP.bind tcp address |> ok "bind" @@ fun () -> + + print_endline "Ok" diff --git a/test/unit/tcp/connect_cancel.ml b/test/unit/tcp/connect_cancel.ml new file mode 100644 index 00000000..329461ad --- /dev/null +++ b/test/unit/tcp/connect_cancel.ml @@ -0,0 +1,10 @@ +let () = + Helpers.with_tcp @@ fun tcp -> + + Luv.Sockaddr.ipv4 "127.0.0.1" 5107 |> ok "ipv4" @@ fun address -> + Luv.TCP.connect tcp address (fun result -> + result |> error [`ECANCELED] "connect" @@ fun () -> + print_endline "Ok"); + + Luv.Handle.close tcp ignore; + Luv.Loop.run () |> ignore diff --git a/test/unit/tcp/connect_gc.ml b/test/unit/tcp/connect_gc.ml new file mode 100644 index 00000000..38bab660 --- /dev/null +++ b/test/unit/tcp/connect_gc.ml @@ -0,0 +1,11 @@ +(* Fails with a segfault if the binding doesn't retain a reference to the + callback. *) +let () = + Helpers.with_tcp @@ fun tcp -> + + Luv.Sockaddr.ipv4 "127.0.0.1" 5103 |> ok "ipv4" @@ fun address -> + Luv.TCP.connect tcp address (fun _ -> print_endline "Ok"); + + Gc.full_major (); + + Luv.Loop.run () |> ignore diff --git a/test/unit/tcp/connect_leak.ml b/test/unit/tcp/connect_leak.ml new file mode 100644 index 00000000..18ea1956 --- /dev/null +++ b/test/unit/tcp/connect_leak.ml @@ -0,0 +1,11 @@ +let () = + Luv.Sockaddr.ipv4 "127.0.0.1" 5104 |> ok "ipv4" @@ fun address -> + + no_memory_leak begin fun _n -> + Helpers.with_tcp @@ fun tcp -> + let callback = fresh_callback () in + Luv.TCP.connect tcp address (fun _ -> callback ()); + Luv.Loop.run () |> ignore + end; + + print_endline "End" diff --git a/test/unit/tcp/connect_sync_error.ml b/test/unit/tcp/connect_sync_error.ml new file mode 100644 index 00000000..f96de910 --- /dev/null +++ b/test/unit/tcp/connect_sync_error.ml @@ -0,0 +1,10 @@ +let () = + Helpers.with_tcp @@ fun tcp -> + + Luv.Sockaddr.ipv4 "127.0.0.1" 5105 |> ok "ipv4" @@ fun address -> + Luv.TCP.connect tcp address ignore; + Luv.TCP.connect tcp address (fun result -> + result |> error [`EALREADY; `EINVAL] "connect" @@ fun () -> + print_endline "Ok"); + + Luv.Loop.run () |> ignore diff --git a/test/unit/tcp/connect_sync_error_leak.ml b/test/unit/tcp/connect_sync_error_leak.ml new file mode 100644 index 00000000..f994e2a9 --- /dev/null +++ b/test/unit/tcp/connect_sync_error_leak.ml @@ -0,0 +1,11 @@ +let () = + Luv.Sockaddr.ipv4 "127.0.0.1" 5106 |> ok "ipv4" @@ fun address -> + + no_memory_leak begin fun _n -> + Helpers.with_tcp @@ fun tcp -> + Luv.TCP.connect tcp address ignore; + Luv.TCP.connect tcp address ignore; + Luv.Loop.run () |> ignore + end; + + print_endline "End" diff --git a/test/unit/tcp/dune b/test/unit/tcp/dune new file mode 100644 index 00000000..15fb7619 --- /dev/null +++ b/test/unit/tcp/dune @@ -0,0 +1,38 @@ +(cram + (alias runtest-windows) + (deps + trivial.exe + nodelay.exe + keepalive.exe + accepts.exe + bind.exe + getsockname.exe + econnrefused.exe + connect_gc.exe + connect_leak.exe + connect_sync_error.exe + connect_sync_error_leak.exe + connect_cancel.exe + listen_accept.exe + getpeername.exe + )) + +(executables + (names + trivial + nodelay + keepalive + accepts + bind + getsockname + econnrefused + connect_gc + connect_leak + connect_sync_error + connect_sync_error_leak + connect_cancel + listen_accept + getpeername + ) + (libraries luv unit_helpers) + (flags -open Unit_helpers)) diff --git a/test/unit/tcp/econnrefused.ml b/test/unit/tcp/econnrefused.ml new file mode 100644 index 00000000..8c0af7b3 --- /dev/null +++ b/test/unit/tcp/econnrefused.ml @@ -0,0 +1,9 @@ +let () = + Helpers.with_tcp @@ fun tcp -> + + Luv.Sockaddr.ipv4 "127.0.0.1" 5102 |> ok "ipv4" @@ fun address -> + Luv.TCP.connect tcp address (fun result -> + result |> error [`ECONNREFUSED] "connect" @@ fun () -> + print_endline "Ok"); + + Luv.Loop.run () |> ignore diff --git a/test/unit/tcp/getpeername.ml b/test/unit/tcp/getpeername.ml new file mode 100644 index 00000000..741a104b --- /dev/null +++ b/test/unit/tcp/getpeername.ml @@ -0,0 +1,12 @@ +let () = + Helpers.with_server_and_client + ~port:5109 + ~server:begin fun server_tcp accept_tcp -> + Luv.Handle.close server_tcp ignore + end + ~client:begin fun client_tcp address -> + Luv.TCP.getpeername client_tcp |> ok "getpeername" @@ fun address' -> + let address = Luv.Sockaddr.to_string address in + let address' = Luv.Sockaddr.to_string address' in + Printf.printf "%b\n" (address' = address) + end diff --git a/test/unit/tcp/getsockname.ml b/test/unit/tcp/getsockname.ml new file mode 100644 index 00000000..045225fb --- /dev/null +++ b/test/unit/tcp/getsockname.ml @@ -0,0 +1,12 @@ +let () = + Helpers.with_tcp @@ fun tcp -> + + Luv.Sockaddr.ipv4 "127.0.0.1" 5101 |> ok "ipv4" @@ fun address -> + Luv.TCP.bind tcp address |> ok "bind" @@ fun () -> + + Luv.TCP.getsockname tcp |> ok "getsockname" @@ fun address' -> + + let address = Luv.Sockaddr.to_string address in + let address' = Luv.Sockaddr.to_string address' in + + Printf.printf "%b\n" (address' = address) diff --git a/test/unit/tcp/helpers.ml b/test/unit/tcp/helpers.ml new file mode 100644 index 00000000..eccca3b4 --- /dev/null +++ b/test/unit/tcp/helpers.ml @@ -0,0 +1,25 @@ +let with_tcp f = + Luv.TCP.init () |> ok "init" @@ fun tcp -> + f tcp; + Luv.Handle.close tcp ignore + +let with_server_and_client ~port ~server ~client = + Luv.Sockaddr.ipv4 "127.0.0.1" port |> ok "ipv4" @@ fun address -> + + Luv.TCP.init () |> ok "server init" @@ fun server_tcp -> + Luv.TCP.bind server_tcp address |> ok "bind" @@ fun () -> + Luv.Stream.listen server_tcp begin fun result -> + result |> ok "listen" @@ fun () -> + Luv.TCP.init () |> ok "accept init" @@ fun accept_tcp -> + Luv.Stream.accept ~server:server_tcp ~client:accept_tcp + |> ok "accept" @@ fun () -> + server server_tcp accept_tcp + end; + + Luv.TCP.init () |> ok "client init" @@ fun client_tcp -> + Luv.TCP.connect client_tcp address begin fun result -> + result |> ok "connect" @@ fun () -> + client client_tcp address + end; + + Luv.Loop.run () |> ignore diff --git a/test/unit/tcp/keepalive.ml b/test/unit/tcp/keepalive.ml new file mode 100644 index 00000000..2e6b9548 --- /dev/null +++ b/test/unit/tcp/keepalive.ml @@ -0,0 +1,6 @@ +let () = + Helpers.with_tcp @@ fun tcp -> + + Luv.TCP.keepalive tcp None |> ok "keepalive" @@ fun () -> + + print_endline "Ok" diff --git a/test/unit/tcp/listen_accept.ml b/test/unit/tcp/listen_accept.ml new file mode 100644 index 00000000..338f4ead --- /dev/null +++ b/test/unit/tcp/listen_accept.ml @@ -0,0 +1,10 @@ +let () = + Helpers.with_server_and_client + ~port:5108 + ~server:begin fun server_tcp accept_tcp -> + prerr_endline "Accepted"; + Luv.Handle.close server_tcp ignore + end + ~client:begin fun client_tcp _ -> + prerr_endline "Connected" + end diff --git a/test/unit/tcp/nodelay.ml b/test/unit/tcp/nodelay.ml new file mode 100644 index 00000000..f1005706 --- /dev/null +++ b/test/unit/tcp/nodelay.ml @@ -0,0 +1,6 @@ +let () = + Helpers.with_tcp @@ fun tcp -> + + Luv.TCP.nodelay tcp true |> ok "nodelay" @@ fun () -> + + print_endline "Ok" diff --git a/test/unit/tcp/tcp.t b/test/unit/tcp/tcp.t new file mode 100644 index 00000000..fc229deb --- /dev/null +++ b/test/unit/tcp/tcp.t @@ -0,0 +1,42 @@ + $ dune exec ./trivial.exe + Ok + + $ dune exec ./nodelay.exe + Ok + + $ dune exec ./keepalive.exe + Ok + + $ dune exec ./accepts.exe + Ok + + $ dune exec ./bind.exe + Ok + + $ dune exec ./getsockname.exe + true + + $ dune exec ./econnrefused.exe + Ok + + $ dune exec ./connect_gc.exe + Ok + + $ dune exec ./connect_leak.exe + End + + $ dune exec ./connect_sync_error.exe + Ok + + $ dune exec ./connect_sync_error_leak.exe + End + + $ dune exec ./connect_cancel.exe + Ok + + $ dune exec ./listen_accept.exe + Accepted + Connected + + $ dune exec ./getpeername.exe + true diff --git a/test/unit/tcp/trivial.ml b/test/unit/tcp/trivial.ml new file mode 100644 index 00000000..8c792bd3 --- /dev/null +++ b/test/unit/tcp/trivial.ml @@ -0,0 +1,3 @@ +let () = + Helpers.with_tcp ignore; + print_endline "Ok"