acceptor_SUITE.erl 27 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753
  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(Config) ->
  406. case code:is_module_native(?MODULE) of
  407. true -> doc("This test uses tracing and is not compatible with native code.");
  408. false -> do_tcp_set_max_connections_clean(Config)
  409. end.
  410. do_tcp_set_max_connections_clean(_) ->
  411. doc("Ensure that setting max_connections does not crash any process."),
  412. Name = name(),
  413. {ok, ListSupPid} = ranch:start_listener(Name, ranch_tcp,
  414. [{max_connections, 4}],
  415. notify_and_wait_protocol, [{msg, connected}, {pid, self()}]),
  416. Children = supervisor:which_children(ListSupPid),
  417. {_, AccSupPid, _, _} = lists:keyfind(ranch_acceptors_sup, 1, Children),
  418. 1 = erlang:trace(ListSupPid, true, [procs]),
  419. 1 = erlang:trace(AccSupPid, true, [procs]),
  420. Port = ranch:get_port(Name),
  421. N = 20,
  422. ok = connect_loop(Port, N*5, 0),
  423. %% Randomly set max_connections.
  424. [spawn(ranch, set_max_connections, [Name, Max]) ||
  425. Max <- lists:flatten(lists:duplicate(N, [6, 4, 8, infinity]))],
  426. receive
  427. {trace, _, spawn, _, _} ->
  428. error(dirty_set_max_connections)
  429. after
  430. 2000 -> ok
  431. end,
  432. _ = erlang:trace(all, false, [all]),
  433. ok = clean_traces(),
  434. ok = ranch:stop_listener(Name).
  435. tcp_upgrade(_) ->
  436. doc("Ensure that protocol options can be updated."),
  437. Name = name(),
  438. {ok, _} = ranch:start_listener(Name,
  439. ranch_tcp, [],
  440. notify_and_wait_protocol, [{msg, connected}, {pid, self()}]),
  441. Port = ranch:get_port(Name),
  442. ok = connect_loop(Port, 1, 0),
  443. receive connected -> ok after 1000 -> error(timeout) end,
  444. ranch:set_protocol_options(Name, [{msg, upgraded}, {pid, self()}]),
  445. ok = connect_loop(Port, 1, 0),
  446. receive upgraded -> ok after 1000 -> error(timeout) end,
  447. ok = ranch:stop_listener(Name).
  448. tcp_error_eaddrinuse(_) ->
  449. doc("Ensure that failure due to an eaddrinuse returns a compact readable error."),
  450. Name = name(),
  451. {ok, _} = ranch:start_listener(Name, ranch_tcp, [], active_echo_protocol, []),
  452. Port = ranch:get_port(Name),
  453. {error, eaddrinuse} = ranch:start_listener({Name, fails},
  454. ranch_tcp, [{port, Port}], active_echo_protocol, []),
  455. ok = ranch:stop_listener(Name),
  456. %% Make sure the listener stopped.
  457. {'EXIT', _} = begin catch ranch:get_port(Name) end,
  458. ok.
  459. tcp_error_eacces(_) ->
  460. doc("Ensure that failure due to an eacces returns a compact readable error."),
  461. Name = name(),
  462. {error, eacces} = ranch:start_listener(Name,
  463. ranch_tcp, [{port, 283}], active_echo_protocol, []),
  464. ok.
  465. %% Supervisor tests
  466. connection_type_supervisor(_) ->
  467. doc("The supervisor connection type must be reflected in the specifications."),
  468. Name = name(),
  469. {ok, _} = ranch:start_listener(Name,
  470. ranch_tcp, [{connection_type, supervisor}],
  471. echo_protocol, []),
  472. Port = ranch:get_port(Name),
  473. {ok, Socket} = gen_tcp:connect("localhost", Port, [binary, {active, false}, {packet, raw}]),
  474. ok = gen_tcp:send(Socket, <<"TCP Ranch is working!">>),
  475. {ok, <<"TCP Ranch is working!">>} = gen_tcp:recv(Socket, 21, 1000),
  476. ConnsSup = ranch_server:get_connections_sup(Name),
  477. [{echo_protocol, _, supervisor, [echo_protocol]}] = supervisor:which_children(ConnsSup),
  478. ok = ranch:stop_listener(Name),
  479. {error, closed} = gen_tcp:recv(Socket, 0, 1000),
  480. %% Make sure the listener stopped.
  481. {'EXIT', _} = begin catch ranch:get_port(Name) end,
  482. ok.
  483. connection_type_supervisor_separate_from_connection(_) ->
  484. doc("The supervisor connection type allows separate supervised and connection processes."),
  485. Name = name(),
  486. {ok, _} = ranch:start_listener(Name,
  487. ranch_tcp, [{connection_type, supervisor}],
  488. supervisor_separate, []),
  489. Port = ranch:get_port(Name),
  490. {ok, Socket} = gen_tcp:connect("localhost", Port, [binary, {active, false}, {packet, raw}]),
  491. ok = gen_tcp:send(Socket, <<"TCP Ranch is working!">>),
  492. {ok, <<"TCP Ranch is working!">>} = gen_tcp:recv(Socket, 21, 1000),
  493. ConnsSup = ranch_server:get_connections_sup(Name),
  494. [{supervisor_separate, _, supervisor, [supervisor_separate]}] = supervisor:which_children(ConnsSup),
  495. ok = ranch:stop_listener(Name),
  496. {error, closed} = gen_tcp:recv(Socket, 0, 1000),
  497. %% Make sure the listener stopped.
  498. {'EXIT', _} = begin catch ranch:get_port(Name) end,
  499. ok.
  500. supervisor_clean_child_restart(Config) ->
  501. case code:is_module_native(?MODULE) of
  502. true -> doc("This test uses tracing and is not compatible with native code.");
  503. false -> do_supervisor_clean_child_restart(Config)
  504. end.
  505. do_supervisor_clean_child_restart(_) ->
  506. doc("Verify that only the relevant parts of the supervision tree restarted "
  507. "when the listening socket is closed."),
  508. Name = name(),
  509. %% Trace socket allocations.
  510. _ = erlang:trace(new, true, [call]),
  511. 1 = erlang:trace_pattern({ranch_tcp, listen, 1},
  512. [{'_', [], [{return_trace}]}], [global]),
  513. {ok, Pid} = ranch:start_listener(Name,
  514. ranch_tcp, [{num_acceptors, 1}], echo_protocol, []),
  515. %% Trace supervisor spawns.
  516. 1 = erlang:trace(Pid, true, [procs, set_on_spawn]),
  517. ConnsSup = ranch_server:get_connections_sup(Name),
  518. %% Manually shut the listening socket down.
  519. LSocket = receive
  520. {trace, _, return_from, {ranch_tcp, listen, 1}, {ok, Socket}} ->
  521. Socket
  522. after 0 ->
  523. error(lsocket_unknown)
  524. end,
  525. ok = gen_tcp:close(LSocket),
  526. receive after 1000 -> ok end,
  527. %% Verify that supervisor and its first two children are alive.
  528. true = is_process_alive(Pid),
  529. true = is_process_alive(ConnsSup),
  530. %% Check that acceptors_sup is restarted properly.
  531. AccSupPid = receive {trace, Pid, spawn, Pid1, _} -> Pid1 end,
  532. receive {trace, AccSupPid, spawn, _, _} -> ok end,
  533. %% No more traces then.
  534. receive
  535. {trace, _, spawn, _, _} -> error(invalid_restart)
  536. after 1000 -> ok end,
  537. %% Verify that children still registered right.
  538. ConnsSup = ranch_server:get_connections_sup(Name),
  539. _ = erlang:trace_pattern({ranch_tcp, listen, 1}, false, []),
  540. _ = erlang:trace(all, false, [all]),
  541. ok = clean_traces(),
  542. ok = ranch:stop_listener(Name).
  543. supervisor_clean_conns_sup_restart(_) ->
  544. doc("Verify that a conns_sup can not register with the same name as an already "
  545. "registered ranch_conns_sup that is still alive. Make sure this does not crash "
  546. "the ranch_server process."),
  547. Name = name(),
  548. {ok, _} = ranch:start_listener(Name,
  549. ranch_tcp, [], echo_protocol, []),
  550. Server = erlang:whereis(ranch_server),
  551. ServerMonRef = erlang:monitor(process, Server),
  552. %% Exit because Name already registered and is alive.
  553. {'EXIT', _} = (catch ranch_server:set_connections_sup(Name, self())),
  554. receive
  555. {'DOWN', ServerMonRef, process, Server, _} ->
  556. error(ranch_server_down)
  557. after
  558. 1000 ->
  559. ok
  560. end,
  561. ok = ranch:stop_listener(Name).
  562. supervisor_clean_restart(Config) ->
  563. case code:is_module_native(?MODULE) of
  564. true -> doc("This test uses tracing and is not compatible with native code.");
  565. false -> do_supervisor_clean_restart(Config)
  566. end.
  567. do_supervisor_clean_restart(_) ->
  568. doc("Verify that killing ranch_conns_sup does not crash everything "
  569. "and that it restarts properly."),
  570. Name = name(),
  571. NumAcc = 4,
  572. {ok, Pid} = ranch:start_listener(Name,
  573. ranch_tcp, [{num_acceptors, NumAcc}], echo_protocol, []),
  574. %% Trace supervisor spawns.
  575. 1 = erlang:trace(Pid, true, [procs, set_on_spawn]),
  576. ConnsSup0 = ranch_server:get_connections_sup(Name),
  577. erlang:exit(ConnsSup0, kill),
  578. receive after 1000 -> ok end,
  579. %% Verify that supervisor is alive
  580. true = is_process_alive(Pid),
  581. %% ...but children are dead.
  582. false = is_process_alive(ConnsSup0),
  583. %% Receive traces from newly started children
  584. ConnsSup = receive {trace, Pid, spawn, Pid2, _} -> Pid2 end,
  585. AccSupPid = receive {trace, Pid, spawn, Pid3, _} -> Pid3 end,
  586. %% ...and its acceptors.
  587. [receive {trace, AccSupPid, spawn, _Pid, _} -> ok end ||
  588. _ <- lists:seq(1, NumAcc)],
  589. %% No more traces then.
  590. receive
  591. {trace, EPid, spawn, _, _} when EPid == Pid; EPid == AccSupPid ->
  592. error(invalid_restart)
  593. after 1000 -> ok end,
  594. %% Verify that new children registered themselves properly.
  595. ConnsSup = ranch_server:get_connections_sup(Name),
  596. _ = erlang:trace(all, false, [all]),
  597. ok = clean_traces(),
  598. ok = ranch:stop_listener(Name).
  599. supervisor_conns_alive(Config) ->
  600. case code:is_module_native(?MODULE) of
  601. true -> doc("This test uses tracing and is not compatible with native code.");
  602. false -> do_supervisor_conns_alive(Config)
  603. end.
  604. do_supervisor_conns_alive(_) ->
  605. doc("Ensure that active connections stay open when the listening socket gets closed."),
  606. Name = name(),
  607. _ = erlang:trace(new, true, [call]),
  608. 1 = erlang:trace_pattern({ranch_tcp, listen, 1},
  609. [{'_', [], [{return_trace}]}], [global]),
  610. {ok, _} = ranch:start_listener(Name,
  611. ranch_tcp, [],
  612. remove_conn_and_wait_protocol, [{remove, false, 2500}]),
  613. %% Get the listener socket
  614. LSocket = receive
  615. {trace, _, return_from, {ranch_tcp, listen, 1}, {ok, S}} ->
  616. S
  617. after 500 ->
  618. error(lsocket_unknown)
  619. end,
  620. TcpPort = ranch:get_port(Name),
  621. {ok, Socket} = gen_tcp:connect("localhost", TcpPort,
  622. [binary, {active, true}, {packet, raw}]),
  623. receive after 500 -> ok end,
  624. %% Shut the socket down
  625. ok = gen_tcp:close(LSocket),
  626. %% Assert that client is still viable.
  627. receive {tcp_closed, _} -> error(closed) after 1500 -> ok end,
  628. ok = gen_tcp:send(Socket, <<"poke">>),
  629. receive {tcp_closed, _} -> ok end,
  630. _ = erlang:trace(all, false, [all]),
  631. ok = clean_traces(),
  632. ok = ranch:stop_listener(Name).
  633. supervisor_protocol_start_link_crash(_) ->
  634. doc("Ensure a protocol start crash does not kill all connections."),
  635. Name = name(),
  636. {ok, _} = ranch:start_listener(Name, ranch_tcp, [], crash_protocol, []),
  637. ConnsSup = ranch_server:get_connections_sup(Name),
  638. Port = ranch:get_port(Name),
  639. {ok, _} = gen_tcp:connect("localhost", Port, [binary, {active, true}, {packet, raw}]),
  640. receive after 500 -> ok end,
  641. ConnsSup = ranch_server:get_connections_sup(Name),
  642. ok = ranch:stop_listener(Name).
  643. supervisor_server_recover_state(Config) ->
  644. case code:is_module_native(?MODULE) of
  645. true -> doc("This test uses tracing and is not compatible with native code.");
  646. false -> do_supervisor_server_recover_state(Config)
  647. end.
  648. do_supervisor_server_recover_state(_) ->
  649. doc("Ensure that when ranch_server crashes and restarts, it recovers "
  650. "its state and continues monitoring the same processes."),
  651. Name = name(),
  652. _ = erlang:trace(new, true, [call]),
  653. 1 = erlang:trace_pattern({ranch_server, init, 1},
  654. [{'_', [], [{return_trace}]}], [global]),
  655. {ok, _} = ranch:start_listener(Name, ranch_tcp, [], echo_protocol, []),
  656. ConnsSup = ranch_server:get_connections_sup(Name),
  657. ServerPid = erlang:whereis(ranch_server),
  658. {monitors, Monitors} = erlang:process_info(ServerPid, monitors),
  659. erlang:exit(ServerPid, kill),
  660. receive
  661. {trace, ServerPid2, return_from, {ranch_server, init, 1}, _Result} ->
  662. {monitors, Monitors2} = erlang:process_info(ServerPid2, monitors),
  663. %% Check that ranch_server is monitoring the same processes.
  664. true = (lists:usort(Monitors) == lists:usort(Monitors2))
  665. after
  666. 1000 ->
  667. error(timeout)
  668. end,
  669. ConnsSup = ranch_server:get_connections_sup(Name),
  670. ok = ranch:stop_listener(Name),
  671. %% Check ranch_server has removed the ranch_conns_sup.
  672. {'EXIT', {badarg, _}} = (catch ranch_server:get_connections_sup(Name)),
  673. _ = erlang:trace(all, false, [all]),
  674. ok = clean_traces().
  675. %% Utility functions.
  676. connect_loop(_, 0, _) ->
  677. ok;
  678. connect_loop(Port, N, Sleep) ->
  679. {ok, _} = gen_tcp:connect("localhost", Port,
  680. [binary, {active, false}, {packet, raw}]),
  681. receive after Sleep -> ok end,
  682. connect_loop(Port, N - 1, Sleep).
  683. receive_loop(Message, Timeout) ->
  684. receive_loop(Message, Timeout, 0).
  685. receive_loop(Message, Timeout, N) ->
  686. receive Message ->
  687. receive_loop(Message, Timeout, N + 1)
  688. after Timeout ->
  689. N
  690. end.
  691. clean_traces() ->
  692. receive
  693. {trace, _, _, _} ->
  694. clean_traces();
  695. {trace, _, _, _, _} ->
  696. clean_traces()
  697. after 0 ->
  698. ok
  699. end.