| 1 |
%% Copyright (c) Loïc Hoguin <[email protected]> |
| 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 |
|
| 15 |
-module(stream_handler_SUITE). |
| 16 |
-compile(export_all). |
| 17 |
-compile(nowarn_export_all). |
| 18 |
|
| 19 |
-import(ct_helper, [config/2]). |
| 20 |
-import(ct_helper, [doc/1]). |
| 21 |
-import(cowboy_test, [gun_open/1]). |
| 22 |
-import(cowboy_test, [gun_down/1]). |
| 23 |
|
| 24 |
%% ct. |
| 25 |
|
| 26 |
all() -> |
| 27 |
cowboy_test:common_all(). |
| 28 |
|
| 29 |
groups() -> |
| 30 |
cowboy_test:common_groups(ct_helper:all(?MODULE)). |
| 31 |
|
| 32 |
%% We set this module as a logger in order to silence expected errors. |
| 33 |
init_per_group(Name = http, Config) -> |
| 34 |
cowboy_test:init_http(Name, init_plain_opts(), Config); |
| 35 |
init_per_group(Name = https, Config) -> |
| 36 |
cowboy_test:init_https(Name, init_plain_opts(), Config); |
| 37 |
init_per_group(Name = h2, Config) -> |
| 38 |
cowboy_test:init_http2(Name, init_plain_opts(), Config); |
| 39 |
init_per_group(Name = h2c, Config) -> |
| 40 |
Config1 = cowboy_test:init_http(Name, init_plain_opts(), Config), |
| 41 |
lists:keyreplace(protocol, 1, Config1, {protocol, http2}); |
| 42 |
init_per_group(Name = h3, Config) -> |
| 43 |
cowboy_test:init_http3(Name, init_plain_opts(), Config); |
| 44 |
init_per_group(Name = http_compress, Config) -> |
| 45 |
cowboy_test:init_http(Name, init_compress_opts(), Config); |
| 46 |
init_per_group(Name = https_compress, Config) -> |
| 47 |
cowboy_test:init_https(Name, init_compress_opts(), Config); |
| 48 |
init_per_group(Name = h2_compress, Config) -> |
| 49 |
cowboy_test:init_http2(Name, init_compress_opts(), Config); |
| 50 |
init_per_group(Name = h2c_compress, Config) -> |
| 51 |
Config1 = cowboy_test:init_http(Name, init_compress_opts(), Config), |
| 52 |
lists:keyreplace(protocol, 1, Config1, {protocol, http2}); |
| 53 |
init_per_group(Name = h3_compress, Config) -> |
| 54 |
cowboy_test:init_http3(Name, init_compress_opts(), Config). |
| 55 |
|
| 56 |
end_per_group(Name, _) -> |
| 57 |
cowboy_test:stop_group(Name). |
| 58 |
|
| 59 |
init_plain_opts() -> |
| 60 |
#{ |
| 61 |
logger => ?MODULE, |
| 62 |
stream_handlers => [stream_handler_h] |
| 63 |
}. |
| 64 |
|
| 65 |
init_compress_opts() -> |
| 66 |
#{ |
| 67 |
logger => ?MODULE, |
| 68 |
stream_handlers => [cowboy_compress_h, stream_handler_h] |
| 69 |
}. |
| 70 |
|
| 71 |
%% Logger function silencing the expected crashes. |
| 72 |
|
| 73 |
error("Unhandled exception " ++ _, [error, crash|_]) -> |
| 74 |
ok; |
| 75 |
error(Format, Args) -> |
| 76 |
error_logger:error_msg(Format, Args). |
| 77 |
|
| 78 |
%% Tests. |
| 79 |
|
| 80 |
crash_in_init(Config) -> |
| 81 |
doc("Confirm an error is sent when a stream handler crashes in init/3."), |
| 82 |
Self = self(), |
| 83 |
ConnPid = gun_open(Config), |
| 84 |
Ref = gun:get(ConnPid, "/long_polling", [ |
| 85 |
{<<"accept-encoding">>, <<"gzip">>}, |
| 86 |
{<<"x-test-case">>, <<"crash_in_init">>}, |
| 87 |
{<<"x-test-pid">>, pid_to_list(Self)} |
| 88 |
]), |
| 89 |
%% Confirm init/3 is called. |
| 90 |
Pid = receive {Self, P, init, _, _, _} -> P after 1000 -> error(timeout) end, |
| 91 |
%% Confirm terminate/3 is NOT called. We have no state to give to it. |
| 92 |
receive {Self, Pid, terminate, _, _, _} -> error(terminate) after 1000 -> ok end, |
| 93 |
%% Confirm early_error/5 is called in HTTP/1.1's case. |
| 94 |
%% HTTP/2 and HTTP/3 do not send a response back so there is no early_error call. |
| 95 |
case config(protocol, Config) of |
| 96 |
http -> receive {Self, Pid, early_error, _, _, _, _, _} -> ok after 1000 -> error(timeout) end; |
| 97 |
http2 -> ok; |
| 98 |
http3 -> ok |
| 99 |
end, |
| 100 |
do_await_internal_error(ConnPid, Ref, Config). |
| 101 |
|
| 102 |
do_await_internal_error(ConnPid, Ref, Config) -> |
| 103 |
Protocol = config(protocol, Config), |
| 104 |
case {Protocol, gun:await(ConnPid, Ref)} of |
| 105 |
{http, {response, fin, 500, _}} -> ok; |
| 106 |
{http2, {error, {stream_error, {stream_error, internal_error, _}}}} -> ok; |
| 107 |
{http3, {error, {stream_error, {stream_error, h3_internal_error, _}}}} -> ok |
| 108 |
end. |
| 109 |
|
| 110 |
crash_in_data(Config) -> |
| 111 |
doc("Confirm an error is sent when a stream handler crashes in data/4."), |
| 112 |
Self = self(), |
| 113 |
ConnPid = gun_open(Config), |
| 114 |
Ref = gun:post(ConnPid, "/long_polling", [ |
| 115 |
{<<"accept-encoding">>, <<"gzip">>}, |
| 116 |
{<<"content-length">>, <<"6">>}, |
| 117 |
{<<"x-test-case">>, <<"crash_in_data">>}, |
| 118 |
{<<"x-test-pid">>, pid_to_list(Self)} |
| 119 |
]), |
| 120 |
%% Confirm init/3 is called. |
| 121 |
Pid = receive {Self, P, init, _, _, _} -> P after 1000 -> error(timeout) end, |
| 122 |
%% Send data to make the stream handler crash. |
| 123 |
gun:data(ConnPid, Ref, fin, <<"Hello!">>), |
| 124 |
%% Confirm terminate/3 is called, indicating the stream ended. |
| 125 |
receive {Self, Pid, terminate, _, _, _} -> ok after 1000 -> error(timeout) end, |
| 126 |
do_await_internal_error(ConnPid, Ref, Config). |
| 127 |
|
| 128 |
crash_in_info(Config) -> |
| 129 |
doc("Confirm an error is sent when a stream handler crashes in info/3."), |
| 130 |
Self = self(), |
| 131 |
ConnPid = gun_open(Config), |
| 132 |
Ref = gun:get(ConnPid, "/long_polling", [ |
| 133 |
{<<"accept-encoding">>, <<"gzip">>}, |
| 134 |
{<<"x-test-case">>, <<"crash_in_info">>}, |
| 135 |
{<<"x-test-pid">>, pid_to_list(Self)} |
| 136 |
]), |
| 137 |
%% Confirm init/3 is called. |
| 138 |
Pid = receive {Self, P, init, _, _, _} -> P after 1000 -> error(timeout) end, |
| 139 |
%% Send a message to make the stream handler crash. |
| 140 |
StreamID = case config(protocol, Config) of |
| 141 |
http3 -> 0; |
| 142 |
_ -> 1 |
| 143 |
end, |
| 144 |
Pid ! {{Pid, StreamID}, crash}, |
| 145 |
%% Confirm terminate/3 is called, indicating the stream ended. |
| 146 |
receive {Self, Pid, terminate, _, _, _} -> ok after 1000 -> error(timeout) end, |
| 147 |
do_await_internal_error(ConnPid, Ref, Config). |
| 148 |
|
| 149 |
crash_in_terminate(Config) -> |
| 150 |
doc("Confirm the state is correct when a stream handler crashes in terminate/3."), |
| 151 |
Self = self(), |
| 152 |
ConnPid = gun_open(Config), |
| 153 |
%% Do a first request. |
| 154 |
Ref1 = gun:get(ConnPid, "/hello_world", [ |
| 155 |
{<<"accept-encoding">>, <<"gzip">>}, |
| 156 |
{<<"x-test-case">>, <<"crash_in_terminate">>}, |
| 157 |
{<<"x-test-pid">>, pid_to_list(Self)} |
| 158 |
]), |
| 159 |
%% Confirm init/3 is called. |
| 160 |
Pid = receive {Self, P, init, _, _, _} -> P after 1000 -> error(timeout) end, |
| 161 |
%% Confirm terminate/3 is called. |
| 162 |
receive {Self, Pid, terminate, _, _, _} -> ok after 1000 -> error(timeout) end, |
| 163 |
%% Receive the response. |
| 164 |
{response, nofin, 200, _} = gun:await(ConnPid, Ref1), |
| 165 |
{ok, <<"Hello world!">>} = gun:await_body(ConnPid, Ref1), |
| 166 |
%% Do a second request to make sure the connection state is still good. |
| 167 |
Ref2 = gun:get(ConnPid, "/hello_world", [ |
| 168 |
{<<"accept-encoding">>, <<"gzip">>}, |
| 169 |
{<<"x-test-case">>, <<"crash_in_terminate">>}, |
| 170 |
{<<"x-test-pid">>, pid_to_list(Self)} |
| 171 |
]), |
| 172 |
%% Confirm init/3 is called. The pid shouldn't change. |
| 173 |
receive {Self, Pid, init, _, _, _} -> ok after 1000 -> error(timeout) end, |
| 174 |
%% Confirm terminate/3 is called. |
| 175 |
receive {Self, Pid, terminate, _, _, _} -> ok after 1000 -> error(timeout) end, |
| 176 |
%% Receive the second response. |
| 177 |
{response, nofin, 200, _} = gun:await(ConnPid, Ref2), |
| 178 |
{ok, <<"Hello world!">>} = gun:await_body(ConnPid, Ref2), |
| 179 |
ok. |
| 180 |
|
| 181 |
%% @todo The callbacks ARE used for HTTP/2 and HTTP/3 CONNECT/TRACE requests. |
| 182 |
crash_in_early_error(Config) -> |
| 183 |
case config(protocol, Config) of |
| 184 |
http -> do_crash_in_early_error(Config); |
| 185 |
http2 -> doc("The callback early_error/5 is not currently used for HTTP/2."); |
| 186 |
http3 -> doc("The callback early_error/5 is not currently used for HTTP/3.") |
| 187 |
end. |
| 188 |
|
| 189 |
do_crash_in_early_error(Config) -> |
| 190 |
doc("Confirm an error is sent when a stream handler crashes in early_error/5." |
| 191 |
"The connection is kept open by Cowboy."), |
| 192 |
Self = self(), |
| 193 |
ConnPid = gun_open(Config), |
| 194 |
Ref1 = gun:get(ConnPid, "/long_polling", [ |
| 195 |
{<<"accept-encoding">>, <<"gzip">>}, |
| 196 |
{<<"x-test-case">>, <<"crash_in_early_error">>}, |
| 197 |
{<<"x-test-pid">>, pid_to_list(Self)} |
| 198 |
]), |
| 199 |
%% Confirm init/3 is called. |
| 200 |
Pid = receive {Self, P, init, _, _, _} -> P after 1000 -> error(timeout) end, |
| 201 |
%% Confirm terminate/3 is NOT called. We have no state to give to it. |
| 202 |
receive {Self, Pid, terminate, _, _, _} -> error(terminate) after 1000 -> ok end, |
| 203 |
%% Confirm early_error/5 is called. |
| 204 |
receive {Self, Pid, early_error, _, _, _, _, _} -> ok after 1000 -> error(timeout) end, |
| 205 |
%% Receive a 500 error response. |
| 206 |
{response, fin, 500, _} = gun:await(ConnPid, Ref1), |
| 207 |
%% This error is not fatal. We should be able to repeat it on the same connection. |
| 208 |
Ref2 = gun:get(ConnPid, "/long_polling", [ |
| 209 |
{<<"accept-encoding">>, <<"gzip">>}, |
| 210 |
{<<"x-test-case">>, <<"crash_in_early_error">>}, |
| 211 |
{<<"x-test-pid">>, pid_to_list(Self)} |
| 212 |
]), |
| 213 |
%% Confirm init/3 is called. |
| 214 |
receive {Self, Pid, init, _, _, _} -> ok after 1000 -> error(timeout) end, |
| 215 |
%% Confirm terminate/3 is NOT called. We have no state to give to it. |
| 216 |
receive {Self, Pid, terminate, _, _, _} -> error(terminate) after 1000 -> ok end, |
| 217 |
%% Confirm early_error/5 is called. |
| 218 |
receive {Self, Pid, early_error, _, _, _, _, _} -> ok after 1000 -> error(timeout) end, |
| 219 |
%% Receive a 500 error response. |
| 220 |
{response, fin, 500, _} = gun:await(ConnPid, Ref2), |
| 221 |
ok. |
| 222 |
|
| 223 |
%% @todo The callbacks ARE used for HTTP/2 and HTTP/3 CONNECT/TRACE requests. |
| 224 |
crash_in_early_error_fatal(Config) -> |
| 225 |
case config(protocol, Config) of |
| 226 |
http -> do_crash_in_early_error_fatal(Config); |
| 227 |
http2 -> doc("The callback early_error/5 is not currently used for HTTP/2."); |
| 228 |
http3 -> doc("The callback early_error/5 is not currently used for HTTP/3.") |
| 229 |
end. |
| 230 |
|
| 231 |
do_crash_in_early_error_fatal(Config) -> |
| 232 |
doc("Confirm an error is sent when a stream handler crashes in early_error/5." |
| 233 |
"The error was fatal and the connection is closed by Cowboy."), |
| 234 |
Self = self(), |
| 235 |
ConnPid = gun_open(Config), |
| 236 |
Ref = gun:get(ConnPid, "/long_polling", [ |
| 237 |
{<<"accept-encoding">>, <<"gzip">>}, |
| 238 |
{<<"host">>, <<"host:port">>}, |
| 239 |
{<<"x-test-case">>, <<"crash_in_early_error_fatal">>}, |
| 240 |
{<<"x-test-pid">>, pid_to_list(Self)} |
| 241 |
]), |
| 242 |
%% Confirm init/3 is NOT called. The error occurs before we reach this step. |
| 243 |
receive {Self, _, init, _, _, _} -> error(init) after 1000 -> ok end, |
| 244 |
%% Confirm terminate/3 is NOT called. We have no state to give to it. |
| 245 |
receive {Self, _, terminate, _, _, _} -> error(terminate) after 1000 -> ok end, |
| 246 |
%% Confirm early_error/5 is called. |
| 247 |
receive {Self, _, early_error, _, _, _, _, _} -> ok after 1000 -> error(timeout) end, |
| 248 |
%% Receive a 400 error response. We do not send a 500 when |
| 249 |
%% early_error/5 crashes, we send the original error. |
| 250 |
{response, fin, 400, _} = gun:await(ConnPid, Ref), |
| 251 |
%% Confirm the connection gets closed. |
| 252 |
gun_down(ConnPid). |
| 253 |
|
| 254 |
early_error_stream_error_reason(Config) -> |
| 255 |
doc("Confirm that the stream_error given to early_error/5 is consistent between protocols."), |
| 256 |
Self = self(), |
| 257 |
ConnPid = gun_open(Config), |
| 258 |
%% We must use different solutions to hit early_error with a stream_error |
| 259 |
%% reason in both protocols. |
| 260 |
{Method, Headers, Status, Error} = case config(protocol, Config) of |
| 261 |
http -> {<<"GET">>, [{<<"host">>, <<"host:port">>}], 400, protocol_error}; |
| 262 |
http2 -> {<<"TRACE">>, [], 501, no_error}; |
| 263 |
http3 -> {<<"TRACE">>, [], 501, h3_no_error} |
| 264 |
end, |
| 265 |
Ref = gun:request(ConnPid, Method, "/long_polling", [ |
| 266 |
{<<"accept-encoding">>, <<"gzip">>}, |
| 267 |
{<<"x-test-case">>, <<"early_error_stream_error_reason">>}, |
| 268 |
{<<"x-test-pid">>, pid_to_list(Self)} |
| 269 |
|Headers], <<>>), |
| 270 |
%% Confirm init/3 is NOT called. The error occurs before we reach this step. |
| 271 |
receive {Self, _, init, _, _, _} -> error(init) after 1000 -> ok end, |
| 272 |
%% Confirm terminate/3 is NOT called. We have no state to give to it. |
| 273 |
receive {Self, _, terminate, _, _, _} -> error(terminate) after 1000 -> ok end, |
| 274 |
%% Confirm early_error/5 is called. |
| 275 |
Reason = receive {Self, _, early_error, _, R, _, _, _} -> R after 1000 -> error(timeout) end, |
| 276 |
%% Confirm that the Reason is a {stream_error, Reason, Human}. |
| 277 |
{stream_error, Error, HumanReadable} = Reason, |
| 278 |
true = is_atom(HumanReadable), |
| 279 |
%% Receive a 400 or 501 error response. |
| 280 |
{response, fin, Status, _} = gun:await(ConnPid, Ref), |
| 281 |
ok. |
| 282 |
|
| 283 |
flow_after_body_fully_read(Config) -> |
| 284 |
doc("A flow command may be returned even after the body was read fully."), |
| 285 |
Self = self(), |
| 286 |
ConnPid = gun_open(Config), |
| 287 |
Ref = gun:post(ConnPid, "/long_polling", [ |
| 288 |
{<<"x-test-case">>, <<"flow_after_body_fully_read">>}, |
| 289 |
{<<"x-test-pid">>, pid_to_list(Self)} |
| 290 |
], <<"Hello world!">>), |
| 291 |
%% Receive a 200 response, sent after the second flow command, |
| 292 |
%% confirming that the flow command was accepted. |
| 293 |
{response, _, 200, _} = gun:await(ConnPid, Ref), |
| 294 |
gun:close(ConnPid). |
| 295 |
|
| 296 |
set_options_ignore_unknown(Config) -> |
| 297 |
doc("Confirm that unknown options are ignored when using the set_options commands."), |
| 298 |
Self = self(), |
| 299 |
ConnPid = gun_open(Config), |
| 300 |
Ref = gun:get(ConnPid, "/long_polling", [ |
| 301 |
{<<"accept-encoding">>, <<"gzip">>}, |
| 302 |
{<<"x-test-case">>, <<"set_options_ignore_unknown">>}, |
| 303 |
{<<"x-test-pid">>, pid_to_list(Self)} |
| 304 |
]), |
| 305 |
%% Confirm init/3 is called. |
| 306 |
Pid = receive {Self, P, init, _, _, _} -> P after 1000 -> error(timeout) end, |
| 307 |
%% Confirm terminate/3 is called, indicating the stream ended. |
| 308 |
receive {Self, Pid, terminate, _, _, _} -> ok after 1000 -> error(timeout) end, |
| 309 |
%% Confirm the response is sent. |
| 310 |
{response, nofin, 200, _} = gun:await(ConnPid, Ref), |
| 311 |
{ok, _} = gun:await_body(ConnPid, Ref), |
| 312 |
ok. |
| 313 |
|
| 314 |
shutdown_on_stream_stop(Config) -> |
| 315 |
doc("Confirm supervised processes are shutdown when stopping the stream."), |
| 316 |
Self = self(), |
| 317 |
ConnPid = gun_open(Config), |
| 318 |
Ref = gun:get(ConnPid, "/long_polling", [ |
| 319 |
{<<"accept-encoding">>, <<"gzip">>}, |
| 320 |
{<<"x-test-case">>, <<"shutdown_on_stream_stop">>}, |
| 321 |
{<<"x-test-pid">>, pid_to_list(Self)} |
| 322 |
]), |
| 323 |
%% Confirm init/3 is called. |
| 324 |
Pid = receive {Self, P, init, _, _, _} -> P after 1000 -> error(timeout) end, |
| 325 |
%% Receive the pid of the newly started process and monitor it. |
| 326 |
Spawn = receive {Self, Pid, spawned, S} -> S after 1000 -> error(timeout) end, |
| 327 |
MRef = monitor(process, Spawn), |
| 328 |
Spawn ! {Self, ready}, |
| 329 |
%% Confirm terminate/3 is called, indicating the stream ended. |
| 330 |
receive {Self, Pid, terminate, _, _, _} -> ok after 1000 -> error(timeout) end, |
| 331 |
%% We should receive a DOWN message soon after (or before) because the stream |
| 332 |
%% handler is stopping the stream immediately after the process started. |
| 333 |
receive {'DOWN', MRef, process, Spawn, shutdown} -> ok after 1000 -> error(timeout) end, |
| 334 |
%% The response is still sent. |
| 335 |
{response, nofin, 200, _} = gun:await(ConnPid, Ref), |
| 336 |
{ok, _} = gun:await_body(ConnPid, Ref), |
| 337 |
ok. |
| 338 |
|
| 339 |
shutdown_on_socket_close(Config) -> |
| 340 |
doc("Confirm supervised processes are shutdown when the socket closes."), |
| 341 |
Self = self(), |
| 342 |
ConnPid = gun_open(Config), |
| 343 |
_ = gun:get(ConnPid, "/long_polling", [ |
| 344 |
{<<"accept-encoding">>, <<"gzip">>}, |
| 345 |
{<<"x-test-case">>, <<"shutdown_on_socket_close">>}, |
| 346 |
{<<"x-test-pid">>, pid_to_list(Self)} |
| 347 |
]), |
| 348 |
%% Confirm init/3 is called. |
| 349 |
Pid = receive {Self, P, init, _, _, _} -> P after 1000 -> error(timeout) end, |
| 350 |
%% Receive the pid of the newly started process and monitor it. |
| 351 |
Spawn = receive {Self, Pid, spawned, S} -> S after 1000 -> error(timeout) end, |
| 352 |
MRef = monitor(process, Spawn), |
| 353 |
Spawn ! {Self, ready}, |
| 354 |
%% Close the socket. |
| 355 |
ok = gun:close(ConnPid), |
| 356 |
Protocol = config(protocol, Config), |
| 357 |
try |
| 358 |
%% Confirm terminate/3 is called, indicating the stream ended. |
| 359 |
receive {Self, Pid, terminate, _, _, _} -> ok after 1000 -> error(timeout) end, |
| 360 |
%% Confirm we receive a DOWN message for the child process. |
| 361 |
receive {'DOWN', MRef, process, Spawn, shutdown} -> ok after 1000 -> error(timeout) end, |
| 362 |
ok |
| 363 |
catch error:timeout when Protocol =:= http3 -> |
| 364 |
%% @todo Figure out why this happens. Could be a timing issue |
| 365 |
%% or a legitimate bug. I suspect that the server just |
| 366 |
%% doesn't receive the GOAWAY frame from Gun because |
| 367 |
%% Gun is too quick to close the connection. |
| 368 |
shutdown_on_socket_close(Config) |
| 369 |
end. |
| 370 |
|
| 371 |
shutdown_timeout_on_stream_stop(Config) -> |
| 372 |
doc("Confirm supervised processes are killed " |
| 373 |
"when the shutdown timeout triggers after stopping the stream."), |
| 374 |
Self = self(), |
| 375 |
ConnPid = gun_open(Config), |
| 376 |
Ref = gun:get(ConnPid, "/long_polling", [ |
| 377 |
{<<"accept-encoding">>, <<"gzip">>}, |
| 378 |
{<<"x-test-case">>, <<"shutdown_timeout_on_stream_stop">>}, |
| 379 |
{<<"x-test-pid">>, pid_to_list(Self)} |
| 380 |
]), |
| 381 |
%% Confirm init/3 is called. |
| 382 |
Pid = receive {Self, P, init, _, _, _} -> P after 1000 -> error(timeout) end, |
| 383 |
%% Receive the pid of the newly started process and monitor it. |
| 384 |
Spawn = receive {Self, Pid, spawned, S} -> S after 1000 -> error(timeout) end, |
| 385 |
MRef = monitor(process, Spawn), |
| 386 |
Spawn ! {Self, ready}, |
| 387 |
%% Confirm terminate/3 is called, indicating the stream ended. |
| 388 |
receive {Self, Pid, terminate, _, _, _} -> ok after 1000 -> error(timeout) end, |
| 389 |
%% We should NOT receive a DOWN message immediately. |
| 390 |
receive {'DOWN', MRef, process, Spawn, killed} -> error(killed) after 1500 -> ok end, |
| 391 |
%% We should received it now. |
| 392 |
receive {'DOWN', MRef, process, Spawn, killed} -> ok after 1000 -> error(timeout) end, |
| 393 |
%% The response is still sent. |
| 394 |
{response, nofin, 200, _} = gun:await(ConnPid, Ref), |
| 395 |
{ok, _} = gun:await_body(ConnPid, Ref), |
| 396 |
ok. |
| 397 |
|
| 398 |
shutdown_timeout_on_socket_close(Config) -> |
| 399 |
doc("Confirm supervised processes are killed " |
| 400 |
"when the shutdown timeout triggers after the socket has closed."), |
| 401 |
Self = self(), |
| 402 |
ConnPid = gun_open(Config), |
| 403 |
_ = gun:get(ConnPid, "/long_polling", [ |
| 404 |
{<<"accept-encoding">>, <<"gzip">>}, |
| 405 |
{<<"x-test-case">>, <<"shutdown_timeout_on_socket_close">>}, |
| 406 |
{<<"x-test-pid">>, pid_to_list(Self)} |
| 407 |
]), |
| 408 |
%% Confirm init/3 is called. |
| 409 |
Pid = receive {Self, P, init, _, _, _} -> P after 1000 -> error(timeout) end, |
| 410 |
%% Receive the pid of the newly started process and monitor it. |
| 411 |
Spawn = receive {Self, Pid, spawned, S} -> S after 1000 -> error(timeout) end, |
| 412 |
MRef = monitor(process, Spawn), |
| 413 |
Spawn ! {Self, ready}, |
| 414 |
%% Close the socket. |
| 415 |
ok = gun:close(ConnPid), |
| 416 |
Protocol = config(protocol, Config), |
| 417 |
try |
| 418 |
%% Confirm terminate/3 is called, indicating the stream ended. |
| 419 |
receive {Self, Pid, terminate, _, _, _} -> ok after 1000 -> error(timeout) end, |
| 420 |
%% We should NOT receive a DOWN message immediately. |
| 421 |
receive {'DOWN', MRef, process, Spawn, killed} -> error(killed) after 1500 -> ok end, |
| 422 |
%% We should receive it now. |
| 423 |
receive {'DOWN', MRef, process, Spawn, killed} -> ok after 1000 -> error(timeout) end, |
| 424 |
ok |
| 425 |
catch error:timeout when Protocol =:= http3 -> |
| 426 |
%% @todo Figure out why this happens. Could be a timing issue |
| 427 |
%% or a legitimate bug. I suspect that the server just |
| 428 |
%% doesn't receive the GOAWAY frame from Gun because |
| 429 |
%% Gun is too quick to close the connection. |
| 430 |
shutdown_timeout_on_socket_close(Config) |
| 431 |
end. |
| 432 |
|
| 433 |
switch_protocol_after_headers(Config) -> |
| 434 |
case config(protocol, Config) of |
| 435 |
http -> do_switch_protocol_after_response( |
| 436 |
<<"switch_protocol_after_headers">>, Config); |
| 437 |
http2 -> doc("The switch_protocol command is not currently supported for HTTP/2."); |
| 438 |
http3 -> doc("The switch_protocol command is not currently supported for HTTP/3.") |
| 439 |
end. |
| 440 |
|
| 441 |
switch_protocol_after_headers_data(Config) -> |
| 442 |
case config(protocol, Config) of |
| 443 |
http -> do_switch_protocol_after_response( |
| 444 |
<<"switch_protocol_after_headers_data">>, Config); |
| 445 |
http2 -> doc("The switch_protocol command is not currently supported for HTTP/2."); |
| 446 |
http3 -> doc("The switch_protocol command is not currently supported for HTTP/3.") |
| 447 |
end. |
| 448 |
|
| 449 |
switch_protocol_after_response(Config) -> |
| 450 |
case config(protocol, Config) of |
| 451 |
http -> do_switch_protocol_after_response( |
| 452 |
<<"switch_protocol_after_response">>, Config); |
| 453 |
http2 -> doc("The switch_protocol command is not currently supported for HTTP/2."); |
| 454 |
http3 -> doc("The switch_protocol command is not currently supported for HTTP/3.") |
| 455 |
end. |
| 456 |
|
| 457 |
do_switch_protocol_after_response(TestCase, Config) -> |
| 458 |
doc("The 101 informational response must not be sent when a response " |
| 459 |
"has already been sent before the switch_protocol is returned."), |
| 460 |
Self = self(), |
| 461 |
ConnPid = gun_open(Config), |
| 462 |
Ref = gun:get(ConnPid, "/long_polling", [ |
| 463 |
{<<"accept-encoding">>, <<"gzip">>}, |
| 464 |
{<<"x-test-case">>, TestCase}, |
| 465 |
{<<"x-test-pid">>, pid_to_list(Self)} |
| 466 |
]), |
| 467 |
%% Confirm init/3 is called and receive the response. |
| 468 |
Pid = receive {Self, P, init, _, _, _} -> P after 1000 -> error(timeout) end, |
| 469 |
{response, nofin, 200, Headers} = gun:await(ConnPid, Ref), |
| 470 |
Gzipped = |
| 471 |
lists:keyfind(<<"content-encoding">>, 1, Headers) |
| 472 |
=:= {<<"content-encoding">>, <<"gzip">>}, |
| 473 |
case TestCase of |
| 474 |
<<"switch_protocol_after_headers">> -> |
| 475 |
ok; |
| 476 |
_ -> |
| 477 |
<<"{}">> = case gun:await_body(ConnPid, Ref) of |
| 478 |
{ok, Body} when Gzipped -> |
| 479 |
zlib:gunzip(Body); |
| 480 |
{ok, Body} -> |
| 481 |
Body |
| 482 |
end, |
| 483 |
ok |
| 484 |
end, |
| 485 |
{error, _} = gun:await(ConnPid, Ref), |
| 486 |
%% Confirm terminate/3 is called. |
| 487 |
receive {Self, Pid, terminate, _, _, _} -> ok after 1000 -> error(timeout) end, |
| 488 |
%% Confirm takeover/7 is called. |
| 489 |
receive {Self, Pid, takeover, _, _, _, _, _, _, _} -> ok after 1000 -> error(timeout) end, |
| 490 |
ok. |
| 491 |
|
| 492 |
terminate_on_socket_close(Config) -> |
| 493 |
doc("Confirm terminate/3 is called when the socket gets closed brutally."), |
| 494 |
Self = self(), |
| 495 |
ConnPid = gun_open(Config), |
| 496 |
Ref = gun:get(ConnPid, "/long_polling", [ |
| 497 |
{<<"accept-encoding">>, <<"gzip">>}, |
| 498 |
{<<"x-test-case">>, <<"terminate_on_socket_close">>}, |
| 499 |
{<<"x-test-pid">>, pid_to_list(Self)} |
| 500 |
]), |
| 501 |
%% Confirm init/3 is called and receive the beginning of the response. |
| 502 |
Pid = receive {Self, P, init, _, _, _} -> P after 1000 -> error(timeout) end, |
| 503 |
{response, nofin, 200, _} = gun:await(ConnPid, Ref), |
| 504 |
%% Close the socket. |
| 505 |
ok = gun:close(ConnPid), |
| 506 |
%% Confirm terminate/3 is called. |
| 507 |
receive {Self, Pid, terminate, _, _, _} -> ok after 1000 -> error(timeout) end, |
| 508 |
ok. |
| 509 |
|
| 510 |
terminate_on_stop(Config) -> |
| 511 |
doc("Confirm terminate/3 is called after stop is returned."), |
| 512 |
Self = self(), |
| 513 |
ConnPid = gun_open(Config), |
| 514 |
Ref = gun:get(ConnPid, "/long_polling", [ |
| 515 |
{<<"accept-encoding">>, <<"gzip">>}, |
| 516 |
{<<"x-test-case">>, <<"terminate_on_stop">>}, |
| 517 |
{<<"x-test-pid">>, pid_to_list(Self)} |
| 518 |
]), |
| 519 |
%% Confirm init/3 is called and receive the response. |
| 520 |
Pid = receive {Self, P, init, _, _, _} -> P after 1000 -> error(timeout) end, |
| 521 |
{response, fin, 204, _} = gun:await(ConnPid, Ref), |
| 522 |
%% Confirm the stream is still alive even though we |
| 523 |
%% received the response fully, and tell it to stop. |
| 524 |
StreamID = case config(protocol, Config) of |
| 525 |
http -> 1; |
| 526 |
http2 -> 1; |
| 527 |
http3 -> 0 |
| 528 |
end, |
| 529 |
Pid ! {{Pid, StreamID}, please_stop}, |
| 530 |
receive {Self, Pid, info, _, please_stop, _} -> ok after 1000 -> error(timeout) end, |
| 531 |
%% Confirm terminate/3 is called. |
| 532 |
receive {Self, Pid, terminate, _, _, _} -> ok after 1000 -> error(timeout) end, |
| 533 |
ok. |
| 534 |
|
| 535 |
terminate_on_switch_protocol(Config) -> |
| 536 |
case config(protocol, Config) of |
| 537 |
http -> do_terminate_on_switch_protocol(Config); |
| 538 |
http2 -> doc("The switch_protocol command is not currently supported for HTTP/2."); |
| 539 |
http3 -> doc("The switch_protocol command is not currently supported for HTTP/3.") |
| 540 |
end. |
| 541 |
|
| 542 |
do_terminate_on_switch_protocol(Config) -> |
| 543 |
doc("Confirm terminate/3 is called after switch_protocol is returned."), |
| 544 |
Self = self(), |
| 545 |
ConnPid = gun_open(Config), |
| 546 |
Ref = gun:get(ConnPid, "/long_polling", [ |
| 547 |
{<<"accept-encoding">>, <<"gzip">>}, |
| 548 |
{<<"connection">>, <<"upgrade">>}, |
| 549 |
{<<"upgrade">>, <<"shsp">>}, |
| 550 |
{<<"x-test-case">>, <<"terminate_on_switch_protocol">>}, |
| 551 |
{<<"x-test-pid">>, pid_to_list(Self)} |
| 552 |
]), |
| 553 |
%% Confirm init/3 is called and receive the response. |
| 554 |
Pid = receive {Self, P, init, _, _, _} -> P after 1000 -> error(timeout) end, |
| 555 |
{upgrade, [<<"shsp">>], _} = gun:await(ConnPid, Ref), |
| 556 |
%% Confirm terminate/3 is called. |
| 557 |
receive {Self, Pid, terminate, _, _, _} -> ok after 1000 -> error(timeout) end, |
| 558 |
%% Confirm takeover/7 is called. |
| 559 |
receive {Self, Pid, takeover, _, _, _, _, _, _, _} -> ok after 1000 -> error(timeout) end, |
| 560 |
ok. |