|
| 1 | +// SPDX-License-Identifier: GPL-2.0-only |
| 2 | + |
| 3 | +#include <kunit/test.h> |
| 4 | + |
| 5 | +#include <linux/ratelimit.h> |
| 6 | +#include <linux/module.h> |
| 7 | + |
| 8 | +/* a simple boot-time regression test */ |
| 9 | + |
| 10 | +#define TESTRL_INTERVAL (5 * HZ) |
| 11 | +static DEFINE_RATELIMIT_STATE(testrl, TESTRL_INTERVAL, 3); |
| 12 | + |
| 13 | +#define test_ratelimited(test, expected) \ |
| 14 | + KUNIT_ASSERT_EQ(test, ___ratelimit(&testrl, "test_ratelimit_smoke"), (expected)) |
| 15 | + |
| 16 | +static void test_ratelimit_smoke(struct kunit *test) |
| 17 | +{ |
| 18 | + // Check settings. |
| 19 | + KUNIT_ASSERT_GE(test, TESTRL_INTERVAL, 100); |
| 20 | + |
| 21 | + // Test normal operation. |
| 22 | + test_ratelimited(test, true); |
| 23 | + test_ratelimited(test, true); |
| 24 | + test_ratelimited(test, true); |
| 25 | + test_ratelimited(test, false); |
| 26 | + |
| 27 | + schedule_timeout_idle(TESTRL_INTERVAL - 40); |
| 28 | + test_ratelimited(test, false); |
| 29 | + |
| 30 | + schedule_timeout_idle(50); |
| 31 | + test_ratelimited(test, true); |
| 32 | + |
| 33 | + schedule_timeout_idle(2 * TESTRL_INTERVAL); |
| 34 | + test_ratelimited(test, true); |
| 35 | + test_ratelimited(test, true); |
| 36 | + |
| 37 | + schedule_timeout_idle(TESTRL_INTERVAL - 40); |
| 38 | + test_ratelimited(test, true); |
| 39 | + schedule_timeout_idle(50); |
| 40 | + test_ratelimited(test, true); |
| 41 | + test_ratelimited(test, true); |
| 42 | + test_ratelimited(test, true); |
| 43 | + test_ratelimited(test, false); |
| 44 | + |
| 45 | + // Test disabling. |
| 46 | + testrl.burst = 0; |
| 47 | + test_ratelimited(test, false); |
| 48 | + testrl.burst = 2; |
| 49 | + testrl.interval = 0; |
| 50 | + test_ratelimited(test, true); |
| 51 | + test_ratelimited(test, true); |
| 52 | + test_ratelimited(test, true); |
| 53 | + test_ratelimited(test, true); |
| 54 | + test_ratelimited(test, true); |
| 55 | + test_ratelimited(test, true); |
| 56 | + test_ratelimited(test, true); |
| 57 | + |
| 58 | + // Testing re-enabling. |
| 59 | + testrl.interval = TESTRL_INTERVAL; |
| 60 | + test_ratelimited(test, true); |
| 61 | + test_ratelimited(test, true); |
| 62 | + test_ratelimited(test, false); |
| 63 | + test_ratelimited(test, false); |
| 64 | +} |
| 65 | + |
| 66 | +static struct kunit_case sort_test_cases[] = { |
| 67 | + KUNIT_CASE_SLOW(test_ratelimit_smoke), |
| 68 | + {} |
| 69 | +}; |
| 70 | + |
| 71 | +static struct kunit_suite ratelimit_test_suite = { |
| 72 | + .name = "lib_ratelimit", |
| 73 | + .test_cases = sort_test_cases, |
| 74 | +}; |
| 75 | + |
| 76 | +kunit_test_suites(&ratelimit_test_suite); |
| 77 | + |
| 78 | +MODULE_DESCRIPTION("___ratelimit() KUnit test suite"); |
| 79 | +MODULE_LICENSE("GPL"); |
0 commit comments