acceptor_SUITE.erl 22 KB

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