2020-include_lib (" proper/include/proper.hrl" ).
2121-include_lib (" eunit/include/eunit.hrl" ).
2222-include_lib (" amqp_client/include/amqp_client.hrl" ).
23+ -include_lib (" rabbit_common/include/rabbit_core_metrics.hrl" ).
2324
2425
2526all () ->
@@ -75,6 +76,7 @@ end_per_group(_, Config) ->
7576 Config .
7677
7778init_per_testcase (Testcase , Config ) ->
79+ clean_core_metrics (Config ),
7880 rabbit_ct_helpers :testcase_started (Config , Testcase ).
7981
8082end_per_testcase (Testcase , Config ) ->
@@ -144,7 +146,7 @@ connection_metric_idemp(Config, {N, R}) ->
144146 Table2 = [ Pid || {Pid , _ } <- read_table_rpc (Config , connection_coarse_metrics )],
145147 % referesh stats 'R' times
146148 [[Pid ! emit_stats || Pid <- Table ] || _ <- lists :seq (1 , R )],
147- timer : sleep ( 100 ),
149+ force_metric_gc ( Config ),
148150 TableAfter = [ Pid || {Pid , _ } <- read_table_rpc (Config , connection_metrics )],
149151 TableAfter2 = [ Pid || {Pid , _ } <- read_table_rpc (Config , connection_coarse_metrics )],
150152 [rabbit_ct_client_helpers :close_connection (Conn ) || Conn <- Conns ],
@@ -158,14 +160,15 @@ channel_metric_idemp(Config, {N, R}) ->
158160 Table2 = [ Pid || {Pid , _ } <- read_table_rpc (Config , channel_process_metrics )],
159161 % referesh stats 'R' times
160162 [[Pid ! emit_stats || Pid <- Table ] || _ <- lists :seq (1 , R )],
161- timer : sleep ( 100 ),
163+ force_metric_gc ( Config ),
162164 TableAfter = [ Pid || {Pid , _ } <- read_table_rpc (Config , channel_metrics )],
163165 TableAfter2 = [ Pid || {Pid , _ } <- read_table_rpc (Config , channel_process_metrics )],
164166 rabbit_ct_client_helpers :close_connection (Conn ),
165167 (Table2 == TableAfter2 ) and (Table == TableAfter ) and
166168 (N == length (Table )) and (N == length (TableAfter )).
167169
168170queue_metric_idemp (Config , {N , R }) ->
171+ clean_core_metrics (Config ),
169172 Conn = rabbit_ct_client_helpers :open_unmanaged_connection (Config ),
170173 {ok , Chan } = amqp_connection :open_channel (Conn ),
171174 Queues =
@@ -175,14 +178,15 @@ queue_metric_idemp(Config, {N, R}) ->
175178 ensure_channel_queue_metrics_populated (Chan , Queue ),
176179 Queue
177180 end || _ <- lists :seq (1 , N )],
178- Table = [ Pid || {Pid , _ } <- read_table_rpc (Config , queue_metrics )],
179- Table2 = [ Pid || {Pid , _ } <- read_table_rpc (Config , queue_coarse_metrics )],
181+
182+ Table = [ Pid || {Pid , _ , _ } <- read_table_rpc (Config , queue_metrics )],
183+ Table2 = [ Pid || {Pid , _ , _ } <- read_table_rpc (Config , queue_coarse_metrics )],
180184 % referesh stats 'R' times
181185 ChanTable = read_table_rpc (Config , channel_created ),
182- [[Pid ! emit_stats || {Pid , _ } <- ChanTable ] || _ <- lists :seq (1 , R )],
183- timer : sleep ( 100 ),
184- TableAfter = [ Pid || {Pid , _ } <- read_table_rpc (Config , queue_metrics )],
185- TableAfter2 = [ Pid || {Pid , _ } <- read_table_rpc (Config , queue_coarse_metrics )],
186+ [[Pid ! emit_stats || {Pid , _ , _ } <- ChanTable ] || _ <- lists :seq (1 , R )],
187+ force_metric_gc ( Config ),
188+ TableAfter = [ Pid || {Pid , _ , _ } <- read_table_rpc (Config , queue_metrics )],
189+ TableAfter2 = [ Pid || {Pid , _ , _ } <- read_table_rpc (Config , queue_coarse_metrics )],
186190 [ delete_queue (Chan , Q ) || Q <- Queues ],
187191 rabbit_ct_client_helpers :close_connection (Conn ),
188192 (Table2 == TableAfter2 ) and (Table == TableAfter ) and
@@ -191,7 +195,9 @@ queue_metric_idemp(Config, {N, R}) ->
191195connection_metric_count (Config , Ops ) ->
192196 add_rem_counter (Config , Ops ,
193197 {fun rabbit_ct_client_helpers :open_unmanaged_connection /1 ,
194- fun rabbit_ct_client_helpers :close_connection /1 },
198+ fun (Cfg ) ->
199+ rabbit_ct_client_helpers :close_connection (Cfg )
200+ end },
195201 [ connection_created ,
196202 connection_metrics ,
197203 connection_coarse_metrics ]).
@@ -222,9 +228,10 @@ queue_metric_count(Config, Ops) ->
222228 end ,
223229 Result = add_rem_counter (Config , Ops ,
224230 {AddFun ,
225- fun (Q ) -> delete_queue (Chan , Q ) end },
226- [ channel_queue_metrics ,
227- channel_queue_exchange_metrics ]),
231+ fun (Q ) -> delete_queue (Chan , Q ),
232+ force_metric_gc (Config )
233+ end }, [channel_queue_metrics ,
234+ channel_queue_exchange_metrics ]),
228235 ok = rabbit_ct_client_helpers :close_connection (Conn ),
229236 Result .
230237
@@ -240,7 +247,10 @@ queue_metric_count_channel_per_queue(Config, Ops) ->
240247 end ,
241248 Result = add_rem_counter (Config , Ops ,
242249 {AddFun ,
243- fun ({Chan , Q }) -> delete_queue (Chan , Q ) end },
250+ fun ({Chan , Q }) ->
251+ delete_queue (Chan , Q ),
252+ force_metric_gc (Config )
253+ end },
244254 [ channel_queue_metrics ,
245255 channel_queue_exchange_metrics ]),
246256 ok = rabbit_ct_client_helpers :close_connection (Conn ),
@@ -258,8 +268,10 @@ add_rem_counter(Config, {Initial, Ops}, {AddFun, RemFun}, Tables) ->
258268 (_ , S ) -> S end ,
259269 {Initial , Things },
260270 Ops ),
271+ force_metric_gc (Config ),
261272 TabLens = lists :map (fun (T ) ->
262- length (read_table_rpc (Config , T )) end , Tables ),
273+ length (read_table_rpc (Config , T ))
274+ end , Tables ),
263275 [RemFun (Thing ) || Thing <- Things1 ],
264276 [FinalLen ] == lists :usort (TabLens ).
265277
@@ -270,9 +282,11 @@ connection(Config) ->
270282 [_ ] = read_table_rpc (Config , connection_metrics ),
271283 [_ ] = read_table_rpc (Config , connection_coarse_metrics ),
272284 ok = rabbit_ct_client_helpers :close_connection (Conn ),
285+ force_metric_gc (Config ),
273286 [] = read_table_rpc (Config , connection_created ),
274287 [] = read_table_rpc (Config , connection_metrics ),
275- [] = read_table_rpc (Config , connection_coarse_metrics ).
288+ [] = read_table_rpc (Config , connection_coarse_metrics ),
289+ ok .
276290
277291channel (Config ) ->
278292 Conn = rabbit_ct_client_helpers :open_unmanaged_connection (Config ),
@@ -308,10 +322,12 @@ channel_queue_delete_queue(Config) ->
308322 [_ ] = read_table_rpc (Config , channel_queue_exchange_metrics ),
309323
310324 delete_queue (Chan , Queue ),
325+ force_metric_gc (Config ),
311326 % ensure removal of queue cleans up channel_queue metrics
312327 [] = read_table_rpc (Config , channel_queue_exchange_metrics ),
313328 [] = read_table_rpc (Config , channel_queue_metrics ),
314- ok = rabbit_ct_client_helpers :close_connection (Conn ).
329+ ok = rabbit_ct_client_helpers :close_connection (Conn ),
330+ ok .
315331
316332channel_queue_exchange_consumer_close_connection (Config ) ->
317333 Conn = rabbit_ct_client_helpers :open_unmanaged_connection (Config ),
@@ -335,10 +351,13 @@ channel_queue_exchange_consumer_close_connection(Config) ->
335351
336352 ok = rabbit_ct_client_helpers :close_connection (Conn ),
337353 % ensure cleanup happened
354+ force_metric_gc (Config ),
338355 [] = read_table_rpc (Config , channel_exchange_metrics ),
339356 [] = read_table_rpc (Config , channel_queue_exchange_metrics ),
340357 [] = read_table_rpc (Config , channel_queue_metrics ),
341- [] = read_table_rpc (Config , consumer_created ).
358+ [] = read_table_rpc (Config , consumer_created ),
359+ ok .
360+
342361
343362
344363% % -------------------------------------------------------------------
@@ -371,6 +390,16 @@ force_channel_stats(Config) ->
371390read_table_rpc (Config , Table ) ->
372391 rabbit_ct_broker_helpers :rpc (Config , 0 , ? MODULE , read_table , [Table ]).
373392
393+ clean_core_metrics (Config ) ->
394+ [ rabbit_ct_broker_helpers :rpc (Config , 0 , ets , delete_all_objects , [Table ])
395+ || {Table , _ } <- ? CORE_TABLES ].
396+
374397read_table (Table ) ->
375398 ets :tab2list (Table ).
376399
400+ force_metric_gc (Config ) ->
401+ timer :sleep (300 ),
402+ rabbit_ct_broker_helpers :rpc (Config , 0 , erlang , send ,
403+ [rabbit_core_metrics_gc , start_gc ]),
404+ rabbit_ct_broker_helpers :rpc (Config , 0 , gen_server , call ,
405+ [rabbit_core_metrics_gc , test ]).
0 commit comments