Skip to content

Commit

Permalink
Replace gun with fusco
Browse files Browse the repository at this point in the history
  • Loading branch information
kanes115 committed Jul 17, 2018
1 parent 3bddde0 commit e18e89c
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 23 deletions.
3 changes: 1 addition & 2 deletions rebar.config
Original file line number Diff line number Diff line change
Expand Up @@ -126,8 +126,7 @@
{fed1, [{relx, [ {overlay_vars, ["rel/vars.config", "rel/fed1.vars.config"]},
{overlay, [{template, "rel/files/ejabberd.cfg", "etc/ejabberd.cfg"}]} ]}]},
{reg1, [{relx, [ {overlay_vars, ["rel/vars.config", "rel/reg1.vars.config"]},
{overlay, [{template, "rel/files/ejabberd.cfg", "etc/ejabberd.cfg"}]} ]}]},
{test, [{deps, [ {gun, "1.0.0-pre.5"}]}]}
{overlay, [{template, "rel/files/ejabberd.cfg", "etc/ejabberd.cfg"}]} ]}]}
]}.

{plugins,
Expand Down
52 changes: 31 additions & 21 deletions test/mongoose_cowboy_metrics_SUITE.erl
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
<<"OPTIONS">>,
<<"PATCH">>]).
-define(DEFAULT_PREFIX, [?HANDLER]).
-define(CLASSES, [<<"1XX">>, <<"2XX">>, <<"3XX">>, <<"4XX">>, <<"5XX">>]).
-define(CLASSES, [<<"2XX">>, <<"3XX">>, <<"4XX">>, <<"5XX">>]).



Expand All @@ -39,12 +39,10 @@ all() ->
%%-------------------------------------------------------------------

init_per_suite(Config) ->
application:ensure_all_started(gun),
ejabberd_helper:start_ejabberd_with_config(Config, "ejabberd.cfg"),
Config.

end_per_suite(_Config) ->
application:stop(gun),
ejabberd_helper:stop_ejabberd(),
ok.

Expand All @@ -71,8 +69,8 @@ metrics_are_not_recorded_when_record_metrics_is_false(_Config) ->
destroy_metrics().

metrics_are_not_recorded() ->
run_requests([get, head, post, put, delete, options, patch],
[<<"100">>, <<"200">>, <<"300">>, <<"400">>, <<"500">>]),
run_requests([get, head, post, put, delete, options, patch],
[<<"200">>, <<"300">>, <<"400">>, <<"500">>]),
[ensure_metric_value(M, 0)
|| M <- count_metrics(?DEFAULT_PREFIX) ++ latency_metrics(?DEFAULT_PREFIX)].

Expand All @@ -81,10 +79,11 @@ metrics_are_recorded(_Config) ->
start_listener([{'_', ?HANDLER, []}], [{record_metrics, true}]),

timer:sleep(1000),
Statuses = [<<"200">>, <<"300">>, <<"400">>, <<"500">>],
run_requests([get, head, post, put, delete, options, patch],
[<<"100">>, <<"200">>, <<"300">>, <<"400">>, <<"500">>]),
Statuses),

[ensure_metric_value(request_count_metric(?DEFAULT_PREFIX, M), 5) || M <- ?METHODS],
[ensure_metric_value(request_count_metric(?DEFAULT_PREFIX, M), length(Statuses)) || M <- ?METHODS],
[ensure_metric_value(response_count_metric(?DEFAULT_PREFIX, M, C), 1) || M <- ?METHODS, C <- ?CLASSES],
[ensure_metric_bumped(response_latency_metric(?DEFAULT_PREFIX, M, C)) || M <- ?METHODS, C <- ?CLASSES],

