Browse Source

Add a metrics test when the handler didn't send anything

Also fix a test group to use h2 instead of HTTP/1.1.
Loïc Hoguin 7 years ago
parent
commit
2a905e9bda
2 changed files with 67 additions and 2 deletions
  1. 8 0
      test/handlers/default_h.erl
  2. 59 2
      test/metrics_SUITE.erl

+ 8 - 0
test/handlers/default_h.erl

@@ -0,0 +1,8 @@
+%% This module does not do anything.
+
+-module(default_h).
+
+-export([init/2]).
+
+init(Req, Opts) ->
+	{ok, Req, Opts}.

+ 59 - 2
test/metrics_SUITE.erl

@@ -33,7 +33,7 @@ init_per_group(Name = http, Config) ->
 init_per_group(Name = https, Config) ->
 init_per_group(Name = https, Config) ->
 	cowboy_test:init_http(Name, init_plain_opts(Config), Config);
 	cowboy_test:init_http(Name, init_plain_opts(Config), Config);
 init_per_group(Name = h2, Config) ->
 init_per_group(Name = h2, Config) ->
-	cowboy_test:init_http(Name, init_plain_opts(Config), Config);
+	cowboy_test:init_http2(Name, init_plain_opts(Config), Config);
 init_per_group(Name = h2c, Config) ->
 init_per_group(Name = h2c, Config) ->
 	Config1 = cowboy_test:init_http(Name, init_plain_opts(Config), Config),
 	Config1 = cowboy_test:init_http(Name, init_plain_opts(Config), Config),
 	lists:keyreplace(protocol, 1, Config1, {protocol, http2});
 	lists:keyreplace(protocol, 1, Config1, {protocol, http2});
@@ -42,7 +42,7 @@ init_per_group(Name = http_compress, Config) ->
 init_per_group(Name = https_compress, Config) ->
 init_per_group(Name = https_compress, Config) ->
 	cowboy_test:init_http(Name, init_compress_opts(Config), Config);
 	cowboy_test:init_http(Name, init_compress_opts(Config), Config);
 init_per_group(Name = h2_compress, Config) ->
 init_per_group(Name = h2_compress, Config) ->
-	cowboy_test:init_http(Name, init_compress_opts(Config), Config);
+	cowboy_test:init_http2(Name, init_compress_opts(Config), Config);
 init_per_group(Name = h2c_compress, Config) ->
 init_per_group(Name = h2c_compress, Config) ->
 	Config1 = cowboy_test:init_http(Name, init_compress_opts(Config), Config),
 	Config1 = cowboy_test:init_http(Name, init_compress_opts(Config), Config),
 	lists:keyreplace(protocol, 1, Config1, {protocol, http2}).
 	lists:keyreplace(protocol, 1, Config1, {protocol, http2}).
@@ -67,6 +67,7 @@ init_compress_opts(Config) ->
 init_routes(_) -> [
 init_routes(_) -> [
 	{"localhost", [
 	{"localhost", [
 		{"/", hello_h, []},
 		{"/", hello_h, []},
+		{"/default", default_h, []},
 		{"/full/:key", echo_h, []}
 		{"/full/:key", echo_h, []}
 	]}
 	]}
 ].
 ].
@@ -200,3 +201,59 @@ post_body(Config) ->
 	after 1000 ->
 	after 1000 ->
 		error(timeout)
 		error(timeout)
 	end.
 	end.
+
+no_resp_body(Config) ->
+	doc("Confirm metrics are correct for a 204 response to a GET request."),
+	%% Perform a GET request.
+	ConnPid = gun_open(Config),
+	Ref = gun:get(ConnPid, "/default", [
+		{<<"accept-encoding">>, <<"gzip">>},
+		{<<"x-test-pid">>, pid_to_list(self())}
+	]),
+	{response, fin, 204, RespHeaders} = gun:await(ConnPid, Ref),
+	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 with a body.
+			#{
+				resp_status := 204,
+				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 := normal
+			}}] = maps:to_list(Procs),
+			true = ProcSpawn =< ProcExit,
+			%% Confirm other metadata are as expected.
+			#{
+				ref := _,
+				pid := From,
+				streamid := 1,
+				reason := normal,
+				req := #{}
+			} = Metrics,
+			%% All good!
+			ok
+	after 1000 ->
+		error(timeout)
+	end.