havoc_SUITE.erl 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123
  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. havoc_embedded(_) ->
  76. doc("Start an embedded TCP listener, establish a hundred connections, "
  77. "run havoc, confirm we can still connect."),
  78. %% Start embedded listener.
  79. Name = name(),
  80. {ok, SupPid} = embedded_sup:start_link(),
  81. {ok, _} = embedded_sup:start_listener(SupPid, Name,
  82. ranch_tcp, #{}, echo_protocol, []),
  83. %% Establish a hundred connections.
  84. ok = do_connect(100, ranch_tcp, ranch:get_port(Name), 1000),
  85. %% Set restart frequency of ranch_sup and embedded_sup.
  86. do_set_sup_frequencies([ranch_sup, SupPid], 999999, 1),
  87. %% Run havoc.
  88. havoc:on([{avg_wait, 100}, {deviation, 0}, {applications, [ranch]},
  89. {supervisors, [SupPid]}, supervisor, {prekill_callback, fun do_log/1}]),
  90. timer:sleep(10000),
  91. havoc:off(),
  92. timer:sleep(1000),
  93. %% Confirm we can still connect.
  94. ok = do_connect(1, ranch_tcp, ranch:get_port(Name), 1000),
  95. ok = embedded_sup:stop_listener(SupPid, Name),
  96. embedded_sup:stop(SupPid),
  97. ok.
  98. do_set_sup_frequencies(Sups, Intensity, Period) ->
  99. StateFun = fun (S) -> setelement(7, setelement(6, S, Intensity), Period) end,
  100. _ = [sys:replace_state(Sup, StateFun) || Sup <- Sups],
  101. ok.
  102. do_connect(0, _, _, _) ->
  103. ok;
  104. do_connect(N, Transport, Port, Timeout) ->
  105. {ok, _} = Transport:connect("localhost", Port, [{active, false}], Timeout),
  106. do_connect(N - 1, Transport, Port, Timeout).
  107. do_log(Pid) when is_pid(Pid) ->
  108. logger:info("~p~n", [erlang:process_info(Pid)]);
  109. do_log(Port) when is_port(Port) ->
  110. logger:info("~p~n", [erlang:port_info(Port)]).