acceptor_SUITE.erl 5.1 KB

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