stream_handler_SUITE.erl 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179
  1. %% Copyright (c) 2017, 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(stream_handler_SUITE).
  15. -compile(export_all).
  16. -import(ct_helper, [config/2]).
  17. -import(ct_helper, [doc/1]).
  18. -import(cowboy_test, [gun_open/1]).
  19. %% ct.
  20. all() ->
  21. cowboy_test:common_all().
  22. groups() ->
  23. cowboy_test:common_groups(ct_helper:all(?MODULE)).
  24. init_per_group(Name = http, Config) ->
  25. cowboy_test:init_http(Name, #{stream_handlers => [stream_handler_h]}, Config);
  26. init_per_group(Name = https, Config) ->
  27. cowboy_test:init_https(Name, #{stream_handlers => [stream_handler_h]}, Config);
  28. init_per_group(Name = h2, Config) ->
  29. cowboy_test:init_http2(Name, #{stream_handlers => [stream_handler_h]}, Config);
  30. init_per_group(Name = h2c, Config) ->
  31. Config1 = cowboy_test:init_http(Name, #{stream_handlers => [stream_handler_h]}, Config),
  32. lists:keyreplace(protocol, 1, Config1, {protocol, http2});
  33. init_per_group(Name = http_compress, Config) ->
  34. cowboy_test:init_http(Name, #{
  35. stream_handlers => [cowboy_compress_h, stream_handler_h]
  36. }, Config);
  37. init_per_group(Name = https_compress, Config) ->
  38. cowboy_test:init_https(Name, #{
  39. stream_handlers => [cowboy_compress_h, stream_handler_h]
  40. }, Config);
  41. init_per_group(Name = h2_compress, Config) ->
  42. cowboy_test:init_http2(Name, #{
  43. stream_handlers => [cowboy_compress_h, stream_handler_h]
  44. }, Config);
  45. init_per_group(Name = h2c_compress, Config) ->
  46. Config1 = cowboy_test:init_http(Name, #{
  47. stream_handlers => [cowboy_compress_h, stream_handler_h]
  48. }, Config),
  49. lists:keyreplace(protocol, 1, Config1, {protocol, http2}).
  50. end_per_group(Name, _) ->
  51. cowboy:stop_listener(Name).
  52. %% Tests.
  53. shutdown_on_stream_stop(Config) ->
  54. doc("Confirm supervised processes are shutdown when stopping the stream."),
  55. Self = self(),
  56. ConnPid = gun_open(Config),
  57. Ref = gun:get(ConnPid, "/long_polling", [
  58. {<<"accept-encoding">>, <<"gzip">>},
  59. {<<"x-test-case">>, <<"shutdown_on_stream_stop">>},
  60. {<<"x-test-pid">>, pid_to_list(Self)}
  61. ]),
  62. %% Confirm init/3 is called.
  63. Pid = receive {Self, P, init, _, _, _} -> P after 1000 -> error(timeout) end,
  64. %% Receive the pid of the newly started process and monitor it.
  65. Spawn = receive {Self, Pid, spawned, S} -> S after 1000 -> error(timeout) end,
  66. MRef = monitor(process, Spawn),
  67. Spawn ! {Self, ready},
  68. %% Confirm terminate/3 is called, indicating the stream ended.
  69. receive {Self, Pid, terminate, _, _, _} -> ok after 1000 -> error(timeout) end,
  70. %% We should receive a DOWN message soon after (or before) because the stream
  71. %% handler is stopping the stream immediately after the process started.
  72. receive {'DOWN', MRef, process, Spawn, shutdown} -> ok after 1000 -> error(timeout) end,
  73. %% The response is still sent.
  74. {response, nofin, 200, _} = gun:await(ConnPid, Ref),
  75. {ok, <<>>} = gun:await_body(ConnPid, Ref),
  76. ok.
  77. shutdown_on_socket_close(Config) ->
  78. doc("Confirm supervised processes are shutdown when the socket closes."),
  79. Self = self(),
  80. ConnPid = gun_open(Config),
  81. Ref = gun:get(ConnPid, "/long_polling", [
  82. {<<"accept-encoding">>, <<"gzip">>},
  83. {<<"x-test-case">>, <<"shutdown_on_socket_close">>},
  84. {<<"x-test-pid">>, pid_to_list(Self)}
  85. ]),
  86. %% Confirm init/3 is called.
  87. Pid = receive {Self, P, init, _, _, _} -> P after 1000 -> error(timeout) end,
  88. %% Receive the pid of the newly started process and monitor it.
  89. Spawn = receive {Self, Pid, spawned, S} -> S after 1000 -> error(timeout) end,
  90. MRef = monitor(process, Spawn),
  91. Spawn ! {Self, ready},
  92. %% Close the socket.
  93. ok = gun:close(ConnPid),
  94. %% Confirm terminate/3 is called, indicating the stream ended.
  95. receive {Self, Pid, terminate, _, _, _} -> ok after 1000 -> error(timeout) end,
  96. %% Confirm we receive a DOWN message for the child process.
  97. receive {'DOWN', MRef, process, Spawn, shutdown} -> ok after 1000 -> error(timeout) end,
  98. ok.
  99. shutdown_timeout_on_stream_stop(Config) ->
  100. doc("Confirm supervised processes are killed "
  101. "when the shutdown timeout triggers after stopping the stream."),
  102. Self = self(),
  103. ConnPid = gun_open(Config),
  104. Ref = gun:get(ConnPid, "/long_polling", [
  105. {<<"accept-encoding">>, <<"gzip">>},
  106. {<<"x-test-case">>, <<"shutdown_timeout_on_stream_stop">>},
  107. {<<"x-test-pid">>, pid_to_list(Self)}
  108. ]),
  109. %% Confirm init/3 is called.
  110. Pid = receive {Self, P, init, _, _, _} -> P after 1000 -> error(timeout) end,
  111. %% Receive the pid of the newly started process and monitor it.
  112. Spawn = receive {Self, Pid, spawned, S} -> S after 1000 -> error(timeout) end,
  113. MRef = monitor(process, Spawn),
  114. Spawn ! {Self, ready},
  115. %% Confirm terminate/3 is called, indicating the stream ended.
  116. receive {Self, Pid, terminate, _, _, _} -> ok after 1000 -> error(timeout) end,
  117. %% We should NOT receive a DOWN message immediately.
  118. receive {'DOWN', MRef, process, Spawn, killed} -> error(killed) after 1500 -> ok end,
  119. %% We should received it now.
  120. receive {'DOWN', MRef, process, Spawn, killed} -> ok after 1000 -> error(timeout) end,
  121. %% The response is still sent.
  122. {response, nofin, 200, _} = gun:await(ConnPid, Ref),
  123. {ok, <<>>} = gun:await_body(ConnPid, Ref),
  124. ok.
  125. shutdown_timeout_on_socket_close(Config) ->
  126. doc("Confirm supervised processes are killed "
  127. "when the shutdown timeout triggers after the socket has closed."),
  128. Self = self(),
  129. ConnPid = gun_open(Config),
  130. Ref = gun:get(ConnPid, "/long_polling", [
  131. {<<"accept-encoding">>, <<"gzip">>},
  132. {<<"x-test-case">>, <<"shutdown_timeout_on_socket_close">>},
  133. {<<"x-test-pid">>, pid_to_list(Self)}
  134. ]),
  135. %% Confirm init/3 is called.
  136. Pid = receive {Self, P, init, _, _, _} -> P after 1000 -> error(timeout) end,
  137. %% Receive the pid of the newly started process and monitor it.
  138. Spawn = receive {Self, Pid, spawned, S} -> S after 1000 -> error(timeout) end,
  139. MRef = monitor(process, Spawn),
  140. Spawn ! {Self, ready},
  141. %% Close the socket.
  142. ok = gun:close(ConnPid),
  143. %% Confirm terminate/3 is called, indicating the stream ended.
  144. receive {Self, Pid, terminate, _, _, _} -> ok after 1000 -> error(timeout) end,
  145. %% We should NOT receive a DOWN message immediately.
  146. receive {'DOWN', MRef, process, Spawn, killed} -> error(killed) after 1500 -> ok end,
  147. %% We should received it now.
  148. receive {'DOWN', MRef, process, Spawn, killed} -> ok after 1000 -> error(timeout) end,
  149. ok.
  150. terminate_on_socket_close(Config) ->
  151. doc("Confirm terminate/3 is called when the socket gets closed brutally."),
  152. Self = self(),
  153. ConnPid = gun_open(Config),
  154. Ref = gun:get(ConnPid, "/long_polling", [
  155. {<<"accept-encoding">>, <<"gzip">>},
  156. {<<"x-test-case">>, <<"terminate_on_socket_close">>},
  157. {<<"x-test-pid">>, pid_to_list(Self)}
  158. ]),
  159. %% Confirm init/3 is called and receive the beginning of the response.
  160. Pid = receive {Self, P, init, _, _, _} -> P after 1000 -> error(timeout) end,
  161. {response, nofin, 200, _} = gun:await(ConnPid, Ref),
  162. %% Close the socket.
  163. ok = gun:close(ConnPid),
  164. %% Confirm terminate/3 is called.
  165. receive {Self, Pid, terminate, _, _, _} -> ok after 1000 -> error(timeout) end,
  166. ok.