havoc_SUITE.erl 2.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071
  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. %% Comment to hide progress reports in the terminal.
  26. application:set_env(kernel, logger_sasl_compatible, true),
  27. %% Enable logging of progress reports.
  28. %% They will only be available in the HTML reports by default.
  29. ok = logger:set_primary_config(level, info),
  30. Config.
  31. end_per_suite(_) ->
  32. ok = application:stop(havoc),
  33. ok = application:stop(ranch).
  34. %% Tests.
  35. havoc_tcp(_) ->
  36. doc("Start a TCP listener, establish a hundred connections, "
  37. "run havoc, confirm we can still connect."),
  38. %% Start a TCP listener.
  39. Name = name(),
  40. {ok, _} = ranch:start_listener(Name,
  41. ranch_tcp, #{},
  42. echo_protocol, []),
  43. Port1 = ranch:get_port(Name),
  44. %% Establish a hundred connections.
  45. _ = [begin
  46. {ok, Socket} = gen_tcp:connect("localhost", Port1, [{active, false}]),
  47. Socket
  48. end || _ <- lists:seq(1, 100)],
  49. %% Run Havoc.
  50. LogFun = fun
  51. (Pid) when is_pid(Pid) ->
  52. logger:info("~p~n", [erlang:process_info(Pid)]);
  53. (Port) when is_port(Port) ->
  54. logger:info("~p~n", [erlang:port_info(Port)])
  55. end,
  56. havoc:on([{applications, [ranch]}, {prekill_callback, LogFun}]),
  57. timer:sleep(60000),
  58. havoc:off(),
  59. timer:sleep(1000),
  60. %% Confirm we can still connect.
  61. Port2 = ranch:get_port(Name),
  62. {ok, _} = gen_tcp:connect("localhost", Port2, [{active, false}]),
  63. ok = ranch:stop_listener(Name).