]> git.ipfire.org Git - thirdparty/systemd.git/commitdiff
test-async: wait a while for fd to be closed
authorYu Watanabe <watanabe.yu+github@gmail.com>
Sun, 2 Jun 2024 00:10:03 +0000 (09:10 +0900)
committerYu Watanabe <watanabe.yu+github@gmail.com>
Sun, 2 Jun 2024 00:10:06 +0000 (09:10 +0900)
When this is run on slow environment, e.g. sanitizer is enabled,
then waiting for 1 second may not be enough.

Hopefully fixes #33150.

src/test/test-async.c

index 4076213d83463dd66e3681786b92c2dfad756f0b..2771cbd5aa3dc4d9d7e303045f7dffe315fa455a 100644 (file)
@@ -10,6 +10,7 @@
 #include "path-util.h"
 #include "process-util.h"
 #include "signal-util.h"
+#include "time-util.h"
 #include "tests.h"
 #include "tmpfile-util.h"
 
@@ -17,6 +18,18 @@ TEST(asynchronous_sync) {
         ASSERT_OK(asynchronous_sync(NULL));
 }
 
+static void wait_fd_closed(int fd) {
+        for (unsigned trial = 0; trial < 100; trial++) {
+                usleep_safe(100 * USEC_PER_MSEC);
+                if (fcntl(fd, F_GETFD) < 0) {
+                        assert_se(errno == EBADF);
+                        return;
+                }
+        }
+
+        assert_not_reached();
+}
+
 TEST(asynchronous_close) {
         _cleanup_(unlink_tempfilep) char name[] = "/tmp/test-asynchronous_close.XXXXXX";
         int fd, r;
@@ -24,11 +37,7 @@ TEST(asynchronous_close) {
         fd = mkostemp_safe(name);
         ASSERT_OK(fd);
         asynchronous_close(fd);
-
-        sleep(1);
-
-        ASSERT_EQ(fcntl(fd, F_GETFD), -1);
-        assert_se(errno == EBADF);
+        wait_fd_closed(fd);
 
         r = safe_fork("(subreaper)", FORK_RESET_SIGNALS|FORK_CLOSE_ALL_FDS|FORK_DEATHSIG_SIGKILL|FORK_LOG|FORK_WAIT, NULL);
         ASSERT_OK(r);
@@ -41,11 +50,7 @@ TEST(asynchronous_close) {
                 fd = open("/dev/null", O_RDONLY|O_CLOEXEC);
                 ASSERT_OK(fd);
                 asynchronous_close(fd);
-
-                sleep(1);
-
-                ASSERT_EQ(fcntl(fd, F_GETFD), -1);
-                assert_se(errno == EBADF);
+                wait_fd_closed(fd);
 
                 _exit(EXIT_SUCCESS);
         }