From 03a8256e989091d7d153b8632c25fdc64c88ada1 Mon Sep 17 00:00:00 2001 From: Maria-12648430 Date: Fri, 4 Sep 2020 14:32:41 +0200 Subject: Metric counters for connection accepts and terminates --- test/acceptor_SUITE.erl | 49 ++++++++++++++++++++++++++++++++++++++++++++++--- test/upgrade_SUITE.erl | 30 +++++++++++++++++++++++++++++- 2 files changed, 75 insertions(+), 4 deletions(-) (limited to 'test') diff --git a/test/acceptor_SUITE.erl b/test/acceptor_SUITE.erl index d944a63..acd19a1 100644 --- a/test/acceptor_SUITE.erl +++ b/test/acceptor_SUITE.erl @@ -75,6 +75,7 @@ groups() -> misc_repeated_remove, misc_info, misc_info_embedded, + misc_metrics, misc_opts_logger, misc_set_transport_options, misc_wait_for_connections, @@ -281,6 +282,48 @@ misc_info_embedded(_) -> embedded_sup:stop(SupPid), ok. +misc_metrics(_) -> + doc("Confirm accept/terminate metrics are correct."), + Name = name(), + {ok, _} = ranch:start_listener(Name, ranch_tcp, #{}, + notify_and_wait_protocol, #{pid => self()}), + Port = ranch:get_port(Name), + %% Start 10 connections. + ok = connect_loop(Port, 10, 0), + {10, ConnPids1} = receive_loop(connected, 400), + #{metrics := Metrics1} = ranch:info(Name), + {10, 0} = do_accumulate_metrics(Metrics1), + %% Start 10 more connections. + ok = connect_loop(Port, 10, 0), + {10, ConnPids2} = receive_loop(connected, 400), + #{metrics := Metrics2} = ranch:info(Name), + {20, 0} = do_accumulate_metrics(Metrics2), + %% Terminate 10 connections. + ok = terminate_loop(stop, ConnPids2), + timer:sleep(100), + #{metrics := Metrics3} = ranch:info(Name), + {20, 10} = do_accumulate_metrics(Metrics3), + %% Terminate 10 more connections. + ok = terminate_loop(stop, ConnPids1), + timer:sleep(100), + #{metrics := Metrics4} = ranch:info(Name), + {20, 20} = do_accumulate_metrics(Metrics4), + ok = ranch:stop_listener(Name), + {'EXIT', _} = begin catch ranch:get_port(Name) end, + ok. + +do_accumulate_metrics(Metrics) -> + maps:fold( + fun + ({conns_sup, _, accept}, N, {Accepts, Terminates}) -> + {Accepts+N, Terminates}; + ({conns_sup, _, terminate}, N, {Accepts, Terminates}) -> + {Accepts, Terminates+N} + end, + {0, 0}, + Metrics + ). + misc_opts_logger(_) -> doc("Confirm that messages are sent via the configured logger module."), register(misc_opts_logger, self()), @@ -322,9 +365,9 @@ misc_set_transport_options(_) -> ConnsSups = [ConnsSup || {_, ConnsSup} <- ranch_server:get_connections_sups(Name)], _ = [begin {State, _, _, _} = sys:get_state(ConnsSup), - 20 = element(10, State), - 5001 = element(9, State), - 1001 = element(5, State) + 20 = element(11, State), + 5001 = element(10, State), + 1001 = element(6, State) end || ConnsSup <- ConnsSups], ok = ranch:suspend_listener(Name), ok = ranch:resume_listener(Name), diff --git a/test/upgrade_SUITE.erl b/test/upgrade_SUITE.erl index 07f2a34..5b9d5e2 100644 --- a/test/upgrade_SUITE.erl +++ b/test/upgrade_SUITE.erl @@ -192,13 +192,18 @@ upgrade_ranch_one_conn(_) -> do_upgrade_ranch_one_conn() -> Example = tcp_echo, + ExampleStr = atom_to_list(Example), Port = 5555, + {_, Rel, _} = do_get_paths(Example), try %% Copy the example. do_copy(Example), %% Build and start the example release using the previous Ranch version. CommitOrTag = do_use_ranch_previous(Example), do_compile_and_start(Example), + %% Ensure that the metrics key is not present in the ranch:info output. + "false\n" = do_exec_log(Rel ++ " eval " + "'maps:is_key(metrics, ranch:info(" ++ ExampleStr ++ "))'"), %% Establish a connection and check that it works. {ok, S} = gen_tcp:connect("localhost", Port, [{active, false}, binary]), ok = gen_tcp:send(S, "Hello!"), @@ -207,12 +212,35 @@ do_upgrade_ranch_one_conn() -> do_build_relup(Example, CommitOrTag), %% Perform the upgrade, then check that our connection is still up. do_upgrade(Example), + %% Ensure that the mextrics key is present in the ranch:info output. + "true\n" = do_exec_log(Rel ++ " eval " + "'maps:is_key(metrics, ranch:info(" ++ ExampleStr ++ "))'"), ok = gen_tcp:send(S, "Hello!"), {ok, <<"Hello!">>} = gen_tcp:recv(S, 0, 1000), + %% Ensure that no accepts have been counted yet. + "0\n" = do_exec_log(Rel ++ " eval " + "'lists:sum([N || {{conns_sup, _, accept}, N} <- " + "maps:to_list(maps:get(metrics, ranch:info(" ++ ExampleStr ++ ")))])'"), %% Check that new connections are still accepted. - {ok, _} = gen_tcp:connect("localhost", Port, [{active, false}, binary]), + {ok, S2} = gen_tcp:connect("localhost", Port, [{active, false}, binary]), + %% Ensure that the accept has been counted. + "1\n" = do_exec_log(Rel ++ " eval " + "'lists:sum([N || {{conns_sup, _, accept}, N} <- " + "maps:to_list(maps:get(metrics, ranch:info(" ++ ExampleStr ++ ")))])'"), + %% Ensure that no terminates have been counted yet. + "0\n" = do_exec_log(Rel ++ " eval " + "'lists:sum([N || {{conns_sup, _, terminate}, N} <- " + "maps:to_list(maps:get(metrics, ranch:info(" ++ ExampleStr ++ ")))])'"), + %% Close the socket, ensure that the termination has been counted. + ok = gen_tcp:close(S2), + "1\n" = do_exec_log(Rel ++ " eval " + "'lists:sum([N || {{conns_sup, _, terminate}, N} <- " + "maps:to_list(maps:get(metrics, ranch:info(" ++ ExampleStr ++ ")))])'"), %% Perform the downgrade, then check that our connection is still up. do_downgrade(Example), + %% Ensure that the mextrics key is not present any more. + "false\n" = do_exec_log(Rel ++ " eval " + "'maps:is_key(metrics, ranch:info(" ++ ExampleStr ++ "))'"), ok = gen_tcp:send(S, "Hello!"), {ok, <<"Hello!">>} = gen_tcp:recv(S, 0, 1000), %% Check that new connections are still accepted. -- cgit v1.2.3