b677e7
From c35ba62cd6f337c4eef64cdc3b9796f988802229 Mon Sep 17 00:00:00 2001
b677e7
From: =?UTF-8?q?Michal=20Sekleta=CC=81r?= <msekleta@redhat.com>
b677e7
Date: Mon, 23 Nov 2020 18:04:57 +0100
b677e7
Subject: [PATCH] test: add ratelimiting test
b677e7
b677e7
(Taken from Michal's #17274 by Lennart, and slightly adjusted)
b677e7
b677e7
(cherry picked from commit 68d890651781904a4c762ac866af36e30c4f7ff8)
b677e7
b677e7
Related: #1819868
b677e7
---
b677e7
 src/libsystemd/sd-event/test-event.c | 96 ++++++++++++++++++++++++++++
b677e7
 1 file changed, 96 insertions(+)
b677e7
b677e7
diff --git a/src/libsystemd/sd-event/test-event.c b/src/libsystemd/sd-event/test-event.c
b677e7
index bde00cf719..e3ee4cd5c3 100644
b677e7
--- a/src/libsystemd/sd-event/test-event.c
b677e7
+++ b/src/libsystemd/sd-event/test-event.c
b677e7
@@ -482,6 +482,100 @@ static void test_inotify(unsigned n_create_events) {
b677e7
         sd_event_unref(e);
b677e7
 }
b677e7
 
b677e7
+
b677e7
+static int ratelimit_io_handler(sd_event_source *s, int fd, uint32_t revents, void *userdata) {
b677e7
+        unsigned *c = (unsigned*) userdata;
b677e7
+        *c += 1;
b677e7
+        return 0;
b677e7
+}
b677e7
+
b677e7
+static int ratelimit_time_handler(sd_event_source *s, uint64_t usec, void *userdata) {
b677e7
+        int r;
b677e7
+
b677e7
+        r = sd_event_source_set_enabled(s, SD_EVENT_ON);
b677e7
+        if (r < 0)
b677e7
+                log_warning_errno(r, "Failed to turn on notify event source: %m");
b677e7
+
b677e7
+        r = sd_event_source_set_time(s, usec + 1000);
b677e7
+        if (r < 0)
b677e7
+                log_error_errno(r, "Failed to restart watchdog event source: %m");
b677e7
+
b677e7
+        unsigned *c = (unsigned*) userdata;
b677e7
+        *c += 1;
b677e7
+
b677e7
+        return 0;
b677e7
+}
b677e7
+
b677e7
+static void test_ratelimit(void) {
b677e7
+        _cleanup_close_pair_ int p[2] = {-1, -1};
b677e7
+        _cleanup_(sd_event_unrefp) sd_event *e = NULL;
b677e7
+        _cleanup_(sd_event_source_unrefp) sd_event_source *s = NULL;
b677e7
+        uint64_t interval;
b677e7
+        unsigned count, burst;
b677e7
+
b677e7
+        assert_se(sd_event_default(&e) >= 0);
b677e7
+        assert_se(pipe2(p, O_CLOEXEC|O_NONBLOCK) >= 0);
b677e7
+
b677e7
+        assert_se(sd_event_add_io(e, &s, p[0], EPOLLIN, ratelimit_io_handler, &count) >= 0);
b677e7
+        assert_se(sd_event_source_set_description(s, "test-ratelimit-io") >= 0);
b677e7
+        assert_se(sd_event_source_set_ratelimit(s, 1 * USEC_PER_SEC, 5) >= 0);
b677e7
+        assert_se(sd_event_source_get_ratelimit(s, &interval, &burst) >= 0);
b677e7
+        assert_se(interval == 1 * USEC_PER_SEC && burst == 5);
b677e7
+
b677e7
+        assert_se(write(p[1], "1", 1) == 1);
b677e7
+
b677e7
+        count = 0;
b677e7
+        for (unsigned i = 0; i < 10; i++) {
b677e7
+                log_debug("slow loop iteration %u", i);
b677e7
+                assert_se(sd_event_run(e, UINT64_MAX) >= 0);
b677e7
+                assert_se(usleep(250 * USEC_PER_MSEC) >= 0);
b677e7
+        }
b677e7
+
b677e7
+        assert_se(sd_event_source_is_ratelimited(s) == 0);
b677e7
+        assert_se(count == 10);
b677e7
+        log_info("ratelimit_io_handler: called %d times, event source not ratelimited", count);
b677e7
+
b677e7
+        assert_se(sd_event_source_set_ratelimit(s, 0, 0) >= 0);
b677e7
+        assert_se(sd_event_source_set_ratelimit(s, 1 * USEC_PER_SEC, 5) >= 0);
b677e7
+
b677e7
+        count = 0;
b677e7
+        for (unsigned i = 0; i < 10; i++) {
b677e7
+                log_debug("fast event loop iteration %u", i);
b677e7
+                assert_se(sd_event_run(e, UINT64_MAX) >= 0);
b677e7
+                assert_se(usleep(10) >= 0);
b677e7
+        }
b677e7
+        log_info("ratelimit_io_handler: called %d times, event source got ratelimited", count);
b677e7
+        assert_se(count < 10);
b677e7
+
b677e7
+        s = sd_event_source_unref(s);
b677e7
+        safe_close_pair(p);
b677e7
+
b677e7
+        count = 0;
b677e7
+
b677e7
+        assert_se(sd_event_add_time(e, &s, CLOCK_MONOTONIC, now(CLOCK_MONOTONIC) + 1000, 0, ratelimit_time_handler, &count) >= 0);
b677e7
+        assert_se(sd_event_source_set_ratelimit(s, 1 * USEC_PER_SEC, 10) == 0);
b677e7
+
b677e7
+        do {
b677e7
+                assert_se(sd_event_run(e, UINT64_MAX) >= 0);
b677e7
+        } while (!sd_event_source_is_ratelimited(s));
b677e7
+
b677e7
+        log_info("ratelimit_time_handler: called %d times, event source got ratelimited", count);
b677e7
+        assert_se(count == 10);
b677e7
+
b677e7
+        /* In order to get rid of active rate limit client needs to disable it explicitely */
b677e7
+        assert_se(sd_event_source_set_ratelimit(s, 0, 0) >= 0);
b677e7
+        assert_se(!sd_event_source_is_ratelimited(s));
b677e7
+
b677e7
+        assert_se(sd_event_source_set_ratelimit(s, 1 * USEC_PER_SEC, 10) >= 0);
b677e7
+
b677e7
+        do {
b677e7
+                assert_se(sd_event_run(e, UINT64_MAX) >= 0);
b677e7
+        } while (!sd_event_source_is_ratelimited(s));
b677e7
+
b677e7
+        log_info("ratelimit_time_handler: called 10 more times, event source got ratelimited");
b677e7
+        assert_se(count == 20);
b677e7
+}
b677e7
+
b677e7
 int main(int argc, char *argv[]) {
b677e7
 
b677e7
         log_set_max_level(LOG_DEBUG);
b677e7
@@ -494,5 +588,7 @@ int main(int argc, char *argv[]) {
b677e7
         test_inotify(100); /* should work without overflow */
b677e7
         test_inotify(33000); /* should trigger a q overflow */
b677e7
 
b677e7
+        test_ratelimit();
b677e7
+
b677e7
         return 0;
b677e7
 }