From 5ab5615ff0bc13e449cc3ff7ef528ffdeb1feb1f Mon Sep 17 00:00:00 2001 From: Grigory Starinkin Date: Sat, 20 Oct 2018 10:11:14 +0100 Subject: Handle error_response command in cowboy_metrics_h --- test/metrics_SUITE.erl | 119 +++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 119 insertions(+) (limited to 'test/metrics_SUITE.erl') diff --git a/test/metrics_SUITE.erl b/test/metrics_SUITE.erl index 92f8016..4ead60c 100644 --- a/test/metrics_SUITE.erl +++ b/test/metrics_SUITE.erl @@ -71,6 +71,8 @@ init_compress_opts(Config) -> init_routes(_) -> [ {"localhost", [ {"/", hello_h, []}, + {"/crash/no_reply", crash_h, no_reply}, + {"/crash/reply", crash_h, reply}, {"/default", default_h, []}, {"/full/:key", echo_h, []}, {"/resp/:key[/:arg]", resp_h, []}, @@ -434,3 +436,120 @@ do_ws(Config) -> error(timeout) end, gun:close(ConnPid). + +error_response(Config) -> + doc("Confirm metrics are correct when an error_response command is returned."), + %% Perform a GET request. + ConnPid = gun_open(Config), + Ref = gun:get(ConnPid, "/crash/no_reply", [ + {<<"accept-encoding">>, <<"gzip">>}, + {<<"x-test-pid">>, pid_to_list(self())} + ]), + {response, fin, 500, RespHeaders} = gun:await(ConnPid, Ref), + timer:sleep(100), + gun:close(ConnPid), + %% Receive the metrics and validate them. + receive + {metrics, From, Metrics} -> + %% Ensure the timestamps are in the expected order. + #{ + req_start := ReqStart, req_end := ReqEnd, + resp_start := RespStart, resp_end := RespEnd + } = Metrics, + true = (ReqStart =< RespStart) + and (RespStart =< RespEnd) + and (RespEnd =< ReqEnd), + %% We didn't send a body. + #{ + req_body_start := undefined, + req_body_end := undefined, + req_body_length := 0 + } = Metrics, + %% We got a 500 response without a body. + #{ + resp_status := 500, + resp_headers := ExpectedRespHeaders, + resp_body_length := 0 + } = Metrics, + ExpectedRespHeaders = maps:from_list(RespHeaders), + %% The request process executed normally. + #{procs := Procs} = Metrics, + [{_, #{ + spawn := ProcSpawn, + exit := ProcExit, + reason := {crash, _StackTrace} + }}] = maps:to_list(Procs), + true = ProcSpawn =< ProcExit, + %% Confirm other metadata are as expected. + #{ + ref := _, + pid := From, + streamid := 1, + reason := {internal_error, {'EXIT', _Pid, {crash, _StackTrace}}, 'Stream process crashed.'}, + req := #{}, + informational := [] + } = Metrics, + %% All good! + ok + after 1000 -> + error(timeout) + end. + +error_response_after_reply(Config) -> + doc("Confirm metrics are correct when an error_response command is returned " + "after a response was sent."), + %% Perform a GET request. + ConnPid = gun_open(Config), + Ref = gun:get(ConnPid, "/crash/reply", [ + {<<"accept-encoding">>, <<"gzip">>}, + {<<"x-test-pid">>, pid_to_list(self())} + ]), + {response, fin, 200, RespHeaders} = gun:await(ConnPid, Ref), + timer:sleep(100), + gun:close(ConnPid), + %% Receive the metrics and validate them. + receive + {metrics, From, Metrics} -> + %% Ensure the timestamps are in the expected order. + #{ + req_start := ReqStart, req_end := ReqEnd, + resp_start := RespStart, resp_end := RespEnd + } = Metrics, + true = (ReqStart =< RespStart) + and (RespStart =< RespEnd) + and (RespEnd =< ReqEnd), + %% We didn't send a body. + #{ + req_body_start := undefined, + req_body_end := undefined, + req_body_length := 0 + } = Metrics, + %% We got a 200 response without a body. + #{ + resp_status := 200, + resp_headers := ExpectedRespHeaders, + resp_body_length := 0 + } = Metrics, + ExpectedRespHeaders = maps:from_list(RespHeaders), + %% The request process executed normally. + #{procs := Procs} = Metrics, + [{_, #{ + spawn := ProcSpawn, + exit := ProcExit, + reason := {crash, _StackTrace} + }}] = maps:to_list(Procs), + true = ProcSpawn =< ProcExit, + %% Confirm other metadata are as expected. + #{ + ref := _, + pid := From, + streamid := 1, + reason := {internal_error, {'EXIT', _Pid, {crash, _StackTrace}}, 'Stream process crashed.'}, + req := #{}, + informational := [] + } = Metrics, + %% All good! + ok + after 1000 -> + error(timeout) + end. -- cgit v1.2.3