acceptor_SUITE.erl 26 KB

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