From beadead276b1849b921f222ba32b62f393a5e99c Mon Sep 17 00:00:00 2001 From: Stephan Bosch Date: Mon, 27 Oct 2025 22:04:45 +0100 Subject: [PATCH] lib: test-file-create-locked - Use the new test-subprocess API --- src/lib/test-file-create-locked.c | 69 ++++++++++++++++++------------- src/lib/test-lib.c | 1 + src/lib/test-lib.h | 1 + 3 files changed, 43 insertions(+), 28 deletions(-) diff --git a/src/lib/test-file-create-locked.c b/src/lib/test-file-create-locked.c index f5b4f6a991..d96d8c76f5 100644 --- a/src/lib/test-file-create-locked.c +++ b/src/lib/test-file-create-locked.c @@ -39,7 +39,7 @@ static bool wait_for_file(pid_t pid, const char *path) return FALSE; } -static void test_file_create_locked_basic(void) +static int test_file_create_locked_basic_child(void *context ATTR_UNUSED) { struct file_create_settings set = { .lock_timeout_secs = 0, @@ -48,43 +48,56 @@ static void test_file_create_locked_basic(void) }, }; const char *path = ".test-file-create-locked"; - struct file_lock *lock; + struct file_lock *lock = NULL; const char *error; bool created; - pid_t pid; int fd; + /* child */ + fd = file_create_locked(path, &set, &lock, &created, &error); + test_assert(fd > 0); + test_assert(created); + if (test_has_failed()) + return 1; + create_file(".test-temp-file-create-locked-child"); + i_sleep_intr_secs(60); + if (lock != NULL) + file_unlock(&lock); + i_close_fd(&fd); + return 0; +} + +static void test_file_create_locked_basic(void) +{ + struct file_create_settings set = { + .lock_timeout_secs = 0, + .lock_settings = { + .lock_method = FILE_LOCK_METHOD_FCNTL, + }, + }; + const char *path = ".test-file-create-locked"; + struct file_lock *lock = NULL; + const char *error; + pid_t pid; + bool created; + test_begin("file_create_locked()"); i_unlink_if_exists(path); i_unlink_if_exists(".test-temp-file-create-locked-child"); - pid = fork(); - switch (pid) { - case (pid_t)-1: - i_error("fork() failed: %m"); - break; - case 0: - /* child */ - fd = file_create_locked(path, &set, &lock, &created, &error); - test_assert(fd > 0); - test_assert(created); - if (test_has_failed()) - lib_exit(1); - create_file(".test-temp-file-create-locked-child"); - sleep(60); - i_close_fd(&fd); - lib_exit(0); - default: - /* parent */ - test_assert(wait_for_file(pid, ".test-temp-file-create-locked-child")); - if (test_has_failed()) - break; - test_assert(file_create_locked(path, &set, &lock, &created, &error) == -1); + pid = test_subprocess_fork(test_file_create_locked_basic_child, NULL, + TRUE); + + /* parent */ + test_assert(wait_for_file(pid, ".test-temp-file-create-locked-child")); + if (!test_has_failed()) { + test_assert(file_create_locked(path, &set, + &lock, &created, &error) == -1); test_assert(errno == EAGAIN); - if (kill(pid, SIGKILL) < 0) - i_error("kill(SIGKILL) failed: %m"); - break; + if (lock != NULL) + file_unlock(&lock); } + test_subprocess_kill_all(20); i_unlink_if_exists(".test-temp-file-create-locked-child"); i_unlink_if_exists(path); test_end(); diff --git a/src/lib/test-lib.c b/src/lib/test-lib.c index 6ca3367e96..73b41bb17d 100644 --- a/src/lib/test-lib.c +++ b/src/lib/test-lib.c @@ -26,6 +26,7 @@ int main(int argc, char **argv) }; test_dir_init("lib"); + test_subprocesses_init(); return test_run_named_with_fatals(match, test_functions, fatal_functions); } diff --git a/src/lib/test-lib.h b/src/lib/test-lib.h index 02867b8430..4f2ad1ef04 100644 --- a/src/lib/test-lib.h +++ b/src/lib/test-lib.h @@ -4,6 +4,7 @@ #include "lib.h" #include "test-common.h" #include "test-dir.h" +#include "test-subprocess.h" #define TEST(x) TEST_DECL(x) #define FATAL(x) FATAL_DECL(x) -- 2.47.3