acceptor_SUITE.erl 20 KB

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