acceptor_SUITE.erl 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440
  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. %% misc.
  24. -export([misc_bad_transport/1]).
  25. %% ssl.
  26. -export([ssl_accept_error/1]).
  27. -export([ssl_accept_socket/1]).
  28. -export([ssl_active_echo/1]).
  29. -export([ssl_echo/1]).
  30. %% tcp.
  31. -export([tcp_accept_socket/1]).
  32. -export([tcp_active_echo/1]).
  33. -export([tcp_echo/1]).
  34. -export([tcp_max_connections/1]).
  35. -export([tcp_max_connections_and_beyond/1]).
  36. -export([tcp_set_max_connections/1]).
  37. -export([tcp_infinity_max_connections/1]).
  38. -export([tcp_upgrade/1]).
  39. %% supervisor.
  40. -export([supervisor_clean_restart/1]).
  41. -export([supervisor_clean_child_restart/1]).
  42. -export([supervisor_conns_alive/1]).
  43. %% ct.
  44. all() ->
  45. [{group, tcp}, {group, ssl}, {group, misc}, {group, supervisor}].
  46. groups() ->
  47. [{tcp, [
  48. tcp_accept_socket,
  49. tcp_active_echo,
  50. tcp_echo,
  51. tcp_max_connections,
  52. tcp_infinity_max_connections,
  53. tcp_max_connections_and_beyond,
  54. tcp_set_max_connections,
  55. tcp_upgrade
  56. ]}, {ssl, [
  57. ssl_accept_error,
  58. ssl_accept_socket,
  59. ssl_active_echo,
  60. ssl_echo
  61. ]}, {misc, [
  62. misc_bad_transport
  63. ]}, {supervisor, [
  64. supervisor_clean_restart,
  65. supervisor_clean_child_restart,
  66. supervisor_conns_alive
  67. ]}].
  68. init_per_suite(Config) ->
  69. ok = application:start(ranch),
  70. Config.
  71. end_per_suite(_) ->
  72. application:stop(ranch),
  73. ok.
  74. init_per_group(ssl, Config) ->
  75. application:start(crypto),
  76. application:start(public_key),
  77. application:start(ssl),
  78. Config;
  79. init_per_group(_, Config) ->
  80. Config.
  81. end_per_group(ssl, _) ->
  82. application:stop(ssl),
  83. application:stop(public_key),
  84. application:stop(crypto),
  85. ok;
  86. end_per_group(_, _) ->
  87. ok.
  88. %% misc.
  89. misc_bad_transport(_) ->
  90. {error, badarg} = ranch:start_listener(misc_bad_transport, 1,
  91. bad_transport, [{port, 0}],
  92. echo_protocol, []),
  93. ok.
  94. %% ssl.
  95. ssl_accept_error(Config) ->
  96. {ok, _} = ranch:start_listener(ssl_accept_error, 1,
  97. ranch_ssl, [{port, 0},
  98. {certfile, ?config(data_dir, Config) ++ "cert.pem"}],
  99. echo_protocol, []),
  100. Port = ranch:get_port(ssl_accept_error),
  101. [AcceptorPid] = ets:lookup_element(ranch_server,
  102. {acceptors, ssl_accept_error}, 2),
  103. true = is_process_alive(AcceptorPid),
  104. {ok, Socket} = gen_tcp:connect("localhost", Port,
  105. [binary, {active, false}, {packet, raw}]),
  106. ok = gen_tcp:close(Socket),
  107. receive after 500 -> ok end,
  108. true = is_process_alive(AcceptorPid).
  109. ssl_accept_socket(Config) ->
  110. %%% XXX we can't do the spawn to test the controlling process change
  111. %%% because of the bug in ssl
  112. {ok, S} = ssl:listen(0,
  113. [{certfile, ?config(data_dir, Config) ++ "cert.pem"}, binary,
  114. {active, false}, {packet, raw}, {reuseaddr, true}]),
  115. {ok, _} = ranch:start_listener(ssl_accept_socket, 1,
  116. ranch_ssl, [{socket, S}], echo_protocol, []),
  117. Port = ranch:get_port(ssl_accept_socket),
  118. {ok, Socket} = ssl:connect("localhost", Port,
  119. [binary, {active, false}, {packet, raw},
  120. {certfile, ?config(data_dir, Config) ++ "cert.pem"}]),
  121. ok = ssl:send(Socket, <<"TCP Ranch is working!">>),
  122. {ok, <<"TCP Ranch is working!">>} = ssl:recv(Socket, 21, 1000),
  123. ok = ranch:stop_listener(ssl_accept_socket),
  124. {error, closed} = ssl:recv(Socket, 0, 1000),
  125. %% Make sure the listener stopped.
  126. {'EXIT', _} = begin catch ranch:get_port(ssl_accept_socket) end,
  127. ok.
  128. ssl_active_echo(Config) ->
  129. {ok, _} = ranch:start_listener(ssl_active_echo, 1,
  130. ranch_ssl, [{port, 0},
  131. {certfile, ?config(data_dir, Config) ++ "cert.pem"}],
  132. active_echo_protocol, []),
  133. Port = ranch:get_port(ssl_active_echo),
  134. {ok, Socket} = ssl:connect("localhost", Port,
  135. [binary, {active, false}, {packet, raw},
  136. {certfile, ?config(data_dir, Config) ++ "cert.pem"}]),
  137. ok = ssl:send(Socket, <<"SSL Ranch is working!">>),
  138. {ok, <<"SSL Ranch is working!">>} = ssl:recv(Socket, 21, 1000),
  139. ok = ranch:stop_listener(ssl_active_echo),
  140. {error, closed} = ssl:recv(Socket, 0, 1000),
  141. %% Make sure the listener stopped.
  142. {'EXIT', _} = begin catch ranch:get_port(ssl_active_echo) end,
  143. ok.
  144. ssl_echo(Config) ->
  145. {ok, _} = ranch:start_listener(ssl_echo, 1,
  146. ranch_ssl, [{port, 0},
  147. {certfile, ?config(data_dir, Config) ++ "cert.pem"}],
  148. echo_protocol, []),
  149. Port = ranch:get_port(ssl_echo),
  150. {ok, Socket} = ssl:connect("localhost", Port,
  151. [binary, {active, false}, {packet, raw},
  152. {certfile, ?config(data_dir, Config) ++ "cert.pem"}]),
  153. ok = ssl:send(Socket, <<"SSL Ranch is working!">>),
  154. {ok, <<"SSL Ranch is working!">>} = ssl:recv(Socket, 21, 1000),
  155. ok = ranch:stop_listener(ssl_echo),
  156. {error, closed} = ssl:recv(Socket, 0, 1000),
  157. %% Make sure the listener stopped.
  158. {'EXIT', _} = begin catch ranch:get_port(ssl_echo) end,
  159. ok.
  160. %% tcp.
  161. tcp_accept_socket(_) ->
  162. Ref = make_ref(),
  163. Parent = self(),
  164. spawn(fun() ->
  165. {ok, S} = gen_tcp:listen(0, [binary, {active, false}, {packet, raw},
  166. {reuseaddr, true}]),
  167. {ok, _} = ranch:start_listener(tcp_accept_socket, 1,
  168. ranch_tcp, [{socket, S}], echo_protocol, []),
  169. Parent ! Ref
  170. end),
  171. receive
  172. Ref -> ok
  173. end,
  174. Port = ranch:get_port(tcp_accept_socket),
  175. {ok, Socket} = gen_tcp:connect("localhost", Port,
  176. [binary, {active, false}, {packet, raw}]),
  177. ok = gen_tcp:send(Socket, <<"TCP Ranch is working!">>),
  178. {ok, <<"TCP Ranch is working!">>} = gen_tcp:recv(Socket, 21, 1000),
  179. ok = ranch:stop_listener(tcp_accept_socket),
  180. {error, closed} = gen_tcp:recv(Socket, 0, 1000),
  181. %% Make sure the listener stopped.
  182. {'EXIT', _} = begin catch ranch:get_port(tcp_accept_socket) end,
  183. ok.
  184. tcp_active_echo(_) ->
  185. {ok, _} = ranch:start_listener(tcp_active_echo, 1,
  186. ranch_tcp, [{port, 0}], active_echo_protocol, []),
  187. Port = ranch:get_port(tcp_active_echo),
  188. {ok, Socket} = gen_tcp:connect("localhost", Port,
  189. [binary, {active, false}, {packet, raw}]),
  190. ok = gen_tcp:send(Socket, <<"TCP Ranch is working!">>),
  191. {ok, <<"TCP Ranch is working!">>} = gen_tcp:recv(Socket, 21, 1000),
  192. ok = ranch:stop_listener(tcp_active_echo),
  193. {error, closed} = gen_tcp:recv(Socket, 0, 1000),
  194. %% Make sure the listener stopped.
  195. {'EXIT', _} = begin catch ranch:get_port(tcp_active_echo) end,
  196. ok.
  197. tcp_echo(_) ->
  198. {ok, _} = ranch:start_listener(tcp_echo, 1,
  199. ranch_tcp, [{port, 0}], echo_protocol, []),
  200. Port = ranch:get_port(tcp_echo),
  201. {ok, Socket} = gen_tcp:connect("localhost", Port,
  202. [binary, {active, false}, {packet, raw}]),
  203. ok = gen_tcp:send(Socket, <<"TCP Ranch is working!">>),
  204. {ok, <<"TCP Ranch is working!">>} = gen_tcp:recv(Socket, 21, 1000),
  205. ok = ranch:stop_listener(tcp_echo),
  206. {error, closed} = gen_tcp:recv(Socket, 0, 1000),
  207. %% Make sure the listener stopped.
  208. {'EXIT', _} = begin catch ranch:get_port(tcp_echo) end,
  209. ok.
  210. tcp_max_connections(_) ->
  211. {ok, _} = ranch:start_listener(tcp_max_connections, 1,
  212. ranch_tcp, [{port, 0}, {max_connections, 10}],
  213. notify_and_wait_protocol, [{msg, connected}, {pid, self()}]),
  214. Port = ranch:get_port(tcp_max_connections),
  215. %% @todo We'll probably want a more direct interface to count_connections.
  216. ListenerPid = ranch_server:lookup_listener(tcp_max_connections),
  217. ok = connect_loop(Port, 11, 150),
  218. 10 = ranch_server:count_connections(ListenerPid),
  219. 10 = receive_loop(connected, 400),
  220. 1 = receive_loop(connected, 1000).
  221. tcp_max_connections_and_beyond(_) ->
  222. {ok, _} = ranch:start_listener(tcp_max_connections_and_beyond, 1,
  223. ranch_tcp, [{port, 0}, {max_connections, 10}],
  224. remove_conn_and_wait_protocol, [{remove, true}]),
  225. Port = ranch:get_port(tcp_max_connections_and_beyond),
  226. %% @todo We'll probably want a more direct interface to count_connections.
  227. ListenerPid = ranch_server:lookup_listener(tcp_max_connections_and_beyond),
  228. ok = connect_loop(Port, 10, 0),
  229. 0 = ranch_server:count_connections(ListenerPid),
  230. ranch:set_protocol_options(tcp_max_connections_and_beyond,
  231. [{remove, false}]),
  232. receive after 500 -> ok end,
  233. ok = connect_loop(Port, 10, 0),
  234. receive after 500 -> ok end,
  235. 10 = ranch_server:count_connections(ListenerPid).
  236. tcp_set_max_connections(_) ->
  237. {ok, _} = ranch:start_listener(tcp_set_max_connections, 1,
  238. ranch_tcp, [{port, 0}, {max_connections, 10}],
  239. notify_and_wait_protocol, [{msg, connected}, {pid, self()}]),
  240. Port = ranch:get_port(tcp_set_max_connections),
  241. %% @todo We'll probably want a more direct interface to count_connections.
  242. ListenerPid = ranch_server:lookup_listener(tcp_set_max_connections),
  243. ok = connect_loop(Port, 20, 0),
  244. 10 = ranch_server:count_connections(ListenerPid),
  245. 10 = receive_loop(connected, 1000),
  246. 10 = ranch:get_max_connections(tcp_set_max_connections),
  247. ranch:set_max_connections(tcp_set_max_connections, 20),
  248. 10 = receive_loop(connected, 1000),
  249. 20 = ranch:get_max_connections(tcp_set_max_connections).
  250. tcp_infinity_max_connections(_) ->
  251. {ok, _} = ranch:start_listener(tcp_infinity_max_connections, 1,
  252. ranch_tcp, [{port, 0}, {max_connections, 10}],
  253. notify_and_wait_protocol, [{msg, connected}, {pid, self()}]),
  254. Port = ranch:get_port(tcp_infinity_max_connections),
  255. %% @todo We'll probably want a more direct interface to count_connections.
  256. ListenerPid = ranch_server:lookup_listener(tcp_infinity_max_connections),
  257. ok = connect_loop(Port, 20, 0),
  258. 10 = ranch_server:count_connections(ListenerPid),
  259. 10 = receive_loop(connected, 1000),
  260. 10 = ranch:get_max_connections(tcp_infinity_max_connections),
  261. ranch:set_max_connections(tcp_infinity_max_connections, infinity),
  262. 0 = ranch_server:count_connections(ListenerPid),
  263. infinity = ranch:get_max_connections(tcp_infinity_max_connections),
  264. ranch:set_max_connections(tcp_infinity_max_connections, 10),
  265. 0 = ranch_server:count_connections(ListenerPid),
  266. 10 = receive_loop(connected, 1000),
  267. 10 = ranch_server:count_connections(ListenerPid). % count could be off
  268. tcp_upgrade(_) ->
  269. {ok, _} = ranch:start_listener(tcp_upgrade, 1,
  270. ranch_tcp, [{port, 0}],
  271. notify_and_wait_protocol, [{msg, connected}, {pid, self()}]),
  272. Port = ranch:get_port(tcp_upgrade),
  273. ok = connect_loop(Port, 1, 0),
  274. receive connected -> ok after 1000 -> error(timeout) end,
  275. ranch:set_protocol_options(tcp_upgrade, [{msg, upgraded}, {pid, self()}]),
  276. ok = connect_loop(Port, 1, 0),
  277. receive upgraded -> ok after 1000 -> error(timeout) end.
  278. %% Supervisor tests
  279. supervisor_clean_restart(_) ->
  280. %% There we verify that mature listener death will not let
  281. %% whole supervisor down and also the supervisor itself will
  282. %% restart everything properly.
  283. Ref = supervisor_clean_restart,
  284. NbAcc = 4,
  285. {ok, Pid} = ranch:start_listener(Ref,
  286. NbAcc, ranch_tcp, [{port, 0}], echo_protocol, []),
  287. %% Trace supervisor spawns.
  288. 1 = erlang:trace(Pid, true, [procs, set_on_spawn]),
  289. ListenerPid0 = ranch_server:lookup_listener(Ref),
  290. erlang:exit(ListenerPid0, kill),
  291. receive after 1000 -> ok end,
  292. %% Verify that supervisor is alive
  293. true = is_process_alive(Pid),
  294. %% ...but children are dead.
  295. false = is_process_alive(ListenerPid0),
  296. %% Receive traces from newly started children
  297. ListenerPid = receive {trace, Pid, spawn, Pid1, _} -> Pid1 end,
  298. _ConnSupPid = receive {trace, Pid, spawn, Pid2, _} -> Pid2 end,
  299. AccSupPid = receive {trace, Pid, spawn, Pid3, _} -> Pid3 end,
  300. %% ...and its acceptors.
  301. [receive {trace, AccSupPid, spawn, _Pid, _} -> ok end ||
  302. _ <- lists:seq(1, NbAcc)],
  303. %% No more traces then.
  304. receive
  305. {trace, EPid, spawn, _, _} when EPid == Pid; EPid == AccSupPid ->
  306. error(invalid_restart)
  307. after 1000 -> ok end,
  308. %% Verify that new children registered themselves properly.
  309. ListenerPid = ranch_server:lookup_listener(Ref),
  310. _ = erlang:trace(all, false, [all]),
  311. ok = clean_traces().
  312. supervisor_clean_child_restart(_) ->
  313. %% Then we verify that only parts of the supervision tree
  314. %% restarted in the case of failure.
  315. Ref = supervisor_clean_child_restart,
  316. %% Trace socket allocations.
  317. _ = erlang:trace(new, true, [call]),
  318. 1 = erlang:trace_pattern({ranch_tcp, listen, 1}, [{'_', [], [{return_trace}]}], [global]),
  319. {ok, Pid} = ranch:start_listener(Ref,
  320. 1, ranch_tcp, [{port, 0}], echo_protocol, []),
  321. %% Trace supervisor spawns.
  322. 1 = erlang:trace(Pid, true, [procs, set_on_spawn]),
  323. ListenerPid0 = ranch_server:lookup_listener(Ref),
  324. %% Manually shut the listening socket down.
  325. LSocket = receive
  326. {trace, _, return_from, {ranch_tcp, listen, 1}, {ok, Socket}} ->
  327. Socket
  328. after 0 ->
  329. error(lsocket_unknown)
  330. end,
  331. ok = gen_tcp:close(LSocket),
  332. receive after 1000 -> ok end,
  333. %% Verify that supervisor and its first two children are alive.
  334. true = is_process_alive(Pid),
  335. true = is_process_alive(ListenerPid0),
  336. %% Check that acceptors_sup is restarted properly.
  337. AccSupPid = receive {trace, Pid, spawn, Pid1, _} -> Pid1 end,
  338. AccPid = receive {trace, AccSupPid, spawn, Pid2, _} -> Pid2 end,
  339. receive {trace, AccPid, spawn, _, _} -> ok end,
  340. %% No more traces then.
  341. receive
  342. {trace, _, spawn, _, _} -> error(invalid_restart)
  343. after 1000 -> ok end,
  344. %% Verify that children still registered right.
  345. ListenerPid0 = ranch_server:lookup_listener(Ref),
  346. _ = erlang:trace_pattern({ranch_tcp, listen, 1}, false, []),
  347. _ = erlang:trace(all, false, [all]),
  348. ok = clean_traces(),
  349. ok.
  350. supervisor_conns_alive(_) ->
  351. %% And finally we make sure that in the case of partial failure
  352. %% live connections are not being killed.
  353. Ref = supervisor_conns_alive,
  354. _ = erlang:trace(new, true, [call]),
  355. 1 = erlang:trace_pattern({ranch_tcp, listen, 1}, [{'_', [], [{return_trace}]}], [global]),
  356. {ok, _} = ranch:start_listener(Ref,
  357. 1, ranch_tcp, [{port, 0}], remove_conn_and_wait_protocol, [{remove, false}]),
  358. ok,
  359. %% Get the listener socket
  360. LSocket = receive
  361. {trace, _, return_from, {ranch_tcp, listen, 1}, {ok, S}} ->
  362. S
  363. after 0 ->
  364. error(lsocket_unknown)
  365. end,
  366. TcpPort = ranch:get_port(Ref),
  367. {ok, Socket} = gen_tcp:connect("localhost", TcpPort,
  368. [binary, {active, true}, {packet, raw}]),
  369. %% Shut the socket down
  370. ok = gen_tcp:close(LSocket),
  371. %% Assert that client is still viable.
  372. receive {tcp_closed, _} -> error(closed) after 1500 -> ok end,
  373. ok = gen_tcp:send(Socket, <<"poke">>),
  374. receive {tcp_closed, _} -> ok end,
  375. _ = erlang:trace(all, false, [all]),
  376. ok = clean_traces().
  377. clean_traces() ->
  378. receive
  379. {trace, _, _, _} ->
  380. clean_traces();
  381. {trace, _, _, _, _} ->
  382. clean_traces()
  383. after 0 ->
  384. ok
  385. end.
  386. %% Utility functions.
  387. connect_loop(_, 0, _) ->
  388. ok;
  389. connect_loop(Port, N, Sleep) ->
  390. {ok, _} = gen_tcp:connect("localhost", Port,
  391. [binary, {active, false}, {packet, raw}]),
  392. receive after Sleep -> ok end,
  393. connect_loop(Port, N - 1, Sleep).
  394. receive_loop(Message, Timeout) ->
  395. receive_loop(Message, Timeout, 0).
  396. receive_loop(Message, Timeout, N) ->
  397. receive Message ->
  398. receive_loop(Message, Timeout, N + 1)
  399. after Timeout ->
  400. N
  401. end.