summaryrefslogtreecommitdiffstats
path: root/src/journal/test-journald-rate-limit.c
diff options
context:
space:
mode:
authorDaniel Baumann <daniel.baumann@progress-linux.org>2024-06-12 03:50:42 +0000
committerDaniel Baumann <daniel.baumann@progress-linux.org>2024-06-12 03:50:42 +0000
commit78e9bb837c258ac0ec7712b3d612cc2f407e731e (patch)
treef515d16b6efd858a9aeb5b0ef5d6f90bf288283d /src/journal/test-journald-rate-limit.c
parentAdding debian version 255.5-1. (diff)
downloadsystemd-78e9bb837c258ac0ec7712b3d612cc2f407e731e.tar.xz
systemd-78e9bb837c258ac0ec7712b3d612cc2f407e731e.zip
Merging upstream version 256.
Signed-off-by: Daniel Baumann <daniel.baumann@progress-linux.org>
Diffstat (limited to 'src/journal/test-journald-rate-limit.c')
-rw-r--r--src/journal/test-journald-rate-limit.c40
1 files changed, 40 insertions, 0 deletions
diff --git a/src/journal/test-journald-rate-limit.c b/src/journal/test-journald-rate-limit.c
new file mode 100644
index 0000000..a08faba
--- /dev/null
+++ b/src/journal/test-journald-rate-limit.c
@@ -0,0 +1,40 @@
+/* SPDX-License-Identifier: LGPL-2.1-or-later */
+
+#include "journald-rate-limit.h"
+#include "tests.h"
+
+TEST(journal_ratelimit_test) {
+ _cleanup_ordered_hashmap_free_ OrderedHashmap *rl = NULL;
+ int r;
+
+ 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);
+}
+
+DEFINE_TEST_MAIN(LOG_INFO);