acceptor_SUITE.erl 21 KB

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