From: Marco Bettini Date: Thu, 22 Dec 2022 16:06:17 +0000 (+0000) Subject: lib-smtp: unit tests - Use signals to ensure client doesn't start before server is... X-Git-Tag: 2.4.0~3179 X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=80725f29d97c15b7e0cd297669e5ef4a9900efd7;p=thirdparty%2Fdovecot%2Fcore.git lib-smtp: unit tests - Use signals to ensure client doesn't start before server is ready --- diff --git a/src/lib-smtp/test-smtp-client-errors.c b/src/lib-smtp/test-smtp-client-errors.c index 7bdd60d4c2..93b83ebeb6 100644 --- a/src/lib-smtp/test-smtp-client-errors.c +++ b/src/lib-smtp/test-smtp-client-errors.c @@ -21,6 +21,7 @@ #include "smtp-client-connection.h" #include "smtp-client-transaction.h" +#include #include #define CLIENT_PROGRESS_TIMEOUT 10 @@ -4204,6 +4205,7 @@ static int test_run_server(struct test_server_data *data) server_ssl_ctx = NULL; + test_subprocess_notify_signal_send_parent(SIGUSR1); ioloop = io_loop_create(); data->server_test(data->index); io_loop_destroy(&ioloop); @@ -4224,6 +4226,7 @@ static int test_run_dns(test_dns_init_t dns_test) if (debug) i_debug("PID=%s", my_pid); + test_subprocess_notify_signal_send_parent(SIGUSR1); ioloop = io_loop_create(); dns_test(); io_loop_destroy(&ioloop); @@ -4281,7 +4284,10 @@ test_run_client_server(const struct smtp_client_settings *client_set, /* Fork server */ fd_listen = fds[i]; + test_subprocess_notify_signal_reset(SIGUSR1); test_subprocess_fork(test_run_server, &data, FALSE); + test_subprocess_notify_signal_wait( + SIGUSR1, TEST_SIGNALS_DEFAULT_TIMEOUT_MS); i_close_fd(&fd_listen); } } @@ -4297,7 +4303,10 @@ test_run_client_server(const struct smtp_client_settings *client_set, /* Fork DNS service */ fd_listen = fd; + test_subprocess_notify_signal_reset(SIGUSR1); test_subprocess_fork(test_run_dns, dns_test, FALSE); + test_subprocess_notify_signal_wait(SIGUSR1, + TEST_SIGNALS_DEFAULT_TIMEOUT_MS); i_close_fd(&fd_listen); } diff --git a/src/lib-smtp/test-smtp-payload.c b/src/lib-smtp/test-smtp-payload.c index 68455037da..277d260021 100644 --- a/src/lib-smtp/test-smtp-payload.c +++ b/src/lib-smtp/test-smtp-payload.c @@ -25,6 +25,7 @@ #include #include +#include #include #include #include @@ -868,6 +869,7 @@ static int test_run_server(struct test_server_data *data) if (debug) i_debug("PID=%s", my_pid); + test_subprocess_notify_signal_send_parent(SIGUSR1); ioloop = io_loop_create(); test_server_init(server_set); io_loop_run(ioloop); @@ -929,7 +931,9 @@ test_run_client_server( /* Fork server */ test_open_server_fd(); + test_subprocess_notify_signal_reset(SIGUSR1); test_subprocess_fork(test_run_server, &data, FALSE); + test_subprocess_notify_signal_wait(SIGUSR1, TEST_SIGNALS_DEFAULT_TIMEOUT_MS); i_close_fd(&fd_listen); /* Run client */ diff --git a/src/lib-smtp/test-smtp-server-errors.c b/src/lib-smtp/test-smtp-server-errors.c index b944a3968d..5990d21786 100644 --- a/src/lib-smtp/test-smtp-server-errors.c +++ b/src/lib-smtp/test-smtp-server-errors.c @@ -16,6 +16,7 @@ #include "smtp-reply-parser.h" #include "smtp-server.h" +#include #include #define VALGRIND_TIMEOUT_MULTIPLIER (ON_VALGRIND ? 5 : 1) @@ -3769,8 +3770,13 @@ static int test_run_client(struct test_client_data *data) if (debug) i_debug("PID=%s", my_pid); - /* wait a little for server setup */ - i_sleep_msecs(100); + test_subprocess_notify_signal_reset(SIGUSR1); + + /* signal server that we started */ + test_subprocess_notify_signal_send_parent(SIGUSR1); + + /* wait server to be ready */ + test_subprocess_notify_signal_wait(SIGUSR1, TEST_SIGNALS_DEFAULT_TIMEOUT_MS); ioloop = io_loop_create(); data->client_test(data->index); @@ -3796,6 +3802,9 @@ test_run_server(const struct smtp_server_settings *server_set, i_zero(&server_callbacks); server_pending = client_tests_count; + + /* signal clients that server is ready */ + test_subprocess_notify_signal_all(SIGUSR1); ioloop = io_loop_create(); server_test(server_set); io_loop_destroy(&ioloop); @@ -3824,8 +3833,10 @@ test_run_client_server(const struct smtp_server_settings *server_set, data.client_test = client_test; /* Fork client */ + test_subprocess_notify_signal_reset(SIGUSR1); test_subprocess_fork(test_run_client, &data, FALSE); - + test_subprocess_notify_signal_wait( + SIGUSR1, TEST_SIGNALS_DEFAULT_TIMEOUT_MS); } /* Run server */ diff --git a/src/lib-smtp/test-smtp-submit.c b/src/lib-smtp/test-smtp-submit.c index 3aff35b8a4..76c2d6237a 100644 --- a/src/lib-smtp/test-smtp-submit.c +++ b/src/lib-smtp/test-smtp-submit.c @@ -22,6 +22,7 @@ #include #include +#include #include #define SERVER_KILL_TIMEOUT_SECS 20 @@ -2070,6 +2071,7 @@ static int test_run_server(struct test_server_data *data) if (debug) i_debug("PID=%s", my_pid); + test_subprocess_notify_signal_send_parent(SIGUSR1); ioloop = io_loop_create(); data->server_test(data->index); io_loop_destroy(&ioloop); @@ -2131,7 +2133,10 @@ test_run_client_server(const struct smtp_submit_settings *submit_set, /* Fork server */ fd_listen = fds[i]; + test_subprocess_notify_signal_reset(SIGUSR1); test_subprocess_fork(test_run_server, &data, FALSE); + test_subprocess_notify_signal_wait( + SIGUSR1, TEST_SIGNALS_DEFAULT_TIMEOUT_MS); i_close_fd(&fd_listen); } } diff --git a/src/lib-test/test-subprocess.c b/src/lib-test/test-subprocess.c index eeaae801b3..cb906e48fd 100644 --- a/src/lib-test/test-subprocess.c +++ b/src/lib-test/test-subprocess.c @@ -23,6 +23,8 @@ static volatile bool test_subprocess_notification_signal_received[SIGUSR1 + 1]; static struct event *test_subprocess_event = NULL; static ARRAY(struct test_subprocess *) test_subprocesses = ARRAY_INIT; static void (*test_subprocess_cleanup_callback)(void) = NULL; +static void +test_subprocess_notification_signal(const siginfo_t *si, void *context); static void test_subprocess_signal(const siginfo_t *si ATTR_UNUSED, @@ -63,6 +65,8 @@ test_subprocess_child(int (*func)(void *context), void *context, lib_signals_set_handler(SIGINT, LIBSIG_FLAG_DELAYED | LIBSIG_FLAG_IOLOOP_AUTOMOVE, test_subprocess_signal, NULL); + lib_signals_set_handler(SIGUSR1, LIBSIG_FLAG_RESTART, + test_subprocess_notification_signal, NULL); ret = func(context); @@ -312,7 +316,7 @@ void test_subprocess_set_cleanup_callback(void (*callback)(void)) void test_subprocess_notify_signal_send(int signo, pid_t pid) { if (kill(pid, signo) < 0) - i_fatal("kill(%ld, SIGHUP) failed: %m", (long)pid); + i_fatal("kill(%ld, SIG %d) failed: %m", (long)pid, signo); } void test_subprocess_notify_signal_send_parent(int signo) @@ -320,6 +324,13 @@ void test_subprocess_notify_signal_send_parent(int signo) test_subprocess_notify_signal_send(signo, getppid()); } +void test_subprocess_notify_signal_all(int signo) +{ + struct test_subprocess *subprocess; + array_foreach_elem(&test_subprocesses, subprocess) + test_subprocess_notify_signal_send(signo, subprocess->pid); +} + void test_subprocess_notify_signal_reset(int signo) { i_assert(signo >= 0 && diff --git a/src/lib-test/test-subprocess.h b/src/lib-test/test-subprocess.h index 598b735299..8742847871 100644 --- a/src/lib-test/test-subprocess.h +++ b/src/lib-test/test-subprocess.h @@ -27,8 +27,10 @@ void test_subprocess_set_cleanup_callback(void (*callback)(void)); /* Send a notification signal (SIGHUP) to the given PID */ void test_subprocess_notify_signal_send(int signo, pid_t pid); -/* Send a notificatino signal to the parent process. */ +/* Send a notification signal to the parent process. */ void test_subprocess_notify_signal_send_parent(int signo); +/* Send a notification signal to all the subprocesses. */ +void test_subprocess_notify_signal_all(int signo); /* Reset any previously sent notification signals. */ void test_subprocess_notify_signal_reset(int signo); /* Wait until a notification signal is sent, or return immediately if it was