acceptor_SUITE.erl 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219
  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_active_echo/1]).
  26. -export([ssl_echo/1]).
  27. %% tcp.
  28. -export([tcp_active_echo/1]).
  29. -export([tcp_echo/1]).
  30. -export([tcp_max_connections/1]).
  31. -export([tcp_max_connections_and_beyond/1]).
  32. -export([tcp_upgrade/1]).
  33. %% ct.
  34. all() ->
  35. [{group, tcp}, {group, ssl}].
  36. groups() ->
  37. [{tcp, [
  38. tcp_active_echo,
  39. tcp_echo,
  40. tcp_max_connections,
  41. tcp_max_connections_and_beyond,
  42. tcp_upgrade
  43. ]}, {ssl, [
  44. ssl_accept_error,
  45. ssl_active_echo,
  46. ssl_echo
  47. ]}].
  48. init_per_suite(Config) ->
  49. ok = application:start(ranch),
  50. Config.
  51. end_per_suite(_) ->
  52. application:stop(ranch),
  53. ok.
  54. init_per_group(ssl, Config) ->
  55. application:start(crypto),
  56. application:start(public_key),
  57. application:start(ssl),
  58. Config;
  59. init_per_group(_, Config) ->
  60. Config.
  61. end_per_group(ssl, _) ->
  62. application:stop(ssl),
  63. application:stop(public_key),
  64. application:stop(crypto),
  65. ok;
  66. end_per_group(_, _) ->
  67. ok.
  68. %% ssl.
  69. ssl_accept_error(Config) ->
  70. {ok, _} = ranch:start_listener(ssl_accept_error, 1,
  71. ranch_ssl, [{port, 0},
  72. {certfile, ?config(data_dir, Config) ++ "cert.pem"}],
  73. echo_protocol, []),
  74. Port = ranch:get_port(ssl_accept_error),
  75. [AcceptorPid] = ets:lookup_element(ranch_server,
  76. {acceptors, ssl_accept_error}, 2),
  77. true = is_process_alive(AcceptorPid),
  78. {ok, Socket} = gen_tcp:connect("localhost", Port,
  79. [binary, {active, false}, {packet, raw}]),
  80. ok = gen_tcp:close(Socket),
  81. receive after 500 -> ok end,
  82. true = is_process_alive(AcceptorPid).
  83. ssl_active_echo(Config) ->
  84. {ok, _} = ranch:start_listener(ssl_active_echo, 1,
  85. ranch_ssl, [{port, 0},
  86. {certfile, ?config(data_dir, Config) ++ "cert.pem"}],
  87. active_echo_protocol, []),
  88. Port = ranch:get_port(ssl_active_echo),
  89. {ok, Socket} = ssl:connect("localhost", Port,
  90. [binary, {active, false}, {packet, raw},
  91. {certfile, ?config(data_dir, Config) ++ "cert.pem"}]),
  92. ok = ssl:send(Socket, <<"SSL Ranch is working!">>),
  93. {ok, <<"SSL Ranch is working!">>} = ssl:recv(Socket, 21, 1000),
  94. ok = ranch:stop_listener(ssl_active_echo),
  95. {error, closed} = ssl:recv(Socket, 0, 1000),
  96. %% Make sure the listener stopped.
  97. {'EXIT', _} = begin catch ranch:get_port(ssl_active_echo) end,
  98. ok.
  99. ssl_echo(Config) ->
  100. {ok, _} = ranch:start_listener(ssl_echo, 1,
  101. ranch_ssl, [{port, 0},
  102. {certfile, ?config(data_dir, Config) ++ "cert.pem"}],
  103. echo_protocol, []),
  104. Port = ranch:get_port(ssl_echo),
  105. {ok, Socket} = ssl:connect("localhost", Port,
  106. [binary, {active, false}, {packet, raw},
  107. {certfile, ?config(data_dir, Config) ++ "cert.pem"}]),
  108. ok = ssl:send(Socket, <<"SSL Ranch is working!">>),
  109. {ok, <<"SSL Ranch is working!">>} = ssl:recv(Socket, 21, 1000),
  110. ok = ranch:stop_listener(ssl_echo),
  111. {error, closed} = ssl:recv(Socket, 0, 1000),
  112. %% Make sure the listener stopped.
  113. {'EXIT', _} = begin catch ranch:get_port(ssl_echo) end,
  114. ok.
  115. %% tcp.
  116. tcp_active_echo(_) ->
  117. {ok, _} = ranch:start_listener(tcp_active_echo, 1,
  118. ranch_tcp, [{port, 0}], active_echo_protocol, []),
  119. Port = ranch:get_port(tcp_active_echo),
  120. {ok, Socket} = gen_tcp:connect("localhost", Port,
  121. [binary, {active, false}, {packet, raw}]),
  122. ok = gen_tcp:send(Socket, <<"TCP Ranch is working!">>),
  123. {ok, <<"TCP Ranch is working!">>} = gen_tcp:recv(Socket, 21, 1000),
  124. ok = ranch:stop_listener(tcp_active_echo),
  125. {error, closed} = gen_tcp:recv(Socket, 0, 1000),
  126. %% Make sure the listener stopped.
  127. {'EXIT', _} = begin catch ranch:get_port(tcp_active_echo) end,
  128. ok.
  129. tcp_echo(_) ->
  130. {ok, _} = ranch:start_listener(tcp_echo, 1,
  131. ranch_tcp, [{port, 0}], echo_protocol, []),
  132. Port = ranch:get_port(tcp_echo),
  133. {ok, Socket} = gen_tcp:connect("localhost", Port,
  134. [binary, {active, false}, {packet, raw}]),
  135. ok = gen_tcp:send(Socket, <<"TCP Ranch is working!">>),
  136. {ok, <<"TCP Ranch is working!">>} = gen_tcp:recv(Socket, 21, 1000),
  137. ok = ranch:stop_listener(tcp_echo),
  138. {error, closed} = gen_tcp:recv(Socket, 0, 1000),
  139. %% Make sure the listener stopped.
  140. {'EXIT', _} = begin catch ranch:get_port(tcp_echo) end,
  141. ok.
  142. tcp_max_connections(_) ->
  143. {ok, _} = ranch:start_listener(tcp_max_connections, 1,
  144. ranch_tcp, [{port, 0}, {max_connections, 10}],
  145. notify_and_wait_protocol, [{msg, connected}, {pid, self()}]),
  146. Port = ranch:get_port(tcp_max_connections),
  147. %% @todo We'll probably want a more direct interface to count_connections.
  148. ListenerPid = ranch_server:lookup_listener(tcp_max_connections),
  149. ok = connect_loop(Port, 11, 150),
  150. 10 = ranch_server:count_connections(ListenerPid),
  151. 10 = receive_loop(connected, 400),
  152. 1 = receive_loop(connected, 1000).
  153. tcp_max_connections_and_beyond(_) ->
  154. {ok, _} = ranch:start_listener(tcp_max_connections_and_beyond, 1,
  155. ranch_tcp, [{port, 0}, {max_connections, 10}],
  156. remove_conn_and_wait_protocol, [{remove, true}]),
  157. Port = ranch:get_port(tcp_max_connections_and_beyond),
  158. %% @todo We'll probably want a more direct interface to count_connections.
  159. ListenerPid = ranch_server:lookup_listener(tcp_max_connections_and_beyond),
  160. ok = connect_loop(Port, 10, 0),
  161. 0 = ranch_server:count_connections(ListenerPid),
  162. ranch:set_protocol_options(tcp_max_connections_and_beyond,
  163. [{remove, false}]),
  164. receive after 500 -> ok end,
  165. ok = connect_loop(Port, 10, 0),
  166. receive after 500 -> ok end,
  167. 10 = ranch_server:count_connections(ListenerPid).
  168. tcp_upgrade(_) ->
  169. receive after 20000 -> ok end,
  170. {ok, _} = ranch:start_listener(tcp_upgrade, 1,
  171. ranch_tcp, [{port, 0}],
  172. notify_and_wait_protocol, [{msg, connected}, {pid, self()}]),
  173. Port = ranch:get_port(tcp_upgrade),
  174. ok = connect_loop(Port, 1, 0),
  175. receive connected -> ok after 1000 -> error(timeout) end,
  176. ranch:set_protocol_options(tcp_upgrade, [{msg, upgraded}, {pid, self()}]),
  177. ok = connect_loop(Port, 1, 0),
  178. receive upgraded -> ok after 1000 -> error(timeout) end.
  179. %% Utility functions.
  180. connect_loop(_, 0, _) ->
  181. ok;
  182. connect_loop(Port, N, Sleep) ->
  183. {ok, _} = gen_tcp:connect("localhost", Port,
  184. [binary, {active, false}, {packet, raw}]),
  185. receive after Sleep -> ok end,
  186. connect_loop(Port, N - 1, Sleep).
  187. receive_loop(Message, Timeout) ->
  188. receive_loop(Message, Timeout, 0).
  189. receive_loop(Message, Timeout, N) ->
  190. receive Message ->
  191. receive_loop(Message, Timeout, N + 1)
  192. after Timeout ->
  193. N
  194. end.