diff options
author | David Herrmann <dh.herrmann@gmail.com> | 2014-11-05 13:32:16 +0100 |
---|---|---|
committer | Peter Hutterer <peter.hutterer@who-t.net> | 2014-11-07 08:30:50 +1000 |
commit | 33ac7436a1ff14e4170ffcee5a155b6bae4e0ae3 (patch) | |
tree | 4eab84b141e7ef00020262a1b8132b4431fc64cc /test | |
parent | d2842893a828407e3c4d2459a382f8c7179b2f51 (diff) | |
download | libinput-33ac7436a1ff14e4170ffcee5a155b6bae4e0ae3.tar.gz |
util: introduce ratelimit helpers
This adds "struct ratelimit" and "ratelimit_test()". It's a very simple
rate-limit helper modeled after Linux' lib/ratelimit.c by Dave Young.
This comes in handy to limit log-messages in possible busy loops etc..
Signed-off-by: David Herrmann <dh.herrmann@gmail.com>
Reviewed-by: Peter Hutterer <peter.hutterer@who-t.net>
Signed-off-by: Peter Hutterer <peter.hutterer@who-t.net>
Diffstat (limited to 'test')
-rw-r--r-- | test/misc.c | 42 |
1 files changed, 42 insertions, 0 deletions
diff --git a/test/misc.c b/test/misc.c index 1512180e..4ed9dce8 100644 --- a/test/misc.c +++ b/test/misc.c @@ -508,6 +508,46 @@ START_TEST(matrix_helpers) } END_TEST +START_TEST(ratelimit_helpers) +{ + struct ratelimit rl; + unsigned int i, j; + + /* 10 attempts every 100ms */ + ratelimit_init(&rl, 100, 10); + + for (j = 0; j < 3; ++j) { + /* a burst of 9 attempts must succeed */ + for (i = 0; i < 9; ++i) { + ck_assert_int_eq(ratelimit_test(&rl), + RATELIMIT_PASS); + } + + /* the 10th attempt reaches the threshold */ + ck_assert_int_eq(ratelimit_test(&rl), RATELIMIT_THRESHOLD); + + /* ..then further attempts must fail.. */ + ck_assert_int_eq(ratelimit_test(&rl), RATELIMIT_EXCEEDED); + + /* ..regardless of how often we try. */ + for (i = 0; i < 100; ++i) { + ck_assert_int_eq(ratelimit_test(&rl), + RATELIMIT_EXCEEDED); + } + + /* ..even after waiting 20ms */ + msleep(20); + for (i = 0; i < 100; ++i) { + ck_assert_int_eq(ratelimit_test(&rl), + RATELIMIT_EXCEEDED); + } + + /* but after 100ms the counter is reset */ + msleep(90); /* +10ms to account for time drifts */ + } +} +END_TEST + int main (int argc, char **argv) { litest_add_no_device("events:conversion", event_conversion_device_notify); litest_add_no_device("events:conversion", event_conversion_pointer); @@ -519,5 +559,7 @@ int main (int argc, char **argv) { litest_add_no_device("config:status string", config_status_string); litest_add_no_device("misc:matrix", matrix_helpers); + litest_add_no_device("misc:ratelimit", ratelimit_helpers); + return litest_run(argc, argv); } |