summaryrefslogtreecommitdiffstats
path: root/src/journal
diff options
context:
space:
mode:
authorYu Watanabe <watanabe.yu+github@gmail.com>2024-05-12 03:31:41 +0200
committerYu Watanabe <watanabe.yu+github@gmail.com>2024-05-12 03:31:41 +0200
commit78da0721c84b0f63da621d6a00b8ab01cb0b979b (patch)
treec62857d7fe1adf01e0c33de4a7915dd900eb5f98 /src/journal
parenttree-wide: use LOG_PRI() and LOG_FAC() (diff)
downloadsystemd-78da0721c84b0f63da621d6a00b8ab01cb0b979b.tar.xz
systemd-78da0721c84b0f63da621d6a00b8ab01cb0b979b.zip
test: introduce test cases for journal_ratelimit_test()
Diffstat (limited to 'src/journal')
-rw-r--r--src/journal/meson.build6
-rw-r--r--src/journal/test-journald-rate-limit.c44
2 files changed, 50 insertions, 0 deletions
diff --git a/src/journal/meson.build b/src/journal/meson.build
index 7d87754e0e..9f0e699501 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 0000000000..89d69bb950
--- /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);