[PATCH libinput v2 1/2] util: introduce ratelimit helpers

David Herrmann dh.herrmann at gmail.com
Thu Nov 6 01:38:04 PST 2014


Hi

On Thu, Nov 6, 2014 at 6:51 AM, Peter Hutterer <peter.hutterer at who-t.net> wrote:
> On Wed, Nov 05, 2014 at 01:32:16PM +0100, David Herrmann wrote:
>> diff --git a/test/misc.c b/test/misc.c
>> index 1512180..aa411ec 100644
>> --- a/test/misc.c
>> +++ b/test/misc.c
>> @@ -508,6 +508,40 @@ START_TEST(matrix_helpers)
>>  }
>>  END_TEST
>>
>> +START_TEST(ratelimit_helpers)
>> +{
>> +     struct ratelimit rl;
>> +     unsigned int i, j;
>> +
>> +     /* 10 attempts every 10ms */
>> +     ratelimit_init(&rl, 10, 10);
>> +
>> +     for (j = 0; j < 100; ++j) {
>> +             /* a burst of 9 attempts must succeed */
>> +             for (i = 0; i < 9; ++i)
>> +                     ck_assert(ratelimit_test(&rl) == RATELIMIT_PASS);
>> +
>> +             /* the 10th attempt reaches the threshold */
>> +             ck_assert(ratelimit_test(&rl) == RATELIMIT_THRESHOLD);
>
> merged both, with changes to ck_assert_int_eq:
>                 ck_assert_int_eq(ratelimit_test(&rl), RATELIMIT_THRESHOLD);
>
> a bit quicker to debug when it fails, the error message will print both
> values so you get an idea of what the mismatch is.

Whoops, yeah, totally forgot about the other ck_assert_* macros.
Thanks for amending the changes!

Thanks
David

> thanks
>
> Cheers,
>    Peter
>
>
>> +
>> +             /* ..then further attempts must fail.. */
>> +             ck_assert(ratelimit_test(&rl) == RATELIMIT_EXCEEDED);
>> +
>> +             /* ..regardless of how often we try. */
>> +             for (i = 0; i < 100; ++i)
>> +                     ck_assert(ratelimit_test(&rl) == RATELIMIT_EXCEEDED);
>> +
>> +             /* ..even after waiting 5ms */
>> +             msleep(5);
>> +             for (i = 0; i < 100; ++i)
>> +                     ck_assert(ratelimit_test(&rl) == RATELIMIT_EXCEEDED);
>> +
>> +             /* but after 10ms the counter is reset */
>> +             msleep(6); /* +1ms 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 +553,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);
>>  }
>> --
>> 2.1.3
>>


More information about the wayland-devel mailing list