From 39a6bb2ad6c87e55d25115eb7d8e9a4043fec2e1 Mon Sep 17 00:00:00 2001 From: Ken Hornstein Date: Fri, 23 Nov 2012 20:54:28 -0500 Subject: [PATCH] Fix the race condition with fakesmtp so it works without needing retries. --- test/fakesmtp.c | 32 +++++++++++++++++++++++++++++++- test/mhmail/test-mhmail | 36 ++++++++---------------------------- test/post/test-post-common.sh | 24 +++--------------------- 3 files changed, 42 insertions(+), 50 deletions(-) diff --git a/test/fakesmtp.c b/test/fakesmtp.c index 05be6de..fbfe044 100644 --- a/test/fakesmtp.c +++ b/test/fakesmtp.c @@ -34,6 +34,7 @@ main(int argc, char *argv[]) struct addrinfo hints, *res; int rc, l, conn, on, datamode; FILE *f, *pid; + pid_t child; fd_set readfd; struct stat st; struct timeval tv; @@ -119,6 +120,32 @@ main(int argc, char *argv[]) } /* + * Now we fork() and print out the process ID of our child + * for scripts to use. Once we do that, then exit. + */ + + child = fork(); + + switch (child) { + case -1: + fprintf(stderr, "Unable to fork child: %s\n", strerror(errno)); + exit(1); + break; + case 0: + /* + * Close stdin & stdout, otherwise people can + * think we're still doing stuff. For now leave stderr + * open. + */ + fclose(stdin); + fclose(stdout); + break; + default: + printf("%ld\n", (long) child); + exit(0); + } + + /* * Now that our socket & files are set up, wait 30 seconds for * a connection. If there isn't one, then exit. */ @@ -202,6 +229,8 @@ main(int argc, char *argv[]) */ if (strcmp(line, "QUIT") == 0) { + fclose(f); + f = NULL; putsmtp(conn, "221 Later alligator!"); close(conn); break; @@ -213,7 +242,8 @@ main(int argc, char *argv[]) } } - fclose(f); + if (f) + fclose(f); exit(0); } diff --git a/test/mhmail/test-mhmail b/test/mhmail/test-mhmail index fa90586..3745207 100755 --- a/test/mhmail/test-mhmail +++ b/test/mhmail/test-mhmail @@ -21,36 +21,16 @@ fi # $4 contains the message body. test_mhmail () { - "${MH_OBJ_DIR}/test/fakesmtp" "$actual" $localport & - pid="$!" - - # The server doesn't always come up fast enough, so sleep and - # retry a few times if it fails... - status=1 - for i in 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20; do - if [ $3 = '|' ]; then - if printf '%s' "$4" | mhmail recipient@example.com $2 \ - -server 127.0.0.1 -port $localport; then - status=0 - break - fi - else - if mhmail recipient@example.com $2 -body "$4" \ - -server 127.0.0.1 -port $localport; then - status=0 - break - fi - fi - sleep 2 - done - if [ $status -ne 0 ]; then - printf '%s: mhmail failed, was fakesmtp given enough time to start?\n' \ - "$0" >&2 - exit 1 + pid=`"${MH_OBJ_DIR}/test/fakesmtp" "$actual" $localport` + + if [ $3 = '|' ]; then + printf '%s' "$4" | mhmail recipient@example.com $2 \ + -server 127.0.0.1 -port $localport + else + mhmail recipient@example.com $2 -body "$4" \ + -server 127.0.0.1 -port $localport fi - wait ${pid} - # # It's hard to calculate the exact Date: header post is going to # use, so we'll just use sed to remove the actual date so we can easily diff --git a/test/post/test-post-common.sh b/test/post/test-post-common.sh index 4535ff8..3b1a3d0 100755 --- a/test/post/test-post-common.sh +++ b/test/post/test-post-common.sh @@ -26,27 +26,9 @@ echo "clientname: nosuchhost.example.com" >> ${MHMTSCONF} # $3: optional switches for send test_post () -{ "${MH_OBJ_DIR}/test/fakesmtp" "$1" $localport & - pid="$!" - - # The server doesn't always come up fast enough, so sleep and - # retry a few times if it fails... - status=1 - for i in 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20; do - if send -draft -server 127.0.0.1 -port $localport $3 - then - status=0 - break - fi - sleep 2 - done - if [ $status -ne 0 ]; then - printf '%s: send failed, was fakesmtp given enough time to start?\n' \ - "$0" >&2 - exit 1 - fi - - wait ${pid} +{ pid=`"${MH_OBJ_DIR}/test/fakesmtp" "$1" $localport` + + send -draft -server 127.0.0.1 -port $localport $3 || exit 1 # # It's hard to calculate the exact Date: header post is going to -- 1.7.10.4