Selaa lähdekoodia

test: make dashboard monitor test case more stable

zmstone 1 vuosi sitten
vanhempi
commit
543daa126e

+ 3 - 0
apps/emqx_dashboard/src/emqx_dashboard_monitor.erl

@@ -237,6 +237,8 @@ inplace_downsample() ->
     ),
     ok.
 
+%% compare the original data points with the compacted data points
+%% return the timestamps to be deleted and the new data points to be written
 compare(Remain, [], Deletes, Writes) ->
     %% all compacted buckets have been processed, remaining datapoints should all be deleted
     RemainTsList = lists:map(fun({T, _Data}) -> T end, Remain),
@@ -254,6 +256,7 @@ compare([{T0, _} | _] = All, [{T1, Data1} | Compacted], Deletes, Writes) when T0
     %% compare with the next compacted bucket timestamp
     compare(All, Compacted, Deletes, [{T1, Data1} | Writes]).
 
+%% compact the data points to a smaller set of buckets
 compact(_Now, [], Acc) ->
     lists:reverse(Acc);
 compact(Now, [{Time, Data} | Rest], Acc) ->

+ 10 - 1
apps/emqx_dashboard/test/emqx_dashboard_monitor_SUITE.erl

@@ -196,7 +196,8 @@ t_inplace_downsample(_Config) ->
     emqx_dashboard_monitor ! clean_expired,
     %% ensure downsample happened
     ok = gen_server:call(emqx_dashboard_monitor, dummy, infinity),
-    All = emqx_dashboard_monitor:all_data(),
+    All1 = emqx_dashboard_monitor:all_data(),
+    All = drop_dummy_data_points(All1),
     AllSent = lists:map(fun({_, #{sent := S}}) -> S end, All),
     ?assertEqual(Total, lists:sum(AllSent)),
     %% check timestamps are not random after downsample
@@ -204,6 +205,14 @@ t_inplace_downsample(_Config) ->
     ok = check_intervals(ExpectedIntervals, All),
     ok.
 
+%% there might be some data points added while downsample is running
+%% because the sampling interval during test is 1s, so they do not perfectly
+%% match the expected intervals
+%% this function is to dorp those dummy data points
+drop_dummy_data_points(All) ->
+    IsZeroValues = fun(Map) -> lists:all(fun(Value) -> Value =:= 0 end, maps:values(Map)) end,
+    lists:filter(fun({_, Map}) -> not IsZeroValues(Map) end, All).
+
 check_intervals(_, []) ->
     ok;
 check_intervals([], All) ->