acceptor_SUITE.erl 19 KB

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