ranch_concuerror.erl 2.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283
  1. %% Copyright (c) 2020-2021, 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(ranch_concuerror).
  15. -compile(export_all).
  16. -compile(nowarn_export_all).
  17. -concuerror_options([
  18. {after_timeout, 5000},
  19. {treat_as_normal, [
  20. killed, %% Acceptors are killed on shutdown.
  21. shutdown %% This is a normal exit reason in OTP.
  22. ]}
  23. ]).
  24. %% Convenience functions.
  25. do_start() ->
  26. {ok, SupPid} = ranch_app:start(normal, []),
  27. SupPid.
  28. -spec do_stop(pid()) -> no_return().
  29. do_stop(SupPid) ->
  30. exit(SupPid, shutdown),
  31. %% We make sure that SupPid terminated before the test ends,
  32. %% because otherwise the shutdown will not be ordered and
  33. %% can produce error exit reasons.
  34. receive after infinity -> ok end.
  35. %% Tests.
  36. -spec start_stop() -> no_return().
  37. start_stop() ->
  38. %% Start a listener then stop it.
  39. SupPid = do_start(),
  40. {ok, _} = ranch:start_listener(?FUNCTION_NAME,
  41. ranch_erlang_transport, #{
  42. num_acceptors => 1
  43. },
  44. echo_protocol, []),
  45. ok = ranch:stop_listener(?FUNCTION_NAME),
  46. do_stop(SupPid).
  47. %% @todo This takes a huge amount of time.
  48. %start_stop_twice() ->
  49. % %% Start a listener then stop it. Then start and stop it again.
  50. % SupPid = do_start(),
  51. % {ok, _} = ranch:start_listener(?FUNCTION_NAME,
  52. % ranch_erlang_transport, #{
  53. % num_acceptors => 1
  54. % },
  55. % echo_protocol, []),
  56. % ok = ranch:stop_listener(?FUNCTION_NAME),
  57. % {ok, _} = ranch:start_listener(?FUNCTION_NAME,
  58. % ranch_erlang_transport, #{
  59. % num_acceptors => 1
  60. % },
  61. % echo_protocol, []),
  62. % ok = ranch:stop_listener(?FUNCTION_NAME),
  63. % do_stop(SupPid).
  64. -spec info() -> no_return().
  65. info() ->
  66. %% Ensure we can call ranch:info/1 after starting a listener.
  67. SupPid = do_start(),
  68. {ok, _} = ranch:start_listener(?FUNCTION_NAME,
  69. ranch_erlang_transport, #{
  70. num_acceptors => 1
  71. },
  72. echo_protocol, []),
  73. #{} = ranch:info(?FUNCTION_NAME),
  74. do_stop(SupPid).