acceptor_SUITE.erl 16 KB

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