acceptor_SUITE.erl 26 KB

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