From d8981b586223af4bffb813e1e76c172828b0810a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Lo=C3=AFc=20Hoguin?= Date: Tue, 14 Aug 2012 12:29:36 +0200 Subject: Add tests for {active, once} for both TCP and SSL --- test/acceptor_SUITE.erl | 35 ++++++++++++++++++++++++++++++++++ test/active_echo_protocol.erl | 25 ++++++++++++++++++++++++ test/echo_protocol.erl | 4 +++- test/notify_and_wait_protocol.erl | 1 + test/remove_conn_and_wait_protocol.erl | 1 + 5 files changed, 65 insertions(+), 1 deletion(-) create mode 100644 test/active_echo_protocol.erl diff --git a/test/acceptor_SUITE.erl b/test/acceptor_SUITE.erl index 1c0931b..8966d57 100644 --- a/test/acceptor_SUITE.erl +++ b/test/acceptor_SUITE.erl @@ -26,9 +26,11 @@ %% ssl. -export([ssl_accept_error/1]). +-export([ssl_active_echo/1]). -export([ssl_echo/1]). %% tcp. +-export([tcp_active_echo/1]). -export([tcp_echo/1]). -export([tcp_max_connections/1]). -export([tcp_max_connections_and_beyond/1]). @@ -41,12 +43,14 @@ all() -> groups() -> [{tcp, [ + tcp_active_echo, tcp_echo, tcp_max_connections, tcp_max_connections_and_beyond, tcp_upgrade ]}, {ssl, [ ssl_accept_error, + ssl_active_echo, ssl_echo ]}]. @@ -91,6 +95,23 @@ ssl_accept_error(Config) -> receive after 500 -> ok end, true = is_process_alive(AcceptorPid). +ssl_active_echo(Config) -> + {ok, _} = ranch:start_listener(ssl_active_echo, 1, + ranch_ssl, [{port, 0}, + {certfile, ?config(data_dir, Config) ++ "cert.pem"}], + active_echo_protocol, []), + Port = ranch:get_port(ssl_active_echo), + {ok, Socket} = ssl:connect("localhost", Port, + [binary, {active, false}, {packet, raw}, + {certfile, ?config(data_dir, Config) ++ "cert.pem"}]), + ok = ssl:send(Socket, <<"SSL Ranch is working!">>), + {ok, <<"SSL Ranch is working!">>} = ssl:recv(Socket, 21, 1000), + ok = ranch:stop_listener(ssl_active_echo), + {error, closed} = ssl:recv(Socket, 0, 1000), + %% Make sure the listener stopped. + {'EXIT', _} = begin catch ranch:get_port(ssl_active_echo) end, + ok. + ssl_echo(Config) -> {ok, _} = ranch:start_listener(ssl_echo, 1, ranch_ssl, [{port, 0}, @@ -110,6 +131,20 @@ ssl_echo(Config) -> %% tcp. +tcp_active_echo(_) -> + {ok, _} = ranch:start_listener(tcp_active_echo, 1, + ranch_tcp, [{port, 0}], active_echo_protocol, []), + Port = ranch:get_port(tcp_active_echo), + {ok, Socket} = gen_tcp:connect("localhost", Port, + [binary, {active, false}, {packet, raw}]), + ok = gen_tcp:send(Socket, <<"TCP Ranch is working!">>), + {ok, <<"TCP Ranch is working!">>} = gen_tcp:recv(Socket, 21, 1000), + ok = ranch:stop_listener(tcp_active_echo), + {error, closed} = gen_tcp:recv(Socket, 0, 1000), + %% Make sure the listener stopped. + {'EXIT', _} = begin catch ranch:get_port(tcp_active_echo) end, + ok. + tcp_echo(_) -> {ok, _} = ranch:start_listener(tcp_echo, 1, ranch_tcp, [{port, 0}], echo_protocol, []), diff --git a/test/active_echo_protocol.erl b/test/active_echo_protocol.erl new file mode 100644 index 0000000..ac53d12 --- /dev/null +++ b/test/active_echo_protocol.erl @@ -0,0 +1,25 @@ +-module(active_echo_protocol). + +-export([start_link/4]). +-export([init/4]). + +start_link(ListenerPid, Socket, Transport, Opts) -> + Pid = spawn_link(?MODULE, init, [ListenerPid, Socket, Transport, Opts]), + {ok, Pid}. + +init(ListenerPid, Socket, Transport, _Opts = []) -> + ok = ranch:accept_ack(ListenerPid), + loop(Socket, Transport). + +loop(Socket, Transport) -> + {OK, Closed, Error} = Transport:messages(), + Transport:setopts(Socket, [{active, once}]), + receive + {OK, Socket, Data} -> + Transport:send(Socket, Data), + loop(Socket, Transport); + {Closed, Socket} -> + ok; + {Error, Socket, _} -> + ok = Transport:close(Socket) + end. diff --git a/test/echo_protocol.erl b/test/echo_protocol.erl index 950cb06..6839239 100644 --- a/test/echo_protocol.erl +++ b/test/echo_protocol.erl @@ -1,5 +1,7 @@ -module(echo_protocol). --export([start_link/4, init/4]). + +-export([start_link/4]). +-export([init/4]). start_link(ListenerPid, Socket, Transport, Opts) -> Pid = spawn_link(?MODULE, init, [ListenerPid, Socket, Transport, Opts]), diff --git a/test/notify_and_wait_protocol.erl b/test/notify_and_wait_protocol.erl index 27542f2..c959732 100644 --- a/test/notify_and_wait_protocol.erl +++ b/test/notify_and_wait_protocol.erl @@ -1,4 +1,5 @@ -module(notify_and_wait_protocol). + -export([start_link/4]). -export([init/2]). diff --git a/test/remove_conn_and_wait_protocol.erl b/test/remove_conn_and_wait_protocol.erl index 8fffca7..011bd5d 100644 --- a/test/remove_conn_and_wait_protocol.erl +++ b/test/remove_conn_and_wait_protocol.erl @@ -1,4 +1,5 @@ -module(remove_conn_and_wait_protocol). + -export([start_link/4]). -export([init/2]). -- cgit v1.2.3