diff options
author | Loïc Hoguin <[email protected]> | 2012-08-06 13:39:28 +0200 |
---|---|---|
committer | Loïc Hoguin <[email protected]> | 2012-08-06 13:39:28 +0200 |
commit | 6b354c1124035c54b6648665aafbe7197b34bd0e (patch) | |
tree | d31bdd1e0e6cffe8f31e50b40516e63201c6046d /test/acceptor_SUITE.erl | |
parent | 7d52280c2e3fcc9e1b89435c98ef96d4758aed7a (diff) | |
download | ranch-6b354c1124035c54b6648665aafbe7197b34bd0e.tar.gz ranch-6b354c1124035c54b6648665aafbe7197b34bd0e.tar.bz2 ranch-6b354c1124035c54b6648665aafbe7197b34bd0e.zip |
Make accept asynchronous
Ranch now accepts connection asynchronously through a separate
process. The accept process is linked to the acceptor, calls
accept and does nothing else but send the socket back to the
acceptor. This allows us to receive messages in the acceptor
to handle upgrades instead of polling. This will also allow us
later to make acceptors system processes.
Remove support for connection pools in favor of a simpler
max_connections setting. Connections can be removed from the
count, allowing us to have as many long-lived connections as
we want while still limiting the number of short-lived ones.
Add max_connections, max_connections with long-lived connections,
and upgrade tests.
Diffstat (limited to 'test/acceptor_SUITE.erl')
-rw-r--r-- | test/acceptor_SUITE.erl | 67 |
1 files changed, 66 insertions, 1 deletions
diff --git a/test/acceptor_SUITE.erl b/test/acceptor_SUITE.erl index c1313cc..389f375 100644 --- a/test/acceptor_SUITE.erl +++ b/test/acceptor_SUITE.erl @@ -29,6 +29,9 @@ %% tcp. -export([tcp_echo/1]). +-export([tcp_max_connections/1]). +-export([tcp_max_connections_and_beyond/1]). +-export([tcp_upgrade/1]). %% ct. @@ -37,7 +40,10 @@ all() -> groups() -> [{tcp, [ - tcp_echo + tcp_echo, + tcp_max_connections, + tcp_max_connections_and_beyond, + tcp_upgrade ]}, {ssl, [ ssl_echo ]}]. @@ -100,3 +106,62 @@ tcp_echo(_) -> %% Make sure the listener stopped. {'EXIT', _} = begin catch ranch:get_port(tcp_echo) end, ok. + +tcp_max_connections(_) -> + {ok, _} = ranch:start_listener(tcp_max_connections, 1, + ranch_tcp, [{port, 0}, {max_connections, 10}], + notify_and_wait_protocol, [{msg, connected}, {pid, self()}]), + Port = ranch:get_port(tcp_max_connections), + %% @todo We'll probably want a more direct interface to count_connections. + ListenerPid = ranch_server:lookup_listener(tcp_max_connections), + ok = connect_loop(Port, 11, 150), + 10 = ranch_server:count_connections(ListenerPid), + 10 = receive_loop(connected, 400), + 1 = receive_loop(connected, 1000). + +tcp_max_connections_and_beyond(_) -> + {ok, _} = ranch:start_listener(tcp_max_connections_and_beyond, 1, + ranch_tcp, [{port, 0}, {max_connections, 10}], + remove_conn_and_wait_protocol, [{remove, true}]), + Port = ranch:get_port(tcp_max_connections_and_beyond), + %% @todo We'll probably want a more direct interface to count_connections. + ListenerPid = ranch_server:lookup_listener(tcp_max_connections_and_beyond), + ok = connect_loop(Port, 10, 0), + 0 = ranch_server:count_connections(ListenerPid), + ranch:set_protocol_options(tcp_max_connections_and_beyond, + [{remove, false}]), + receive after 500 -> ok end, + ok = connect_loop(Port, 10, 0), + receive after 500 -> ok end, + 10 = ranch_server:count_connections(ListenerPid). + +tcp_upgrade(_) -> + receive after 20000 -> ok end, + {ok, _} = ranch:start_listener(tcp_upgrade, 1, + ranch_tcp, [{port, 0}], + notify_and_wait_protocol, [{msg, connected}, {pid, self()}]), + Port = ranch:get_port(tcp_upgrade), + ok = connect_loop(Port, 1, 0), + receive connected -> ok after 1000 -> error(timeout) end, + ranch:set_protocol_options(tcp_upgrade, [{msg, upgraded}, {pid, self()}]), + ok = connect_loop(Port, 1, 0), + receive upgraded -> ok after 1000 -> error(timeout) end. + +%% Utility functions. + +connect_loop(_, 0, _) -> + ok; +connect_loop(Port, N, Sleep) -> + {ok, _} = gen_tcp:connect("localhost", Port, + [binary, {active, false}, {packet, raw}]), + receive after Sleep -> ok end, + connect_loop(Port, N - 1, Sleep). + +receive_loop(Message, Timeout) -> + receive_loop(Message, Timeout, 0). +receive_loop(Message, Timeout, N) -> + receive Message -> + receive_loop(Message, Timeout, N + 1) + after Timeout -> + N + end. |