acceptor_SUITE.erl 26 KB

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