acceptor_SUITE.erl 23 KB

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