ranch_concuerror.erl 2.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980
  1. %% Copyright (c) 2020, 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(temporary, []),
  27. SupPid.
  28. do_stop(SupPid) ->
  29. exit(SupPid, shutdown),
  30. %% We make sure that SupPid terminated before the test ends,
  31. %% because otherwise the shutdown will not be ordered and
  32. %% can produce error exit reasons.
  33. receive after infinity -> ok end.
  34. %% Tests.
  35. start_stop() ->
  36. %% Start a listener then stop it.
  37. SupPid = do_start(),
  38. {ok, _} = ranch:start_listener(?FUNCTION_NAME,
  39. ranch_erlang_transport, #{
  40. num_acceptors => 1
  41. },
  42. echo_protocol, []),
  43. ok = ranch:stop_listener(?FUNCTION_NAME),
  44. do_stop(SupPid).
  45. %% @todo This takes a huge amount of time.
  46. %start_stop_twice() ->
  47. % %% Start a listener then stop it. Then start and stop it again.
  48. % SupPid = do_start(),
  49. % {ok, _} = ranch:start_listener(?FUNCTION_NAME,
  50. % ranch_erlang_transport, #{
  51. % num_acceptors => 1
  52. % },
  53. % echo_protocol, []),
  54. % ok = ranch:stop_listener(?FUNCTION_NAME),
  55. % {ok, _} = ranch:start_listener(?FUNCTION_NAME,
  56. % ranch_erlang_transport, #{
  57. % num_acceptors => 1
  58. % },
  59. % echo_protocol, []),
  60. % ok = ranch:stop_listener(?FUNCTION_NAME),
  61. % do_stop(SupPid).
  62. info() ->
  63. %% Ensure we can call ranch:info/1 after starting a listener.
  64. SupPid = do_start(),
  65. {ok, _} = ranch:start_listener(?FUNCTION_NAME,
  66. ranch_erlang_transport, #{
  67. num_acceptors => 1
  68. },
  69. echo_protocol, []),
  70. #{} = ranch:info(?FUNCTION_NAME),
  71. do_stop(SupPid).