acceptor_SUITE.erl 21 KB

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