]> git.ipfire.org Git - thirdparty/systemd.git/commitdiff
test-pressure: set timeout to make not wait forever
authorYu Watanabe <watanabe.yu+github@gmail.com>
Wed, 27 May 2026 15:33:35 +0000 (00:33 +0900)
committerLuca Boccassi <luca.boccassi@gmail.com>
Wed, 27 May 2026 20:34:04 +0000 (21:34 +0100)
If this runs on a slow or busy machine, then we may not get enough
pressure to trigger the event sources. In such case, the test does not
finish. It is problematic when the test is _not_ run with 'meson test',
e.g. debian/ubuntu CIs.

Let's introduce a timeout for each event loop, and skip test cases
gracefully.

src/test/test-pressure.c

index 7e89032545ee3261ade5b3b172cd6e08d19cc4dd..c1305edac012bbb7f793b58f60af4f958273011d 100644 (file)
 
 /* Shared infrastructure for fake pressure tests */
 
+static int event_loop_with_timeout(sd_event *event) {
+        ASSERT_NOT_NULL(event);
+
+        /* Note, the default meson test timeout is 30 seconds. */
+        ASSERT_OK(sd_event_add_time_relative(
+                                  event,
+                                  /* ret= */ NULL,
+                                  CLOCK_MONOTONIC,
+                                  20 * USEC_PER_SEC,
+                                  /* accuracy= */ 0,
+                                  /* callback= */ NULL,
+                                  INT_TO_PTR(-ETIMEDOUT)));
+
+        if (ASSERT_OK_OR(sd_event_loop(event), -ETIMEDOUT) < 0)
+                return log_tests_skipped("event loop timeout");
+
+        return 0;
+}
+
 struct fake_pressure_context {
         int fifo_fd;
         int socket_fd;
@@ -133,7 +152,11 @@ static void test_fake_pressure(
         ASSERT_OK(add_pressure(e, &ef, fake_pressure_callback, &value));
         ASSERT_OK(sd_event_source_set_description(ef, "socket event source"));
 
-        ASSERT_OK(sd_event_loop(e));
+        if (event_loop_with_timeout(e) != 0) {
+                (void) pthread_cancel(th);
+                (void) pthread_join(th, /* retval= */ NULL);
+                return;
+        }
 
         ASSERT_EQ(value, 7 * 'f' * 's');
 
@@ -363,7 +386,9 @@ TEST(real_memory_pressure) {
         /* Now start eating memory */
         ASSERT_EQ(write(pipe_fd[1], &(const char) { 'x' }, 1), 1);
 
-        ASSERT_OK(sd_event_loop(e));
+        if (event_loop_with_timeout(e) != 0)
+                return;
+
         int ex = 0;
         ASSERT_OK(sd_event_get_exit_code(e, &ex));
         ASSERT_EQ(ex, 31);
@@ -485,7 +510,9 @@ TEST(real_cpu_pressure) {
         /* Now start eating CPU */
         ASSERT_EQ(write(pipe_fd[1], &(const char) { 'x' }, 1), 1);
 
-        ASSERT_OK(sd_event_loop(e));
+        if (event_loop_with_timeout(e) != 0)
+                return;
+
         int ex = 0;
         ASSERT_OK(sd_event_get_exit_code(e, &ex));
         ASSERT_EQ(ex, 31);
@@ -624,7 +651,9 @@ TEST(real_io_pressure) {
         /* Now start eating IO */
         ASSERT_EQ(write(pipe_fd[1], &(const char) { 'x' }, 1), 1);
 
-        ASSERT_OK(sd_event_loop(e));
+        if (event_loop_with_timeout(e) != 0)
+                return;
+
         int ex = 0;
         ASSERT_OK(sd_event_get_exit_code(e, &ex));
         ASSERT_EQ(ex, 31);