Debian Codegraph @

src/journal/test-journald-rate-limit.c

raw
1 /* SPDX-License-Identifier: LGPL-2.1-or-later */
2
3 #include "hashmap.h"
4 #include "journald-rate-limit.h"
5 #include "tests.h"
6 #include "time-util.h"
7
8 TEST(journal_ratelimit_test) {
9 _cleanup_ordered_hashmap_free_ OrderedHashmap *rl = NULL;
10 int r;
11
12 for (unsigned i = 0; i < 20; i++) {
13 r = journal_ratelimit_test(&rl, "hoge", USEC_PER_SEC, 10, LOG_DEBUG, 0);
14 assert_se(r == (i < 10 ? 1 : 0));
15 r = journal_ratelimit_test(&rl, "foo", 10 * USEC_PER_SEC, 10, LOG_DEBUG, 0);
16 assert_se(r == (i < 10 ? 1 : 0));
17 }
18
19 /* Different priority group with the same ID is not ratelimited. */
20 assert_se(journal_ratelimit_test(&rl, "hoge", USEC_PER_SEC, 10, LOG_INFO, 0) == 1);
21 assert_se(journal_ratelimit_test(&rl, "foo", 10 * USEC_PER_SEC, 10, LOG_INFO, 0) == 1);
22 /* Still LOG_DEBUG is ratelimited. */
23 assert_se(journal_ratelimit_test(&rl, "hoge", USEC_PER_SEC, 10, LOG_DEBUG, 0) == 0);
24 assert_se(journal_ratelimit_test(&rl, "foo", 10 * USEC_PER_SEC, 10, LOG_DEBUG, 0) == 0);
25 /* Different ID is not ratelimited. */
26 assert_se(journal_ratelimit_test(&rl, "quux", USEC_PER_SEC, 10, LOG_DEBUG, 0) == 1);
27
28 usleep_safe(USEC_PER_SEC);
29
30 /* The ratelimit is now expired (11 trials are suppressed, so the return value should be 12). */
31 assert_se(journal_ratelimit_test(&rl, "hoge", USEC_PER_SEC, 10, LOG_DEBUG, 0) == 1 + 11);
32
33 /* foo is still ratelimited. */
34 assert_se(journal_ratelimit_test(&rl, "foo", 10 * USEC_PER_SEC, 10, LOG_DEBUG, 0) == 0);
35
36 /* Still other priority and/or other IDs are not ratelimited. */
37 assert_se(journal_ratelimit_test(&rl, "hoge", USEC_PER_SEC, 10, LOG_INFO, 0) == 1);
38 assert_se(journal_ratelimit_test(&rl, "foo", 10 * USEC_PER_SEC, 10, LOG_INFO, 0) == 1);
39 assert_se(journal_ratelimit_test(&rl, "quux", USEC_PER_SEC, 10, LOG_DEBUG, 0) == 1);
40 }
41
42 DEFINE_TEST_MAIN(LOG_INFO);