Skip to content

Commit

Permalink
Partially convert TCP tests to cram
Browse files Browse the repository at this point in the history
  • Loading branch information
aantron committed Jan 22, 2024
1 parent 2c5c1ea commit cc7d97c
Show file tree
Hide file tree
Showing 17 changed files with 229 additions and 0 deletions.
6 changes: 6 additions & 0 deletions test/unit/tcp/accepts.ml
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
let () =
Helpers.with_tcp @@ fun tcp ->

Luv.TCP.simultaneous_accepts tcp true |> ok "accepts" @@ fun () ->

print_endline "Ok"
7 changes: 7 additions & 0 deletions test/unit/tcp/bind.ml
Original file line number Diff line number Diff line change
@@ -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"
10 changes: 10 additions & 0 deletions test/unit/tcp/connect_cancel.ml
Original file line number Diff line number Diff line change
@@ -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
11 changes: 11 additions & 0 deletions test/unit/tcp/connect_gc.ml
Original file line number Diff line number Diff line change
@@ -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
11 changes: 11 additions & 0 deletions test/unit/tcp/connect_leak.ml
Original file line number Diff line number Diff line change
@@ -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"
10 changes: 10 additions & 0 deletions test/unit/tcp/connect_sync_error.ml
Original file line number Diff line number Diff line change
@@ -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
11 changes: 11 additions & 0 deletions test/unit/tcp/connect_sync_error_leak.ml
Original file line number Diff line number Diff line change
@@ -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"
38 changes: 38 additions & 0 deletions test/unit/tcp/dune
Original file line number Diff line number Diff line change
@@ -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))
9 changes: 9 additions & 0 deletions test/unit/tcp/econnrefused.ml
Original file line number Diff line number Diff line change
@@ -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
12 changes: 12 additions & 0 deletions test/unit/tcp/getpeername.ml
Original file line number Diff line number Diff line change
@@ -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
12 changes: 12 additions & 0 deletions test/unit/tcp/getsockname.ml
Original file line number Diff line number Diff line change
@@ -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)
25 changes: 25 additions & 0 deletions test/unit/tcp/helpers.ml
Original file line number Diff line number Diff line change
@@ -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
6 changes: 6 additions & 0 deletions test/unit/tcp/keepalive.ml
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
let () =
Helpers.with_tcp @@ fun tcp ->

Luv.TCP.keepalive tcp None |> ok "keepalive" @@ fun () ->

print_endline "Ok"
10 changes: 10 additions & 0 deletions test/unit/tcp/listen_accept.ml
Original file line number Diff line number Diff line change
@@ -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
6 changes: 6 additions & 0 deletions test/unit/tcp/nodelay.ml
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
let () =
Helpers.with_tcp @@ fun tcp ->

Luv.TCP.nodelay tcp true |> ok "nodelay" @@ fun () ->

print_endline "Ok"
42 changes: 42 additions & 0 deletions test/unit/tcp/tcp.t
Original file line number Diff line number Diff line change
@@ -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
3 changes: 3 additions & 0 deletions test/unit/tcp/trivial.ml
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
let () =
Helpers.with_tcp ignore;
print_endline "Ok"

0 comments on commit cc7d97c

Please sign in to comment.