From 11ae407eed92002339fc6cde8acd767e7be953c1 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Lo=C3=AFc=20Hoguin?= Date: Wed, 27 Sep 2017 14:17:27 +0200 Subject: Ensure the behavior on stream handler crash is consistent Also corrects the lack of error response when HTTP/1.1 is used. --- test/handlers/stream_handler_h.erl | 26 +++++- test/stream_handler_SUITE.erl | 168 ++++++++++++++++++++++++++++++++++++- 2 files changed, 190 insertions(+), 4 deletions(-) (limited to 'test') diff --git a/test/handlers/stream_handler_h.erl b/test/handlers/stream_handler_h.erl index 3ee4932..74fc478 100644 --- a/test/handlers/stream_handler_h.erl +++ b/test/handlers/stream_handler_h.erl @@ -21,6 +21,16 @@ init(StreamID, Req, Opts) -> Pid ! {Pid, self(), init, StreamID, Req, Opts}, {init_commands(StreamID, Req, State), State}. +init_commands(_, _, #state{test=crash_in_init}) -> + error(crash); +init_commands(_, _, #state{test=crash_in_data}) -> + []; +init_commands(_, _, #state{test=crash_in_info}) -> + []; +init_commands(_, _, #state{test=crash_in_terminate}) -> + [{response, 200, #{<<"content-length">> => <<"12">>}, <<"Hello world!">>}, stop]; +init_commands(_, _, #state{test=crash_in_early_error}) -> + error(crash); init_commands(_, _, State=#state{test=shutdown_on_stream_stop}) -> Spawn = init_process(false, State), [{headers, 200, #{}}, {spawn, Spawn, 5000}, stop]; @@ -50,20 +60,32 @@ init_process(TrapExit, #state{pid=Pid}) -> receive {Spawn, ready} -> ok after 1000 -> error(timeout) end, Spawn. +data(_, _, _, #state{test=crash_in_data}) -> + error(crash); data(StreamID, IsFin, Data, State=#state{pid=Pid}) -> Pid ! {Pid, self(), data, StreamID, IsFin, Data, State}, {[], State}. +info(_, Resp={response, _, _, _}, State) -> + {[Resp], State}; +info(_, crash, #state{test=crash_in_info}) -> + error(crash); info(StreamID, Info, State=#state{pid=Pid}) -> Pid ! {Pid, self(), info, StreamID, Info, State}, {[], State}. +terminate(StreamID, Reason, State=#state{pid=Pid, test=crash_in_terminate}) -> + Pid ! {Pid, self(), terminate, StreamID, Reason, State}, + error(crash); terminate(StreamID, Reason, State=#state{pid=Pid}) -> Pid ! {Pid, self(), terminate, StreamID, Reason, State}, ok. -%% This clause can only test for early errors that reached the required header. +%% This clause can only test for early errors that reached the required headers. early_error(StreamID, Reason, PartialReq, Resp, Opts) -> Pid = list_to_pid(binary_to_list(cowboy_req:header(<<"x-test-pid">>, PartialReq))), Pid ! {Pid, self(), early_error, StreamID, Reason, PartialReq, Resp, Opts}, - Resp. + case cowboy_req:header(<<"x-test-case">>, PartialReq) of + <<"crash_in_early_error",_/bits>> -> error(crash); + _ -> Resp + end. diff --git a/test/stream_handler_SUITE.erl b/test/stream_handler_SUITE.erl index 6a5ff8e..632adff 100644 --- a/test/stream_handler_SUITE.erl +++ b/test/stream_handler_SUITE.erl @@ -18,6 +18,7 @@ -import(ct_helper, [config/2]). -import(ct_helper, [doc/1]). -import(cowboy_test, [gun_open/1]). +-import(cowboy_test, [gun_down/1]). %% ct. @@ -59,6 +60,169 @@ end_per_group(Name, _) -> %% Tests. +crash_in_init(Config) -> + doc("Confirm an error is sent when a stream handler crashes in init/3."), + Self = self(), + ConnPid = gun_open(Config), + Ref = gun:get(ConnPid, "/long_polling", [ + {<<"accept-encoding">>, <<"gzip">>}, + {<<"x-test-case">>, <<"crash_in_init">>}, + {<<"x-test-pid">>, pid_to_list(Self)} + ]), + %% Confirm init/3 is called. + Pid = receive {Self, P, init, _, _, _} -> P after 1000 -> error(timeout) end, + %% Confirm terminate/3 is NOT called. We have no state to give to it. + receive {Self, Pid, terminate, _, _, _} -> error(terminate) after 1000 -> ok end, + %% Receive a 500 error response. + case gun:await(ConnPid, Ref) of + {response, fin, 500, _} -> ok; + {error, {stream_error, internal_error, _}} -> ok + end. + +crash_in_data(Config) -> + doc("Confirm an error is sent when a stream handler crashes in data/4."), + Self = self(), + ConnPid = gun_open(Config), + Ref = gun:post(ConnPid, "/long_polling", [ + {<<"accept-encoding">>, <<"gzip">>}, + {<<"content-length">>, <<"6">>}, + {<<"x-test-case">>, <<"crash_in_data">>}, + {<<"x-test-pid">>, pid_to_list(Self)} + ]), + %% Confirm init/3 is called. + Pid = receive {Self, P, init, _, _, _} -> P after 1000 -> error(timeout) end, + %% Send data to make the stream handler crash. + gun:data(ConnPid, Ref, fin, <<"Hello!">>), + %% Confirm terminate/3 is called, indicating the stream ended. + receive {Self, Pid, terminate, _, _, _} -> ok after 1000 -> error(timeout) end, + %% Receive a 500 error response. + case gun:await(ConnPid, Ref) of + {response, fin, 500, _} -> ok; + {error, {stream_error, internal_error, _}} -> ok + end. + +crash_in_info(Config) -> + doc("Confirm an error is sent when a stream handler crashes in info/3."), + Self = self(), + ConnPid = gun_open(Config), + Ref = gun:get(ConnPid, "/long_polling", [ + {<<"accept-encoding">>, <<"gzip">>}, + {<<"x-test-case">>, <<"crash_in_info">>}, + {<<"x-test-pid">>, pid_to_list(Self)} + ]), + %% Confirm init/3 is called. + Pid = receive {Self, P, init, _, _, _} -> P after 1000 -> error(timeout) end, + %% Send a message to make the stream handler crash. + Pid ! {{Pid, 1}, crash}, + %% Confirm terminate/3 is called, indicating the stream ended. + receive {Self, Pid, terminate, _, _, _} -> ok after 1000 -> error(timeout) end, + %% Receive a 500 error response. + case gun:await(ConnPid, Ref) of + {response, fin, 500, _} -> ok; + {error, {stream_error, internal_error, _}} -> ok + end. + +crash_in_terminate(Config) -> + doc("Confirm the state is correct when a stream handler crashes in terminate/3."), + Self = self(), + ConnPid = gun_open(Config), + %% Do a first request. + Ref1 = gun:get(ConnPid, "/hello_world", [ + {<<"accept-encoding">>, <<"gzip">>}, + {<<"x-test-case">>, <<"crash_in_terminate">>}, + {<<"x-test-pid">>, pid_to_list(Self)} + ]), + %% Confirm init/3 is called. + Pid = receive {Self, P, init, _, _, _} -> P after 1000 -> error(timeout) end, + %% Confirm terminate/3 is called. + receive {Self, Pid, terminate, _, _, _} -> ok after 1000 -> error(timeout) end, + %% Receive the response. + {response, nofin, 200, _} = gun:await(ConnPid, Ref1), + {ok, <<"Hello world!">>} = gun:await_body(ConnPid, Ref1), + %% Do a second request to make sure the connection state is still good. + Ref2 = gun:get(ConnPid, "/hello_world", [ + {<<"accept-encoding">>, <<"gzip">>}, + {<<"x-test-case">>, <<"crash_in_terminate">>}, + {<<"x-test-pid">>, pid_to_list(Self)} + ]), + %% Confirm init/3 is called. The pid shouldn't change. + receive {Self, Pid, init, _, _, _} -> ok after 1000 -> error(timeout) end, + %% Confirm terminate/3 is called. + receive {Self, Pid, terminate, _, _, _} -> ok after 1000 -> error(timeout) end, + %% Receive the second response. + {response, nofin, 200, _} = gun:await(ConnPid, Ref2), + {ok, <<"Hello world!">>} = gun:await_body(ConnPid, Ref2), + ok. + +crash_in_early_error(Config) -> + case config(protocol, Config) of + http -> do_crash_in_early_error(Config); + http2 -> doc("The callback early_error/5 is not currently used for HTTP/2.") + end. + +do_crash_in_early_error(Config) -> + doc("Confirm an error is sent when a stream handler crashes in early_error/5." + "The connection is kept open by Cowboy."), + Self = self(), + ConnPid = gun_open(Config), + Ref1 = gun:get(ConnPid, "/long_polling", [ + {<<"accept-encoding">>, <<"gzip">>}, + {<<"x-test-case">>, <<"crash_in_early_error">>}, + {<<"x-test-pid">>, pid_to_list(Self)} + ]), + %% Confirm init/3 is called. + Pid = receive {Self, P, init, _, _, _} -> P after 1000 -> error(timeout) end, + %% Confirm terminate/3 is NOT called. We have no state to give to it. + receive {Self, Pid, terminate, _, _, _} -> error(terminate) after 1000 -> ok end, + %% Confirm early_error/5 is called. + receive {Self, Pid, early_error, _, _, _, _, _} -> ok after 1000 -> error(timeout) end, + %% Receive a 500 error response. + {response, fin, 500, _} = gun:await(ConnPid, Ref1), + %% This error is not fatal. We should be able to repeat it on the same connection. + Ref2 = gun:get(ConnPid, "/long_polling", [ + {<<"accept-encoding">>, <<"gzip">>}, + {<<"x-test-case">>, <<"crash_in_early_error">>}, + {<<"x-test-pid">>, pid_to_list(Self)} + ]), + %% Confirm init/3 is called. + receive {Self, Pid, init, _, _, _} -> ok after 1000 -> error(timeout) end, + %% Confirm terminate/3 is NOT called. We have no state to give to it. + receive {Self, Pid, terminate, _, _, _} -> error(terminate) after 1000 -> ok end, + %% Confirm early_error/5 is called. + receive {Self, Pid, early_error, _, _, _, _, _} -> ok after 1000 -> error(timeout) end, + %% Receive a 500 error response. + {response, fin, 500, _} = gun:await(ConnPid, Ref2), + ok. + +crash_in_early_error_fatal(Config) -> + case config(protocol, Config) of + http -> do_crash_in_early_error_fatal(Config); + http2 -> doc("The callback early_error/5 is not currently used for HTTP/2.") + end. + +do_crash_in_early_error_fatal(Config) -> + doc("Confirm an error is sent when a stream handler crashes in early_error/5." + "The error was fatal and the connection is closed by Cowboy."), + Self = self(), + ConnPid = gun_open(Config), + Ref = gun:get(ConnPid, "/long_polling", [ + {<<"accept-encoding">>, <<"gzip">>}, + {<<"host">>, <<"host:port">>}, + {<<"x-test-case">>, <<"crash_in_early_error_fatal">>}, + {<<"x-test-pid">>, pid_to_list(Self)} + ]), + %% Confirm init/3 is NOT called. The error occurs before we reach this step. + receive {Self, _, init, _, _, _} -> error(init) after 1000 -> ok end, + %% Confirm terminate/3 is NOT called. We have no state to give to it. + receive {Self, _, terminate, _, _, _} -> error(terminate) after 1000 -> ok end, + %% Confirm early_error/5 is called. + receive {Self, _, early_error, _, _, _, _, _} -> ok after 1000 -> error(timeout) end, + %% Receive a 400 error response. We do not send a 500 when + %% early_error/5 crashes, we send the original error. + {response, fin, 400, _} = gun:await(ConnPid, Ref), + %% Confirm the connection gets closed. + gun_down(ConnPid). + shutdown_on_stream_stop(Config) -> doc("Confirm supervised processes are shutdown when stopping the stream."), Self = self(), @@ -88,7 +252,7 @@ shutdown_on_socket_close(Config) -> doc("Confirm supervised processes are shutdown when the socket closes."), Self = self(), ConnPid = gun_open(Config), - Ref = gun:get(ConnPid, "/long_polling", [ + _ = gun:get(ConnPid, "/long_polling", [ {<<"accept-encoding">>, <<"gzip">>}, {<<"x-test-case">>, <<"shutdown_on_socket_close">>}, {<<"x-test-pid">>, pid_to_list(Self)} @@ -139,7 +303,7 @@ shutdown_timeout_on_socket_close(Config) -> "when the shutdown timeout triggers after the socket has closed."), Self = self(), ConnPid = gun_open(Config), - Ref = gun:get(ConnPid, "/long_polling", [ + _ = gun:get(ConnPid, "/long_polling", [ {<<"accept-encoding">>, <<"gzip">>}, {<<"x-test-case">>, <<"shutdown_timeout_on_socket_close">>}, {<<"x-test-pid">>, pid_to_list(Self)} -- cgit v1.2.3