]> git.ipfire.org Git - thirdparty/systemd.git/commitdiff
test: introduce test cases for journal_ratelimit_test()
authorYu Watanabe <watanabe.yu+github@gmail.com>
Sun, 12 May 2024 01:31:41 +0000 (10:31 +0900)
committerYu Watanabe <watanabe.yu+github@gmail.com>
Sun, 12 May 2024 01:31:41 +0000 (10:31 +0900)
src/journal/meson.build
src/journal/test-journald-rate-limit.c [new file with mode: 0644]

index 7d87754e0e565c858d8935042f1b1e06443b2eff..9f0e6995018d648583f47632a262d9faa36f170a 100644 (file)
@@ -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 (file)
index 0000000..89d69bb
--- /dev/null
@@ -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);