acceptor_SUITE.erl 26 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718
  1. %% Copyright (c) 2011-2016, 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. -compile(export_all).
  16. -import(ct_helper, [doc/1]).
  17. -import(ct_helper, [name/0]).
  18. %% ct.
  19. all() ->
  20. [{group, tcp}, {group, ssl}, {group, misc}, {group, supervisor}].
  21. groups() ->
  22. [{tcp, [
  23. tcp_accept_socket,
  24. tcp_active_echo,
  25. tcp_echo,
  26. tcp_inherit_options,
  27. tcp_max_connections,
  28. tcp_max_connections_and_beyond,
  29. tcp_max_connections_infinity,
  30. tcp_remove_connections,
  31. tcp_set_max_connections,
  32. tcp_set_max_connections_clean,
  33. tcp_upgrade,
  34. tcp_error_eaddrinuse,
  35. tcp_error_eacces
  36. ]}, {ssl, [
  37. ssl_accept_error,
  38. ssl_accept_socket,
  39. ssl_active_echo,
  40. ssl_echo,
  41. ssl_sni_echo,
  42. ssl_sni_fail,
  43. ssl_error_eaddrinuse,
  44. ssl_error_no_cert,
  45. ssl_error_eacces
  46. ]}, {misc, [
  47. misc_bad_transport,
  48. misc_bad_transport_options,
  49. misc_info
  50. ]}, {supervisor, [
  51. connection_type_supervisor,
  52. connection_type_supervisor_separate_from_connection,
  53. supervisor_clean_child_restart,
  54. supervisor_clean_conns_sup_restart,
  55. supervisor_clean_restart,
  56. supervisor_conns_alive,
  57. supervisor_protocol_start_link_crash,
  58. supervisor_server_recover_state
  59. ]}].
  60. %% misc.
  61. misc_bad_transport(_) ->
  62. doc("Reject invalid transport modules."),
  63. {error, badarg} = ranch:start_listener(misc_bad_transport, 1,
  64. bad_transport, [], echo_protocol, []),
  65. ok.
  66. misc_bad_transport_options(_) ->
  67. doc("Ignore invalid transport options."),
  68. {ok, _} = ranch:start_listener(misc_bad_transport, 1,
  69. ranch_tcp, [binary, {packet, 4}, <<"garbage">>, raw, backlog], echo_protocol, []),
  70. ok.
  71. misc_info(_) ->
  72. doc("Information about listeners."),
  73. %% Open a listener with a few connections.
  74. {ok, Pid1} = ranch:start_listener({misc_info, tcp}, 1, ranch_tcp, [],
  75. remove_conn_and_wait_protocol, [{remove, true, 2500}]),
  76. Port1 = ranch:get_port({misc_info, tcp}),
  77. %% Open a few more listeners with different arguments.
  78. {ok, Pid2} = ranch:start_listener({misc_info, act}, 2, ranch_tcp, [], active_echo_protocol, {}),
  79. Port2 = ranch:get_port({misc_info, act}),
  80. ranch:set_max_connections({misc_info, act}, infinity),
  81. Opts = ct_helper:get_certs_from_ets(),
  82. {ok, Pid3} = ranch:start_listener({misc_info, ssl}, 3, ranch_ssl, Opts, echo_protocol, [{}]),
  83. Port3 = ranch:get_port({misc_info, ssl}),
  84. %% Open 5 connections, 3 removed from the count.
  85. {ok, _} = gen_tcp:connect("localhost", Port1, [binary, {active, false}, {packet, raw}]),
  86. {ok, _} = gen_tcp:connect("localhost", Port1, [binary, {active, false}, {packet, raw}]),
  87. {ok, _} = gen_tcp:connect("localhost", Port1, [binary, {active, false}, {packet, raw}]),
  88. receive after 250 -> ok end,
  89. ranch:set_protocol_options({misc_info, tcp}, [{remove, false, 2500}]),
  90. receive after 250 -> ok end,
  91. {ok, _} = gen_tcp:connect("localhost", Port1, [binary, {active, false}, {packet, raw}]),
  92. {ok, _} = gen_tcp:connect("localhost", Port1, [binary, {active, false}, {packet, raw}]),
  93. receive after 250 -> ok end,
  94. %% Confirm the info returned by Ranch is correct.
  95. [
  96. {{misc_info, act}, [
  97. {pid, Pid2},
  98. {ip, _},
  99. {port, Port2},
  100. {num_acceptors, 2},
  101. {max_connections, infinity}, %% Option was modified.
  102. {active_connections, 0},
  103. {all_connections, 0},
  104. {transport, ranch_tcp},
  105. {transport_options, []},
  106. {protocol, active_echo_protocol},
  107. {protocol_options, {}}
  108. ]},
  109. {{misc_info, ssl}, [
  110. {pid, Pid3},
  111. {ip, _},
  112. {port, Port3},
  113. {num_acceptors, 3},
  114. {max_connections, 1024},
  115. {active_connections, 0},
  116. {all_connections, 0},
  117. {transport, ranch_ssl},
  118. {transport_options, Opts},
  119. {protocol, echo_protocol},
  120. {protocol_options, [{}]}
  121. ]},
  122. {{misc_info, tcp}, [
  123. {pid, Pid1},
  124. {ip, _},
  125. {port, Port1},
  126. {num_acceptors, 1},
  127. {max_connections, 1024},
  128. {active_connections, 2},
  129. {all_connections, 5},
  130. {transport, ranch_tcp},
  131. {transport_options, []},
  132. {protocol, remove_conn_and_wait_protocol},
  133. {protocol_options, [{remove, false, 2500}]} %% Option was modified.
  134. ]}
  135. ] = lists:sort([L || L={{misc_info, _}, _} <- ranch:info()]),
  136. %% Get acceptors.
  137. [_] = ranch:procs({misc_info, tcp}, acceptors),
  138. [_, _] = ranch:procs({misc_info, act}, acceptors),
  139. [_, _, _] = ranch:procs({misc_info, ssl}, acceptors),
  140. %% Get connections.
  141. [_, _, _, _, _] = ranch:procs({misc_info, tcp}, connections),
  142. [] = ranch:procs({misc_info, act}, connections),
  143. [] = ranch:procs({misc_info, ssl}, connections),
  144. ok.
  145. %% ssl.
  146. ssl_accept_error(_) ->
  147. doc("Acceptor must not crash if client disconnects in the middle of SSL handshake."),
  148. Name = name(),
  149. Opts = ct_helper:get_certs_from_ets(),
  150. {ok, ListenerSup} = ranch:start_listener(Name, 1, ranch_ssl, Opts, echo_protocol, []),
  151. Port = ranch:get_port(Name),
  152. ListenerSupChildren = supervisor:which_children(ListenerSup),
  153. {_, AcceptorsSup, _, _} = lists:keyfind(ranch_acceptors_sup, 1, ListenerSupChildren),
  154. [{{acceptor, _, _}, AcceptorPid, _, _}] = supervisor:which_children(AcceptorsSup),
  155. true = is_process_alive(AcceptorPid),
  156. {ok, Socket} = gen_tcp:connect("localhost", Port, [binary, {active, false}, {packet, raw}]),
  157. ok = gen_tcp:close(Socket),
  158. receive after 500 -> ok end,
  159. true = is_process_alive(AcceptorPid),
  160. ok = ranch:stop_listener(Name).
  161. ssl_accept_socket(_) ->
  162. doc("Ensure that listener can use an externally opened SSL listen socket."),
  163. Name = name(),
  164. Opts = ct_helper:get_certs_from_ets(),
  165. {ok, S} = ssl:listen(0, [binary, {active, false}, {packet, raw}, {reuseaddr, true}|Opts]),
  166. {ok, _} = ranch:start_listener(Name, 1, ranch_ssl, [{socket, S}], echo_protocol, []),
  167. Port = ranch:get_port(Name),
  168. {ok, Socket} = ssl:connect("localhost", Port, [binary, {active, false}, {packet, raw}]),
  169. ok = ssl:send(Socket, <<"TCP Ranch is working!">>),
  170. {ok, <<"TCP Ranch is working!">>} = ssl:recv(Socket, 21, 1000),
  171. ok = ranch:stop_listener(Name),
  172. {error, closed} = ssl:recv(Socket, 0, 1000),
  173. %% Make sure the listener stopped.
  174. {'EXIT', _} = begin catch ranch:get_port(Name) end,
  175. ok.
  176. ssl_active_echo(_) ->
  177. doc("Ensure that active mode works with SSL transport."),
  178. Name = name(),
  179. Opts = ct_helper:get_certs_from_ets(),
  180. {ok, _} = ranch:start_listener(Name, 1, ranch_ssl, Opts, active_echo_protocol, []),
  181. Port = ranch:get_port(Name),
  182. {ok, Socket} = ssl:connect("localhost", Port, [binary, {active, false}, {packet, raw}]),
  183. ok = ssl:send(Socket, <<"SSL Ranch is working!">>),
  184. {ok, <<"SSL Ranch is working!">>} = ssl:recv(Socket, 21, 1000),
  185. ok = ranch:stop_listener(Name),
  186. {error, closed} = ssl:recv(Socket, 0, 1000),
  187. %% Make sure the listener stopped.
  188. {'EXIT', _} = begin catch ranch:get_port(Name) end,
  189. ok.
  190. ssl_echo(_) ->
  191. doc("Ensure that passive mode works with SSL transport."),
  192. Name = name(),
  193. Opts = ct_helper:get_certs_from_ets(),
  194. {ok, _} = ranch:start_listener(Name, 1, ranch_ssl, Opts, echo_protocol, []),
  195. Port = ranch:get_port(Name),
  196. {ok, Socket} = ssl:connect("localhost", Port, [binary, {active, false}, {packet, raw}]),
  197. ok = ssl:send(Socket, <<"SSL Ranch is working!">>),
  198. {ok, <<"SSL Ranch is working!">>} = ssl:recv(Socket, 21, 1000),
  199. ok = ranch:stop_listener(Name),
  200. {error, closed} = ssl:recv(Socket, 0, 1000),
  201. %% Make sure the listener stopped.
  202. {'EXIT', _} = begin catch ranch:get_port(Name) end,
  203. ok.
  204. ssl_sni_echo(_) ->
  205. case application:get_key(ssl, vsn) of
  206. {ok, Vsn} when Vsn >= "7.0" ->
  207. do_ssl_sni_echo();
  208. _ ->
  209. {skip, "No SNI support."}
  210. end.
  211. do_ssl_sni_echo() ->
  212. doc("Ensure that SNI works with SSL transport."),
  213. Name = name(),
  214. Opts = ct_helper:get_certs_from_ets(),
  215. {ok, _} = ranch:start_listener(Name, 1, ranch_ssl, [{sni_hosts, [{"localhost", Opts}]}], echo_protocol, []),
  216. Port = ranch:get_port(Name),
  217. {ok, Socket} = ssl:connect("localhost", Port, [binary, {active, false}, {packet, raw}]),
  218. ok = ssl:send(Socket, <<"SSL Ranch is working!">>),
  219. {ok, <<"SSL Ranch is working!">>} = ssl:recv(Socket, 21, 1000),
  220. ok = ranch:stop_listener(Name),
  221. {error, closed} = ssl:recv(Socket, 0, 1000),
  222. %% Make sure the listener stopped.
  223. {'EXIT', _} = begin catch ranch:get_port(Name) end,
  224. ok.
  225. ssl_sni_fail(_) ->
  226. case application:get_key(ssl, vsn) of
  227. {ok, Vsn} when Vsn >= "7.0" ->
  228. do_ssl_sni_fail();
  229. _ ->
  230. {skip, "No SNI support."}
  231. end.
  232. do_ssl_sni_fail() ->
  233. doc("Ensure that connection fails when host is not in SNI list."),
  234. Name = name(),
  235. Opts = ct_helper:get_certs_from_ets(),
  236. {ok, _} = ranch:start_listener(Name, 1, ranch_ssl, [{sni_hosts, [{"pouet", Opts}]}], echo_protocol, []),
  237. Port = ranch:get_port(Name),
  238. {error, _} = ssl:connect("localhost", Port, [binary, {active, false}, {packet, raw}]),
  239. ok = ranch:stop_listener(Name),
  240. %% Make sure the listener stopped.
  241. {'EXIT', _} = begin catch ranch:get_port(Name) end,
  242. ok.
  243. ssl_error_eaddrinuse(_) ->
  244. doc("Ensure that failure due to an eaddrinuse returns a compact readable error."),
  245. Name = name(),
  246. Opts = ct_helper:get_certs_from_ets(),
  247. {ok, _} = ranch:start_listener(Name, 1, ranch_ssl, Opts, active_echo_protocol, []),
  248. Port = ranch:get_port(Name),
  249. {error, eaddrinuse} = ranch:start_listener({Name, fails}, 1,
  250. ranch_ssl, [{port, Port}|Opts], active_echo_protocol, []),
  251. ok = ranch:stop_listener(Name),
  252. %% Make sure the listener stopped.
  253. {'EXIT', _} = begin catch ranch:get_port(Name) end,
  254. ok.
  255. ssl_error_no_cert(_) ->
  256. doc("Ensure that failure due to missing certificate returns a compact readable error."),
  257. {error, no_cert} = ranch:start_listener(name(), 1, ranch_ssl, [], active_echo_protocol, []),
  258. ok.
  259. ssl_error_eacces(_) ->
  260. doc("Ensure that failure due to an eacces returns a compact readable error."),
  261. Name = name(),
  262. Opts = ct_helper:get_certs_from_ets(),
  263. {error, eacces} = ranch:start_listener(Name, 1,
  264. ranch_ssl, [{port, 283}|Opts], active_echo_protocol, []),
  265. ok.
  266. %% tcp.
  267. tcp_accept_socket(_) ->
  268. doc("Ensure that listener can use an externally opened TCP listen socket."),
  269. Name = name(),
  270. {ok, S} = gen_tcp:listen(0, [binary, {active, false}, {packet, raw}, {reuseaddr, true}]),
  271. {ok, _} = ranch:start_listener(Name, 1, ranch_tcp, [{socket, S}], echo_protocol, []),
  272. Port = ranch:get_port(Name),
  273. {ok, Socket} = gen_tcp:connect("localhost", Port, [binary, {active, false}, {packet, raw}]),
  274. ok = gen_tcp:send(Socket, <<"TCP Ranch is working!">>),
  275. {ok, <<"TCP Ranch is working!">>} = gen_tcp:recv(Socket, 21, 1000),
  276. ok = ranch:stop_listener(Name),
  277. {error, closed} = gen_tcp:recv(Socket, 0, 1000),
  278. %% Make sure the listener stopped.
  279. {'EXIT', _} = begin catch ranch:get_port(Name) end,
  280. ok.
  281. tcp_active_echo(_) ->
  282. doc("Ensure that active mode works with TCP transport."),
  283. Name = name(),
  284. {ok, _} = ranch:start_listener(Name, 1, ranch_tcp, [], active_echo_protocol, []),
  285. Port = ranch:get_port(Name),
  286. {ok, Socket} = gen_tcp:connect("localhost", Port, [binary, {active, false}, {packet, raw}]),
  287. ok = gen_tcp:send(Socket, <<"TCP Ranch is working!">>),
  288. {ok, <<"TCP Ranch is working!">>} = gen_tcp:recv(Socket, 21, 1000),
  289. ok = ranch:stop_listener(Name),
  290. {error, closed} = gen_tcp:recv(Socket, 0, 1000),
  291. %% Make sure the listener stopped.
  292. {'EXIT', _} = begin catch ranch:get_port(Name) end,
  293. ok.
  294. tcp_echo(_) ->
  295. doc("Ensure that passive mode works with TCP transport."),
  296. Name = name(),
  297. {ok, _} = ranch:start_listener(Name, 1, ranch_tcp, [], echo_protocol, []),
  298. Port = ranch:get_port(Name),
  299. {ok, Socket} = gen_tcp:connect("localhost", Port, [binary, {active, false}, {packet, raw}]),
  300. ok = gen_tcp:send(Socket, <<"TCP Ranch is working!">>),
  301. {ok, <<"TCP Ranch is working!">>} = gen_tcp:recv(Socket, 21, 1000),
  302. ok = ranch:stop_listener(Name),
  303. {error, closed} = gen_tcp:recv(Socket, 0, 1000),
  304. %% Make sure the listener stopped.
  305. {'EXIT', _} = begin catch ranch:get_port(Name) end,
  306. ok.
  307. tcp_inherit_options(_) ->
  308. doc("Ensure TCP options are inherited in the protocol."),
  309. Name = name(),
  310. Opts = [{nodelay, false}, {send_timeout_close, false}],
  311. {ok, _} = ranch:start_listener(Name, 4, ranch_tcp, Opts, check_tcp_options, [{pid, self()} | Opts]),
  312. Port = ranch:get_port(Name),
  313. {ok, Socket} = gen_tcp:connect("localhost", Port, [binary, {active, true}, {packet, raw}]),
  314. receive checked -> ok after 1000 -> error(timeout) end,
  315. ok = gen_tcp:close(Socket),
  316. ok = ranch:stop_listener(Name).
  317. tcp_max_connections(_) ->
  318. doc("Ensure the max_connections option actually limits connections."),
  319. Name = name(),
  320. {ok, _} = ranch:start_listener(Name, 1,
  321. ranch_tcp, [{max_connections, 10}],
  322. notify_and_wait_protocol, [{msg, connected}, {pid, self()}]),
  323. Port = ranch:get_port(Name),
  324. ok = connect_loop(Port, 11, 150),
  325. 10 = ranch_server:count_connections(Name),
  326. 10 = receive_loop(connected, 400),
  327. 1 = receive_loop(connected, 1000),
  328. ok = ranch:stop_listener(Name).
  329. tcp_max_connections_and_beyond(_) ->
  330. doc("Ensure the max_connections option works when connections are removed from the count."),
  331. Name = name(),
  332. {ok, _} = ranch:start_listener(Name, 1,
  333. ranch_tcp, [{max_connections, 10}],
  334. remove_conn_and_wait_protocol, [{remove, true, 2500}]),
  335. Port = ranch:get_port(Name),
  336. ok = connect_loop(Port, 10, 0),
  337. receive after 250 -> ok end,
  338. 0 = ranch_server:count_connections(Name),
  339. 10 = length(supervisor:which_children(ranch_server:get_connections_sup(Name))),
  340. Counts = supervisor:count_children(ranch_server:get_connections_sup(Name)),
  341. {_, 1} = lists:keyfind(specs, 1, Counts),
  342. {_, 0} = lists:keyfind(supervisors, 1, Counts),
  343. {_, 10} = lists:keyfind(active, 1, Counts),
  344. {_, 10} = lists:keyfind(workers, 1, Counts),
  345. ranch:set_protocol_options(Name, [{remove, false, 2500}]),
  346. receive after 250 -> ok end,
  347. ok = connect_loop(Port, 10, 0),
  348. receive after 250 -> ok end,
  349. 10 = ranch_server:count_connections(Name),
  350. 20 = length(supervisor:which_children(ranch_server:get_connections_sup(Name))),
  351. Counts2 = supervisor:count_children(ranch_server:get_connections_sup(Name)),
  352. {_, 20} = lists:keyfind(active, 1, Counts2),
  353. {_, 20} = lists:keyfind(workers, 1, Counts2),
  354. ok = ranch:stop_listener(Name).
  355. tcp_max_connections_infinity(_) ->
  356. doc("Set the max_connections option from 10 to infinity and back to 10."),
  357. Name = name(),
  358. {ok, _} = ranch:start_listener(Name, 1,
  359. ranch_tcp, [{max_connections, 10}],
  360. notify_and_wait_protocol, [{msg, connected}, {pid, self()}]),
  361. Port = ranch:get_port(Name),
  362. ok = connect_loop(Port, 20, 0),
  363. 10 = ranch_server:count_connections(Name),
  364. 10 = receive_loop(connected, 1000),
  365. 10 = ranch_server:count_connections(Name),
  366. 10 = ranch:get_max_connections(Name),
  367. ranch:set_max_connections(Name, infinity),
  368. receive after 250 -> ok end,
  369. 20 = ranch_server:count_connections(Name),
  370. infinity = ranch:get_max_connections(Name),
  371. ranch:set_max_connections(Name, 10),
  372. 20 = ranch_server:count_connections(Name),
  373. 10 = receive_loop(connected, 1000),
  374. ok = ranch:stop_listener(Name).
  375. tcp_remove_connections(_) ->
  376. doc("Ensure that removed connections are only removed once."),
  377. Name = name(),
  378. {ok, _} = ranch:start_listener(Name, 1,
  379. ranch_tcp, [],
  380. remove_conn_and_wait_protocol, [{remove, true, 0}]),
  381. Port = ranch:get_port(Name),
  382. ok = connect_loop(Port, 10, 0),
  383. receive after 250 -> ok end,
  384. 0 = ranch_server:count_connections(Name),
  385. ok = ranch:stop_listener(Name).
  386. tcp_set_max_connections(_) ->
  387. doc("Ensure that changing the max_connections option to a larger value allows for more connections."),
  388. Name = name(),
  389. {ok, _} = ranch:start_listener(Name, 1,
  390. ranch_tcp, [{max_connections, 10}],
  391. notify_and_wait_protocol, [{msg, connected}, {pid, self()}]),
  392. Port = ranch:get_port(Name),
  393. ok = connect_loop(Port, 20, 0),
  394. 10 = ranch_server:count_connections(Name),
  395. 10 = receive_loop(connected, 1000),
  396. 10 = ranch:get_max_connections(Name),
  397. ranch:set_max_connections(Name, 20),
  398. 10 = receive_loop(connected, 1000),
  399. 20 = ranch:get_max_connections(Name),
  400. ok = ranch:stop_listener(Name).
  401. tcp_set_max_connections_clean(_) ->
  402. doc("Ensure that setting max_connections does not crash any process."),
  403. Name = name(),
  404. {ok, ListSupPid} = ranch:start_listener(Name, 4, ranch_tcp,
  405. [{max_connections, 4}],
  406. notify_and_wait_protocol, [{msg, connected}, {pid, self()}]),
  407. Children = supervisor:which_children(ListSupPid),
  408. {_, AccSupPid, _, _} = lists:keyfind(ranch_acceptors_sup, 1, Children),
  409. 1 = erlang:trace(ListSupPid, true, [procs]),
  410. 1 = erlang:trace(AccSupPid, true, [procs]),
  411. Port = ranch:get_port(Name),
  412. N = 20,
  413. ok = connect_loop(Port, N*5, 0),
  414. %% Randomly set max_connections.
  415. [spawn(ranch, set_max_connections, [Name, Max]) ||
  416. Max <- lists:flatten(lists:duplicate(N, [6, 4, 8, infinity]))],
  417. receive
  418. {trace, _, spawn, _, _} ->
  419. error(dirty_set_max_connections)
  420. after
  421. 2000 -> ok
  422. end,
  423. _ = erlang:trace(all, false, [all]),
  424. ok = clean_traces(),
  425. ok = ranch:stop_listener(Name).
  426. tcp_upgrade(_) ->
  427. doc("Ensure that protocol options can be updated."),
  428. Name = name(),
  429. {ok, _} = ranch:start_listener(Name, 1,
  430. ranch_tcp, [],
  431. notify_and_wait_protocol, [{msg, connected}, {pid, self()}]),
  432. Port = ranch:get_port(Name),
  433. ok = connect_loop(Port, 1, 0),
  434. receive connected -> ok after 1000 -> error(timeout) end,
  435. ranch:set_protocol_options(Name, [{msg, upgraded}, {pid, self()}]),
  436. ok = connect_loop(Port, 1, 0),
  437. receive upgraded -> ok after 1000 -> error(timeout) end,
  438. ok = ranch:stop_listener(Name).
  439. tcp_error_eaddrinuse(_) ->
  440. doc("Ensure that failure due to an eaddrinuse returns a compact readable error."),
  441. Name = name(),
  442. {ok, _} = ranch:start_listener(Name, 1, ranch_tcp, [], active_echo_protocol, []),
  443. Port = ranch:get_port(Name),
  444. {error, eaddrinuse} = ranch:start_listener({Name, fails}, 1,
  445. ranch_tcp, [{port, Port}], active_echo_protocol, []),
  446. ok = ranch:stop_listener(Name),
  447. %% Make sure the listener stopped.
  448. {'EXIT', _} = begin catch ranch:get_port(Name) end,
  449. ok.
  450. tcp_error_eacces(_) ->
  451. doc("Ensure that failure due to an eacces returns a compact readable error."),
  452. Name = name(),
  453. {error, eacces} = ranch:start_listener(Name, 1,
  454. ranch_tcp, [{port, 283}], active_echo_protocol, []),
  455. ok.
  456. %% Supervisor tests
  457. connection_type_supervisor(_) ->
  458. doc("The supervisor connection type must be reflected in the specifications."),
  459. Name = name(),
  460. {ok, _} = ranch:start_listener(Name, 1,
  461. ranch_tcp, [{connection_type, supervisor}],
  462. echo_protocol, []),
  463. Port = ranch:get_port(Name),
  464. {ok, Socket} = gen_tcp:connect("localhost", Port, [binary, {active, false}, {packet, raw}]),
  465. ok = gen_tcp:send(Socket, <<"TCP Ranch is working!">>),
  466. {ok, <<"TCP Ranch is working!">>} = gen_tcp:recv(Socket, 21, 1000),
  467. ConnsSup = ranch_server:get_connections_sup(Name),
  468. [{echo_protocol, _, supervisor, [echo_protocol]}] = supervisor:which_children(ConnsSup),
  469. ok = ranch:stop_listener(Name),
  470. {error, closed} = gen_tcp:recv(Socket, 0, 1000),
  471. %% Make sure the listener stopped.
  472. {'EXIT', _} = begin catch ranch:get_port(Name) end,
  473. ok.
  474. connection_type_supervisor_separate_from_connection(_) ->
  475. doc("The supervisor connection type allows separate supervised and connection processes."),
  476. Name = name(),
  477. {ok, _} = ranch:start_listener(Name, 1,
  478. ranch_tcp, [{connection_type, supervisor}],
  479. supervisor_separate, []),
  480. Port = ranch:get_port(Name),
  481. {ok, Socket} = gen_tcp:connect("localhost", Port, [binary, {active, false}, {packet, raw}]),
  482. ok = gen_tcp:send(Socket, <<"TCP Ranch is working!">>),
  483. {ok, <<"TCP Ranch is working!">>} = gen_tcp:recv(Socket, 21, 1000),
  484. ConnsSup = ranch_server:get_connections_sup(Name),
  485. [{supervisor_separate, _, supervisor, [supervisor_separate]}] = supervisor:which_children(ConnsSup),
  486. ok = ranch:stop_listener(Name),
  487. {error, closed} = gen_tcp:recv(Socket, 0, 1000),
  488. %% Make sure the listener stopped.
  489. {'EXIT', _} = begin catch ranch:get_port(Name) end,
  490. ok.
  491. supervisor_clean_child_restart(_) ->
  492. doc("Verify that only the relevant parts of the supervision tree restarted "
  493. "when the listening socket is closed."),
  494. Name = name(),
  495. %% Trace socket allocations.
  496. _ = erlang:trace(new, true, [call]),
  497. 1 = erlang:trace_pattern({ranch_tcp, listen, 1},
  498. [{'_', [], [{return_trace}]}], [global]),
  499. {ok, Pid} = ranch:start_listener(Name,
  500. 1, ranch_tcp, [], echo_protocol, []),
  501. %% Trace supervisor spawns.
  502. 1 = erlang:trace(Pid, true, [procs, set_on_spawn]),
  503. ConnsSup = ranch_server:get_connections_sup(Name),
  504. %% Manually shut the listening socket down.
  505. LSocket = receive
  506. {trace, _, return_from, {ranch_tcp, listen, 1}, {ok, Socket}} ->
  507. Socket
  508. after 0 ->
  509. error(lsocket_unknown)
  510. end,
  511. ok = gen_tcp:close(LSocket),
  512. receive after 1000 -> ok end,
  513. %% Verify that supervisor and its first two children are alive.
  514. true = is_process_alive(Pid),
  515. true = is_process_alive(ConnsSup),
  516. %% Check that acceptors_sup is restarted properly.
  517. AccSupPid = receive {trace, Pid, spawn, Pid1, _} -> Pid1 end,
  518. receive {trace, AccSupPid, spawn, _, _} -> ok end,
  519. %% No more traces then.
  520. receive
  521. {trace, _, spawn, _, _} -> error(invalid_restart)
  522. after 1000 -> ok end,
  523. %% Verify that children still registered right.
  524. ConnsSup = ranch_server:get_connections_sup(Name),
  525. _ = erlang:trace_pattern({ranch_tcp, listen, 1}, false, []),
  526. _ = erlang:trace(all, false, [all]),
  527. ok = clean_traces(),
  528. ok = ranch:stop_listener(Name).
  529. supervisor_clean_conns_sup_restart(_) ->
  530. doc("Verify that a conns_sup can not register with the same name as an already "
  531. "registered ranch_conns_sup that is still alive. Make sure this does not crash "
  532. "the ranch_server process."),
  533. Name = name(),
  534. {ok, _} = ranch:start_listener(Name,
  535. 1, ranch_tcp, [], echo_protocol, []),
  536. Server = erlang:whereis(ranch_server),
  537. ServerMonRef = erlang:monitor(process, Server),
  538. %% Exit because Name already registered and is alive.
  539. {'EXIT', _} = (catch ranch_server:set_connections_sup(Name, self())),
  540. receive
  541. {'DOWN', ServerMonRef, process, Server, _} ->
  542. error(ranch_server_down)
  543. after
  544. 1000 ->
  545. ok
  546. end,
  547. ok = ranch:stop_listener(Name).
  548. supervisor_clean_restart(_) ->
  549. doc("Verify that killing ranch_conns_sup does not crash everything "
  550. "and that it restarts properly."),
  551. Name = name(),
  552. NumAcc = 4,
  553. {ok, Pid} = ranch:start_listener(Name, NumAcc, ranch_tcp, [], echo_protocol, []),
  554. %% Trace supervisor spawns.
  555. 1 = erlang:trace(Pid, true, [procs, set_on_spawn]),
  556. ConnsSup0 = ranch_server:get_connections_sup(Name),
  557. erlang:exit(ConnsSup0, kill),
  558. receive after 1000 -> ok end,
  559. %% Verify that supervisor is alive
  560. true = is_process_alive(Pid),
  561. %% ...but children are dead.
  562. false = is_process_alive(ConnsSup0),
  563. %% Receive traces from newly started children
  564. ConnsSup = receive {trace, Pid, spawn, Pid2, _} -> Pid2 end,
  565. AccSupPid = receive {trace, Pid, spawn, Pid3, _} -> Pid3 end,
  566. %% ...and its acceptors.
  567. [receive {trace, AccSupPid, spawn, _Pid, _} -> ok end ||
  568. _ <- lists:seq(1, NumAcc)],
  569. %% No more traces then.
  570. receive
  571. {trace, EPid, spawn, _, _} when EPid == Pid; EPid == AccSupPid ->
  572. error(invalid_restart)
  573. after 1000 -> ok end,
  574. %% Verify that new children registered themselves properly.
  575. ConnsSup = ranch_server:get_connections_sup(Name),
  576. _ = erlang:trace(all, false, [all]),
  577. ok = clean_traces(),
  578. ok = ranch:stop_listener(Name).
  579. supervisor_conns_alive(_) ->
  580. doc("Ensure that active connections stay open when the listening socket gets closed."),
  581. Name = name(),
  582. _ = erlang:trace(new, true, [call]),
  583. 1 = erlang:trace_pattern({ranch_tcp, listen, 1},
  584. [{'_', [], [{return_trace}]}], [global]),
  585. {ok, _} = ranch:start_listener(Name, 1,
  586. ranch_tcp, [],
  587. remove_conn_and_wait_protocol, [{remove, false, 2500}]),
  588. %% Get the listener socket
  589. LSocket = receive
  590. {trace, _, return_from, {ranch_tcp, listen, 1}, {ok, S}} ->
  591. S
  592. after 500 ->
  593. error(lsocket_unknown)
  594. end,
  595. TcpPort = ranch:get_port(Name),
  596. {ok, Socket} = gen_tcp:connect("localhost", TcpPort,
  597. [binary, {active, true}, {packet, raw}]),
  598. receive after 500 -> ok end,
  599. %% Shut the socket down
  600. ok = gen_tcp:close(LSocket),
  601. %% Assert that client is still viable.
  602. receive {tcp_closed, _} -> error(closed) after 1500 -> ok end,
  603. ok = gen_tcp:send(Socket, <<"poke">>),
  604. receive {tcp_closed, _} -> ok end,
  605. _ = erlang:trace(all, false, [all]),
  606. ok = clean_traces(),
  607. ok = ranch:stop_listener(Name).
  608. supervisor_protocol_start_link_crash(_) ->
  609. doc("Ensure a protocol start crash does not kill all connections."),
  610. Name = name(),
  611. {ok, _} = ranch:start_listener(Name, 1, ranch_tcp, [], crash_protocol, []),
  612. ConnsSup = ranch_server:get_connections_sup(Name),
  613. Port = ranch:get_port(Name),
  614. {ok, _} = gen_tcp:connect("localhost", Port, [binary, {active, true}, {packet, raw}]),
  615. receive after 500 -> ok end,
  616. ConnsSup = ranch_server:get_connections_sup(Name),
  617. ok = ranch:stop_listener(Name).
  618. supervisor_server_recover_state(_) ->
  619. doc("Ensure that when ranch_server crashes and restarts, it recovers "
  620. "its state and continues monitoring the same processes."),
  621. Name = name(),
  622. _ = erlang:trace(new, true, [call]),
  623. 1 = erlang:trace_pattern({ranch_server, init, 1},
  624. [{'_', [], [{return_trace}]}], [global]),
  625. {ok, _} = ranch:start_listener(Name, 1, ranch_tcp, [], echo_protocol, []),
  626. ConnsSup = ranch_server:get_connections_sup(Name),
  627. ServerPid = erlang:whereis(ranch_server),
  628. {monitors, Monitors} = erlang:process_info(ServerPid, monitors),
  629. erlang:exit(ServerPid, kill),
  630. receive
  631. {trace, ServerPid2, return_from, {ranch_server, init, 1}, _Result} ->
  632. {monitors, Monitors2} = erlang:process_info(ServerPid2, monitors),
  633. %% Check that ranch_server is monitoring the same processes.
  634. true = (lists:usort(Monitors) == lists:usort(Monitors2))
  635. after
  636. 1000 ->
  637. error(timeout)
  638. end,
  639. ConnsSup = ranch_server:get_connections_sup(Name),
  640. ok = ranch:stop_listener(Name),
  641. %% Check ranch_server has removed the ranch_conns_sup.
  642. {'EXIT', {badarg, _}} = (catch ranch_server:get_connections_sup(Name)),
  643. _ = erlang:trace(all, false, [all]),
  644. ok = clean_traces().
  645. %% Utility functions.
  646. connect_loop(_, 0, _) ->
  647. ok;
  648. connect_loop(Port, N, Sleep) ->
  649. {ok, _} = gen_tcp:connect("localhost", Port,
  650. [binary, {active, false}, {packet, raw}]),
  651. receive after Sleep -> ok end,
  652. connect_loop(Port, N - 1, Sleep).
  653. receive_loop(Message, Timeout) ->
  654. receive_loop(Message, Timeout, 0).
  655. receive_loop(Message, Timeout, N) ->
  656. receive Message ->
  657. receive_loop(Message, Timeout, N + 1)
  658. after Timeout ->
  659. N
  660. end.
  661. clean_traces() ->
  662. receive
  663. {trace, _, _, _} ->
  664. clean_traces();
  665. {trace, _, _, _, _} ->
  666. clean_traces()
  667. after 0 ->
  668. ok
  669. end.