From 99bf25db3ce756f911236b344a0b855a12e57031 Mon Sep 17 00:00:00 2001 From: Beniamino Galvani Date: Tue, 18 Nov 2025 13:51:26 +0100 Subject: [PATCH] core: fix rate-limit test failures It's possible that the first timeout gets delayed; therefore the interval between the first and the second callback can be less than one second, and the budget doesn't refill completely. Schedule the second timeout from the first callback to guarantee that at least one second passes between the callbacks. Fixes: ff0c4346fc0c ('core: add rate-limiting helper') --- src/core/tests/test-utils.c | 13 +++++-------- 1 file changed, 5 insertions(+), 8 deletions(-) diff --git a/src/core/tests/test-utils.c b/src/core/tests/test-utils.c index ec760cf171..0418d3ae89 100644 --- a/src/core/tests/test-utils.c +++ b/src/core/tests/test-utils.c @@ -264,8 +264,7 @@ test_shorten_hostname(void) typedef struct { NMRateLimit ratelimit; GMainLoop *loop; - GSource *source1; - GSource *source2; + GSource *source; guint num; } RateLimitData; @@ -283,10 +282,9 @@ rate_limit_window_expire_cb(gpointer user_data) g_assert(!nm_rate_limit_check(&data->ratelimit, 1, 5)); g_assert(!nm_rate_limit_check(&data->ratelimit, 1, 5)); + nm_clear_g_source_inst(&data->source); g_main_loop_quit(data->loop); - nm_clear_g_source_inst(&data->source1); - return G_SOURCE_CONTINUE; } @@ -304,7 +302,8 @@ rate_limit_check_cb(gpointer user_data) g_assert(!nm_rate_limit_check(&data->ratelimit, 1, 5)); g_assert(!nm_rate_limit_check(&data->ratelimit, 1, 5)); - nm_clear_g_source_inst(&data->source2); + nm_clear_g_source_inst(&data->source); + data->source = nm_g_timeout_add_source(1000, rate_limit_window_expire_cb, data); return G_SOURCE_CONTINUE; } @@ -317,12 +316,10 @@ test_rate_limit_check(void) data = (RateLimitData) { .loop = g_main_loop_new(NULL, FALSE), .ratelimit = {}, + .source = nm_g_timeout_add_source(1, rate_limit_check_cb, &data), .num = 0, }; - data.source1 = nm_g_timeout_add_source(1100, rate_limit_window_expire_cb, &data); - data.source2 = nm_g_timeout_add_source(10, rate_limit_check_cb, &data); - g_main_loop_run(data.loop); g_main_loop_unref(data.loop); }