From f0ff32c031dd3d27ae0f2ff8f8bd8bd65fa1f8d1 Mon Sep 17 00:00:00 2001 From: Thales Macedo Garitezi Date: Tue, 11 Oct 2022 17:37:59 -0300 Subject: [PATCH] test: fix tests after counter changes --- .../test/emqx_bridge_mqtt_SUITE.erl | 3 ++- .../test/emqx_connector_demo.erl | 24 ++++++++++++++++--- .../test/emqx_resource_SUITE.erl | 12 ++++++++-- 3 files changed, 33 insertions(+), 6 deletions(-) diff --git a/apps/emqx_bridge/test/emqx_bridge_mqtt_SUITE.erl b/apps/emqx_bridge/test/emqx_bridge_mqtt_SUITE.erl index 4ffeee71f..819556d81 100644 --- a/apps/emqx_bridge/test/emqx_bridge_mqtt_SUITE.erl +++ b/apps/emqx_bridge/test/emqx_bridge_mqtt_SUITE.erl @@ -540,6 +540,7 @@ t_mqtt_conn_bridge_egress_reconnect(_) -> %% stop the listener 1883 to make the bridge disconnected ok = emqx_listeners:stop_listener('tcp:default'), + ct:sleep(1500), %% PUBLISH 2 messages to the 'local' broker, the message should emqx:publish(emqx_message:make(LocalTopic, Payload)), @@ -551,7 +552,7 @@ t_mqtt_conn_bridge_egress_reconnect(_) -> #{ <<"status">> := Status, <<"metrics">> := #{ - <<"matched">> := 3, <<"success">> := 1, <<"failed">> := 0, <<"queuing">> := 2 + <<"matched">> := 1, <<"success">> := 1, <<"failed">> := 0, <<"queuing">> := 2 } } when Status == <<"connected">> orelse Status == <<"connecting">>, jsx:decode(BridgeStr1) diff --git a/apps/emqx_resource/test/emqx_connector_demo.erl b/apps/emqx_resource/test/emqx_connector_demo.erl index 3b83cf7ed..105bcad77 100644 --- a/apps/emqx_resource/test/emqx_connector_demo.erl +++ b/apps/emqx_resource/test/emqx_connector_demo.erl @@ -100,6 +100,15 @@ on_query(_InstId, {inc_counter, N}, #{pid := Pid}) -> after 1000 -> {error, timeout} end; +on_query(_InstId, get_incorrect_status_count, #{pid := Pid}) -> + ReqRef = make_ref(), + From = {self(), ReqRef}, + Pid ! {From, get_incorrect_status_count}, + receive + {ReqRef, Count} -> {ok, Count} + after 1000 -> + {error, timeout} + end; on_query(_InstId, get_counter, #{pid := Pid}) -> ReqRef = make_ref(), From = {self(), ReqRef}, @@ -157,9 +166,15 @@ spawn_counter_process(Name, Register) -> Pid. counter_loop() -> - counter_loop(#{counter => 0, status => running}). + counter_loop(#{counter => 0, status => running, incorrect_status_count => 0}). -counter_loop(#{counter := Num, status := Status} = State) -> +counter_loop( + #{ + counter := Num, + status := Status, + incorrect_status_count := IncorrectCount + } = State +) -> NewState = receive block -> @@ -179,10 +194,13 @@ counter_loop(#{counter := Num, status := Status} = State) -> State#{counter => Num + N}; {{FromPid, ReqRef}, {inc, _N}} when Status == blocked -> FromPid ! {ReqRef, incorrect_status}, - State; + State#{incorrect_status_count := IncorrectCount + 1}; {get, ReplyFun} -> apply_reply(ReplyFun, Num), State; + {{FromPid, ReqRef}, get_incorrect_status_count} -> + FromPid ! {ReqRef, IncorrectCount}, + State; {{FromPid, ReqRef}, get} -> FromPid ! {ReqRef, Num}, State diff --git a/apps/emqx_resource/test/emqx_resource_SUITE.erl b/apps/emqx_resource/test/emqx_resource_SUITE.erl index 2446c8102..672e01896 100644 --- a/apps/emqx_resource/test/emqx_resource_SUITE.erl +++ b/apps/emqx_resource/test/emqx_resource_SUITE.erl @@ -420,10 +420,18 @@ t_query_counter_async_inflight(_) -> {ok, _, #{metrics := #{counters := C}}} = emqx_resource:get_instance(?ID), ct:pal("metrics: ~p", [C]), + {ok, IncorrectStatusCount} = emqx_resource:simple_sync_query(?ID, get_incorrect_status_count), + %% The `simple_sync_query' we just did also increases the matched + %% count, hence the + 1. + ExtraSimpleCallCount = IncorrectStatusCount + 1, ?assertMatch( #{matched := M, success := Ss, dropped := Dp, 'retried.success' := Rs} when - M == Ss + Dp - Rs, - C + M == Ss + Dp - Rs + ExtraSimpleCallCount, + C, + #{ + metrics => C, + extra_simple_call_count => ExtraSimpleCallCount + } ), ?assert( lists:all(