acceptor_SUITE.erl 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184
  1. %% Copyright (c) 2011-2012, Loïc Hoguin <essen@ninenines.eu>
  2. %%
  3. %% Permission to use, copy, modify, and/or distribute this software for any
  4. %% purpose with or without fee is hereby granted, provided that the above
  5. %% copyright notice and this permission notice appear in all copies.
  6. %%
  7. %% THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
  8. %% WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
  9. %% MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
  10. %% ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
  11. %% WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
  12. %% ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
  13. %% OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
  14. -module(acceptor_SUITE).
  15. -include_lib("common_test/include/ct.hrl").
  16. %% ct.
  17. -export([all/0]).
  18. -export([groups/0]).
  19. -export([init_per_suite/1]).
  20. -export([end_per_suite/1]).
  21. -export([init_per_group/2]).
  22. -export([end_per_group/2]).
  23. %% ssl.
  24. -export([ssl_accept_error/1]).
  25. -export([ssl_echo/1]).
  26. %% tcp.
  27. -export([tcp_echo/1]).
  28. -export([tcp_max_connections/1]).
  29. -export([tcp_max_connections_and_beyond/1]).
  30. -export([tcp_upgrade/1]).
  31. %% ct.
  32. all() ->
  33. [{group, tcp}, {group, ssl}].
  34. groups() ->
  35. [{tcp, [
  36. tcp_echo,
  37. tcp_max_connections,
  38. tcp_max_connections_and_beyond,
  39. tcp_upgrade
  40. ]}, {ssl, [
  41. ssl_accept_error,
  42. ssl_echo
  43. ]}].
  44. init_per_suite(Config) ->
  45. application:start(ranch),
  46. Config.
  47. end_per_suite(_) ->
  48. application:stop(ranch),
  49. ok.
  50. init_per_group(ssl, Config) ->
  51. application:start(crypto),
  52. application:start(public_key),
  53. application:start(ssl),
  54. Config;
  55. init_per_group(_, Config) ->
  56. Config.
  57. end_per_group(ssl, _) ->
  58. application:stop(ssl),
  59. application:stop(public_key),
  60. application:stop(crypto),
  61. ok;
  62. end_per_group(_, _) ->
  63. ok.
  64. %% ssl.
  65. ssl_accept_error(Config) ->
  66. {ok, _} = ranch:start_listener(ssl_accept_error, 1,
  67. ranch_ssl, [{port, 0},
  68. {certfile, ?config(data_dir, Config) ++ "cert.pem"}],
  69. echo_protocol, []),
  70. Port = ranch:get_port(ssl_accept_error),
  71. [AcceptorPid] = ets:lookup_element(ranch_server,
  72. {acceptors, ssl_accept_error}, 2),
  73. true = is_process_alive(AcceptorPid),
  74. {ok, Socket} = gen_tcp:connect("localhost", Port,
  75. [binary, {active, false}, {packet, raw}]),
  76. ok = gen_tcp:close(Socket),
  77. receive after 500 -> ok end,
  78. true = is_process_alive(AcceptorPid).
  79. ssl_echo(Config) ->
  80. {ok, _} = ranch:start_listener(ssl_echo, 1,
  81. ranch_ssl, [{port, 0},
  82. {certfile, ?config(data_dir, Config) ++ "cert.pem"}],
  83. echo_protocol, []),
  84. Port = ranch:get_port(ssl_echo),
  85. {ok, Socket} = ssl:connect("localhost", Port,
  86. [binary, {active, false}, {packet, raw},
  87. {certfile, ?config(data_dir, Config) ++ "cert.pem"}]),
  88. ok = ssl:send(Socket, <<"SSL Ranch is working!">>),
  89. {ok, <<"SSL Ranch is working!">>} = ssl:recv(Socket, 21, 1000),
  90. ok = ranch:stop_listener(ssl_echo),
  91. {error, closed} = ssl:recv(Socket, 0, 1000),
  92. %% Make sure the listener stopped.
  93. {'EXIT', _} = begin catch ranch:get_port(ssl_echo) end,
  94. ok.
  95. %% tcp.
  96. tcp_echo(_) ->
  97. {ok, _} = ranch:start_listener(tcp_echo, 1,
  98. ranch_tcp, [{port, 0}], echo_protocol, []),
  99. Port = ranch:get_port(tcp_echo),
  100. {ok, Socket} = gen_tcp:connect("localhost", Port,
  101. [binary, {active, false}, {packet, raw}]),
  102. ok = gen_tcp:send(Socket, <<"TCP Ranch is working!">>),
  103. {ok, <<"TCP Ranch is working!">>} = gen_tcp:recv(Socket, 21, 1000),
  104. ok = ranch:stop_listener(tcp_echo),
  105. {error, closed} = gen_tcp:recv(Socket, 0, 1000),
  106. %% Make sure the listener stopped.
  107. {'EXIT', _} = begin catch ranch:get_port(tcp_echo) end,
  108. ok.
  109. tcp_max_connections(_) ->
  110. {ok, _} = ranch:start_listener(tcp_max_connections, 1,
  111. ranch_tcp, [{port, 0}, {max_connections, 10}],
  112. notify_and_wait_protocol, [{msg, connected}, {pid, self()}]),
  113. Port = ranch:get_port(tcp_max_connections),
  114. %% @todo We'll probably want a more direct interface to count_connections.
  115. ListenerPid = ranch_server:lookup_listener(tcp_max_connections),
  116. ok = connect_loop(Port, 11, 150),
  117. 10 = ranch_server:count_connections(ListenerPid),
  118. 10 = receive_loop(connected, 400),
  119. 1 = receive_loop(connected, 1000).
  120. tcp_max_connections_and_beyond(_) ->
  121. {ok, _} = ranch:start_listener(tcp_max_connections_and_beyond, 1,
  122. ranch_tcp, [{port, 0}, {max_connections, 10}],
  123. remove_conn_and_wait_protocol, [{remove, true}]),
  124. Port = ranch:get_port(tcp_max_connections_and_beyond),
  125. %% @todo We'll probably want a more direct interface to count_connections.
  126. ListenerPid = ranch_server:lookup_listener(tcp_max_connections_and_beyond),
  127. ok = connect_loop(Port, 10, 0),
  128. 0 = ranch_server:count_connections(ListenerPid),
  129. ranch:set_protocol_options(tcp_max_connections_and_beyond,
  130. [{remove, false}]),
  131. receive after 500 -> ok end,
  132. ok = connect_loop(Port, 10, 0),
  133. receive after 500 -> ok end,
  134. 10 = ranch_server:count_connections(ListenerPid).
  135. tcp_upgrade(_) ->
  136. receive after 20000 -> ok end,
  137. {ok, _} = ranch:start_listener(tcp_upgrade, 1,
  138. ranch_tcp, [{port, 0}],
  139. notify_and_wait_protocol, [{msg, connected}, {pid, self()}]),
  140. Port = ranch:get_port(tcp_upgrade),
  141. ok = connect_loop(Port, 1, 0),
  142. receive connected -> ok after 1000 -> error(timeout) end,
  143. ranch:set_protocol_options(tcp_upgrade, [{msg, upgraded}, {pid, self()}]),
  144. ok = connect_loop(Port, 1, 0),
  145. receive upgraded -> ok after 1000 -> error(timeout) end.
  146. %% Utility functions.
  147. connect_loop(_, 0, _) ->
  148. ok;
  149. connect_loop(Port, N, Sleep) ->
  150. {ok, _} = gen_tcp:connect("localhost", Port,
  151. [binary, {active, false}, {packet, raw}]),
  152. receive after Sleep -> ok end,
  153. connect_loop(Port, N - 1, Sleep).
  154. receive_loop(Message, Timeout) ->
  155. receive_loop(Message, Timeout, 0).
  156. receive_loop(Message, Timeout, N) ->
  157. receive Message ->
  158. receive_loop(Message, Timeout, N + 1)
  159. after Timeout ->
  160. N
  161. end.