havoc_SUITE.erl 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899
  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. %% Establish a hundred connections.
  42. ok = do_connect(100, ranch_tcp, ranch:get_port(Name), 1000),
  43. %% Set restart frequency of ranch_sup.
  44. do_set_sup_frequencies([ranch_sup], 999999, 1),
  45. %% Run Havoc.
  46. havoc:on([{avg_wait, 100}, {deviation, 0}, {applications, [ranch]},
  47. {supervisors, [ranch_sup]}, supervisor, {prekill_callback, fun do_log/1}]),
  48. timer:sleep(10000),
  49. havoc:off(),
  50. timer:sleep(1000),
  51. %% Confirm we can still connect.
  52. ok = do_connect(1, ranch_tcp, ranch:get_port(Name), 1000),
  53. ok = ranch:stop_listener(Name).
  54. havoc_ssl(_) ->
  55. doc("Start a SSL listener, establish a hundred connections, "
  56. "run havoc, confirm we can still connect."),
  57. %% Start a TCP listener.
  58. Name = name(),
  59. {ok, _} = ranch:start_listener(Name,
  60. ranch_ssl, ct_helper:get_certs_from_ets(),
  61. echo_protocol, []),
  62. %% Establish a hundred connections.
  63. ok = do_connect(100, ranch_ssl, ranch:get_port(Name), 1000),
  64. %% Set restart frequencies of ranch_sup and ssl_sup.
  65. do_set_sup_frequencies([ranch_sup, ssl_sup], 999999, 1),
  66. %% Run Havoc.
  67. havoc:on([{avg_wait, 100}, {deviation, 0}, {applications, [ranch]}, {otp_applications, [ssl]},
  68. {supervisors, [ssl_sup]}, supervisor, {prekill_callback, fun do_log/1}]),
  69. timer:sleep(10000),
  70. havoc:off(),
  71. timer:sleep(1000),
  72. %% Confirm we can still connect.
  73. ok = do_connect(1, ranch_ssl, ranch:get_port(Name), 1000),
  74. ok = ranch:stop_listener(Name).
  75. do_set_sup_frequencies(Sups, Intensity, Period) ->
  76. StateFun = fun (S) -> setelement(7, setelement(6, S, Intensity), Period) end,
  77. _ = [sys:replace_state(Sup, StateFun) || Sup <- Sups],
  78. ok.
  79. do_connect(0, _, _, _) ->
  80. ok;
  81. do_connect(N, Transport, Port, Timeout) ->
  82. {ok, _} = Transport:connect("localhost", Port, [{active, false}], Timeout),
  83. do_connect(N - 1, Transport, Port, Timeout).
  84. do_log(Pid) when is_pid(Pid) ->
  85. logger:info("~p~n", [erlang:process_info(Pid)]);
  86. do_log(Port) when is_port(Port) ->
  87. logger:info("~p~n", [erlang:port_info(Port)]).