aboutsummaryrefslogtreecommitdiffstats
path: root/test/metrics_SUITE.erl
diff options
context:
space:
mode:
authorGrigory Starinkin <[email protected]>2018-10-20 10:11:14 +0100
committerLoïc Hoguin <[email protected]>2018-10-31 15:13:23 +0100
commit5ab5615ff0bc13e449cc3ff7ef528ffdeb1feb1f (patch)
tree1f7bb40c661cc253b3078f8efc1a8d014c38ca3e /test/metrics_SUITE.erl
parent473e3fb82bd56a984dcf8936a51e3fd8affcb8f4 (diff)
downloadcowboy-5ab5615ff0bc13e449cc3ff7ef528ffdeb1feb1f.tar.gz
cowboy-5ab5615ff0bc13e449cc3ff7ef528ffdeb1feb1f.tar.bz2
cowboy-5ab5615ff0bc13e449cc3ff7ef528ffdeb1feb1f.zip
Handle error_response command in cowboy_metrics_h
Diffstat (limited to 'test/metrics_SUITE.erl')
-rw-r--r--test/metrics_SUITE.erl119
1 files changed, 119 insertions, 0 deletions
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.