From: Ken Hornstein <kenh@pobox.com>
Date: Thu, 12 Apr 2012 15:40:42 +0000 (-0400)
Subject: Clean up fakesmtp and the post tests a bit to hopefully reduce race
X-Git-Url: http://git.marmaro.de/?a=commitdiff_plain;h=fdc65105f4da4796ea11ede295622deae7690c18;p=mmh

Clean up fakesmtp and the post tests a bit to hopefully reduce race
conditions that make the tests fail sometimes.
---

diff --git a/test/fakesmtp.c b/test/fakesmtp.c
index 700bc45d..05be6dea 100644
--- a/test/fakesmtp.c
+++ b/test/fakesmtp.c
@@ -49,6 +49,33 @@ main(int argc, char *argv[])
 		exit(1);
 	}
 
+	/*
+	 * If there is a pid file already around, kill the previously running
+	 * fakesmtp process.  Hopefully this will reduce the race conditions
+	 * that crop up when running the test suite.
+	 */
+
+	if (stat(PIDFILE, &st) == 0) {
+		long oldpid;
+
+		if (!(pid = fopen(PIDFILE, "r"))) {
+			fprintf(stderr, "Cannot open " PIDFILE
+				" (%s), continuing ...\n", strerror(errno));
+		} else {
+			rc = fscanf(pid, "%ld", &oldpid);
+			fclose(pid);
+
+			if (rc != 1) {
+				fprintf(stderr, "Unable to parse pid in "
+					PIDFILE ", continuing ...\n");
+			} else {
+				kill((pid_t) oldpid, SIGTERM);
+			}
+		}
+
+		unlink(PIDFILE);
+	}
+
 	memset(&hints, 0, sizeof(hints));
 
 	hints.ai_family = PF_INET;
@@ -92,34 +119,10 @@ main(int argc, char *argv[])
 	}
 
 	/*
-	 * Now that our socket & files are set up, do the following things:
-	 *
-	 * - Check for a PID file.  If there is one, kill the old version.
-	 * - Wait 30 seconds for a connection.  If there isn't one, then
-	 *   exit.
+	 * Now that our socket & files are set up, wait 30 seconds for
+	 * a connection.  If there isn't one, then exit.
 	 */
 
-	if (stat(PIDFILE, &st) == 0) {
-		long oldpid;
-
-		if (!(pid = fopen(PIDFILE, "r"))) {
-			fprintf(stderr, "Cannot open " PIDFILE
-				" (%s), continuing ...\n", strerror(errno));
-		} else {
-			rc = fscanf(pid, "%ld", &oldpid);
-			fclose(pid);
-
-			if (rc != 1) {
-				fprintf(stderr, "Unable to parse pid in "
-					PIDFILE ", continuing ...\n");
-			} else {
-				kill((pid_t) oldpid, SIGTERM);
-			}
-		}
-
-		unlink(PIDFILE);
-	}
-
 	if (!(pid = fopen(PIDFILE, "w"))) {
 		fprintf(stderr, "Cannot open " PIDFILE ": %s\n",
 			strerror(errno));
diff --git a/test/post/test-post-common.sh b/test/post/test-post-common.sh
index c35a5a25..e0eaa4ac 100755
--- a/test/post/test-post-common.sh
+++ b/test/post/test-post-common.sh
@@ -29,7 +29,7 @@ test_post ()
 
     send -draft -server 127.0.0.1 -port $localport || exit 1
 
-    wait $!
+    wait ${pid}
 
     #
     # It's hard to calculate the exact Date: header post is going to