havoc_SUITE.erl 2.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273
  1. %% Copyright (c) 2019, 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(havoc_SUITE).
  15. -compile(export_all).
  16. -compile(nowarn_export_all).
  17. -import(ct_helper, [doc/1]).
  18. -import(ct_helper, [name/0]).
  19. %% ct.
  20. all() ->
  21. ct_helper:all(?MODULE).
  22. init_per_suite(Config) ->
  23. {ok, _} = application:ensure_all_started(ranch),
  24. ok = application:start(havoc),
  25. %% Enable logging of progress reports.
  26. %% They will only be available in the HTML reports by default.
  27. ok = logger:set_primary_config(level, info),
  28. Config.
  29. end_per_suite(_) ->
  30. ok = application:stop(havoc),
  31. ok = application:stop(ranch).
  32. %% Tests.
  33. havoc_tcp(_) ->
  34. doc("Start a TCP listener, establish a hundred connections, "
  35. "run havoc, confirm we can still connect."),
  36. %% Start a TCP listener.
  37. Name = name(),
  38. {ok, _} = ranch:start_listener(Name,
  39. ranch_tcp, #{},
  40. echo_protocol, []),
  41. Port1 = ranch:get_port(Name),
  42. %% Establish a hundred connections.
  43. _ = [begin
  44. {ok, Socket} = gen_tcp:connect("localhost", Port1, [{active, false}]),
  45. Socket
  46. end || _ <- lists:seq(1, 100)],
  47. %% Log process info of process about to be killed.
  48. LogFun = fun
  49. (Pid) when is_pid(Pid) ->
  50. logger:info("~p~n", [erlang:process_info(Pid)]);
  51. (Port) when is_port(Port) ->
  52. logger:info("~p~n", [erlang:port_info(Port)])
  53. end,
  54. %% Don't kill faster than ranch_sup can handle.
  55. KillInterval = 1000 * application:get_env(ranch, ranch_sup_period, 5),
  56. %% Run Havoc.
  57. havoc:on([{avg_wait, KillInterval}, {deviation, 0}, {applications, [ranch]},
  58. supervisor, {prekill_callback, LogFun}]),
  59. timer:sleep(10000),
  60. havoc:off(),
  61. timer:sleep(1000),
  62. %% Confirm we can still connect.
  63. Port2 = ranch:get_port(Name),
  64. {ok, _} = gen_tcp:connect("localhost", Port2, [{active, false}]),
  65. ok = ranch:stop_listener(Name).