From: Yu Watanabe Date: Sun, 12 May 2024 01:31:41 +0000 (+0900) Subject: test: introduce test cases for journal_ratelimit_test() X-Git-Tag: v256-rc2~35^2~7 X-Git-Url: http://git.ipfire.org/gitweb.cgi?a=commitdiff_plain;h=78da0721c84b0f63da621d6a00b8ab01cb0b979b;p=thirdparty%2Fsystemd.git test: introduce test cases for journal_ratelimit_test() --- diff --git a/src/journal/meson.build b/src/journal/meson.build index 7d87754e0e5..9f0e6995018 100644 --- a/src/journal/meson.build +++ b/src/journal/meson.build @@ -122,6 +122,12 @@ executables += [ libxz_cflags, ], }, + test_template + { + 'sources' : files( + 'test-journald-rate-limit.c', + 'journald-rate-limit.c', + ), + }, journal_test_template + { 'sources' : files('test-journald-syslog.c'), 'dependencies' : [ diff --git a/src/journal/test-journald-rate-limit.c b/src/journal/test-journald-rate-limit.c new file mode 100644 index 00000000000..89d69bb950e --- /dev/null +++ b/src/journal/test-journald-rate-limit.c @@ -0,0 +1,44 @@ +/* SPDX-License-Identifier: LGPL-2.1-or-later */ + +#include "journald-rate-limit.h" +#include "tests.h" + +TEST(journal_ratelimit_test) { + JournalRateLimit *rl; + int r; + + assert_se(rl = journal_ratelimit_new()); + + for (unsigned i = 0; i < 20; i++) { + r = journal_ratelimit_test(rl, "hoge", USEC_PER_SEC, 10, LOG_DEBUG, 0); + assert_se(r == (i < 10 ? 1 : 0)); + r = journal_ratelimit_test(rl, "foo", 10 * USEC_PER_SEC, 10, LOG_DEBUG, 0); + assert_se(r == (i < 10 ? 1 : 0)); + } + + /* Different priority group with the same ID is not ratelimited. */ + assert_se(journal_ratelimit_test(rl, "hoge", USEC_PER_SEC, 10, LOG_INFO, 0) == 1); + assert_se(journal_ratelimit_test(rl, "foo", 10 * USEC_PER_SEC, 10, LOG_INFO, 0) == 1); + /* Still LOG_DEBUG is ratelimited. */ + assert_se(journal_ratelimit_test(rl, "hoge", USEC_PER_SEC, 10, LOG_DEBUG, 0) == 0); + assert_se(journal_ratelimit_test(rl, "foo", 10 * USEC_PER_SEC, 10, LOG_DEBUG, 0) == 0); + /* Different ID is not ratelimited. */ + assert_se(journal_ratelimit_test(rl, "quux", USEC_PER_SEC, 10, LOG_DEBUG, 0) == 1); + + usleep_safe(USEC_PER_SEC); + + /* The ratelimit is now expired (11 trials are suppressed, so the return value should be 12). */ + assert_se(journal_ratelimit_test(rl, "hoge", USEC_PER_SEC, 10, LOG_DEBUG, 0) == 1 + 11); + + /* foo is still ratelimited. */ + assert_se(journal_ratelimit_test(rl, "foo", 10 * USEC_PER_SEC, 10, LOG_DEBUG, 0) == 0); + + /* Still other priority and/or other IDs are not ratelimited. */ + assert_se(journal_ratelimit_test(rl, "hoge", USEC_PER_SEC, 10, LOG_INFO, 0) == 1); + assert_se(journal_ratelimit_test(rl, "foo", 10 * USEC_PER_SEC, 10, LOG_INFO, 0) == 1); + assert_se(journal_ratelimit_test(rl, "quux", USEC_PER_SEC, 10, LOG_DEBUG, 0) == 1); + + journal_ratelimit_free(rl); +} + +DEFINE_TEST_MAIN(LOG_INFO);