acceptor_SUITE.erl 34 KB

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