ryantimwilson / rpms / systemd

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