Expand All @@ -98,10 +97,11 @@ metrics_are_recorded_with_configured_prefix(_Config) ->
start_listener([{'_', ?HANDLER, []}], [{record_metrics, true},
{handler_to_metric_prefix, #{?HANDLER => Prefix}}]),

Statuses = [<<"200">>, <<"300">>, <<"400">>, <<"500">>],
run_requests([get, head, post, put, delete, options, patch],
[<<"100">>, <<"200">>, <<"300">>, <<"400">>, <<"500">>]),
Statuses),

[ensure_metric_value(request_count_metric(Prefix, M), 5) || M <- ?METHODS],
[ensure_metric_value(request_count_metric(Prefix, M), length(Statuses)) || M <- ?METHODS],
[ensure_metric_value(response_count_metric(Prefix, M, C), 1) || M <- ?METHODS, C <- ?CLASSES],
[ensure_metric_bumped(response_latency_metric(Prefix, M, C)) || M <- ?METHODS, C <- ?CLASSES],

Expand All @@ -112,12 +112,13 @@ unsent_responses_generate_metrics(_Config) ->
create_metrics(),
start_listener([{'_', ?HANDLER, [{action, none}]}], [{record_metrics, true}]),

Statuses = [<<"200">>, <<"300">>, <<"400">>, <<"500">>],
run_requests([get, head, post, put, delete, options, patch],
[<<"100">>, <<"200">>, <<"300">>, <<"400">>, <<"500">>]),
Statuses),

%% when response is not sent Cowboy returns 204
[ensure_metric_value(request_count_metric(?DEFAULT_PREFIX, M), 5) || M <- ?METHODS],
[ensure_metric_value(response_count_metric(?DEFAULT_PREFIX, M, <<"2XX">>), 5) || M <- ?METHODS],
[ensure_metric_value(request_count_metric(?DEFAULT_PREFIX, M), length(Statuses)) || M <- ?METHODS],
[ensure_metric_value(response_count_metric(?DEFAULT_PREFIX, M, <<"2XX">>), length(Statuses)) || M <- ?METHODS],
[ensure_metric_value(response_count_metric(?DEFAULT_PREFIX, M, C), 0) || M <- ?METHODS, C <- ?CLASSES -- [<<"2XX">>]],
[ensure_metric_bumped(response_latency_metric(?DEFAULT_PREFIX, M, <<"2XX">>)) || M <- ?METHODS],
[ensure_metric_value(response_latency_metric(?DEFAULT_PREFIX, M, C), 0) || M <- ?METHODS, C <- ?CLASSES -- [<<"2XX">>]],
Expand All @@ -135,12 +136,12 @@ init(_Type, Req, Opts) ->
handle(Req, echo_status = State) ->
{BinStatus, Req1} = cowboy_req:qs_val(<<"status">>, Req),
Status = binary_to_integer(BinStatus),
{ok, RespReq} = cowboy_req:reply(Status, [], <<>>, Req1),
{ok, RespReq} = cowboy_req:reply(Status, Req1),
{ok, RespReq, State};
handle(Req, none = State) ->
{ok, Req, State}.

terminate(_Reason, _Req, _State) ->
terminate(Reason, _Req, _State) ->
ok.

%%-------------------------------------------------------------------
Expand Down Expand Up @@ -192,10 +193,19 @@ ensure_metric_bumped(Metric) ->
?assertNotEqual(0, proplists:get_value(value, DataPoints)).

run_requests(Methods, Statuses) ->
{ok, Conn} = gun:open("localhost", ranch:get_port(?LISTENER)),
{ok, _} = gun:await_up(Conn),
[begin
StreamRef = gun:M(Conn, <<"/?status=", S/binary>>, []),
{response, fin, _, _} = gun:await(Conn, StreamRef)
end || M <- Methods, S <- Statuses].

Port = ranch:get_port(?LISTENER),
{ok, Conn} = fusco:start_link({"localhost", Port, false}, []),
Res = [{ok, _Result} = fusco:request(Conn, <<"/?status=", S/binary>>, method_to_upper_bin(M), [], [], 5000)
|| M <- Methods, S <- Statuses],
fusco:disconnect(Conn),
Res.

method_to_upper_bin(Method) when is_binary(Method) ->
Method;
method_to_upper_bin(Method) when is_atom(Method) ->
MethodBin = atom_to_binary(Method, utf8),
list_to_binary(
string:to_upper(
binary_to_list(MethodBin)
)
).

0 comments on commit e18e89c

Please sign in to comment.