[PATCH] test: let the OS choose a port for smtp-dummy

David Bremner david at tethera.net
Tue May 7 03:20:49 PDT 2019


This should avoid potential collisions if we start running multiple
smtp-dummy processes in parallel.
---
 test/smtp-dummy.c | 15 ++++++++++++++-
 test/test-lib.sh  |  2 +-
 2 files changed, 15 insertions(+), 2 deletions(-)

diff --git a/test/smtp-dummy.c b/test/smtp-dummy.c
index 71992edd..a7c1fe4f 100644
--- a/test/smtp-dummy.c
+++ b/test/smtp-dummy.c
@@ -129,6 +129,7 @@ main (int argc, char *argv[])
     int reuse;
     int background;
     int ret = 0;
+    socklen_t addrlen;
 
     progname = argv[0];
 
@@ -191,7 +192,7 @@ main (int argc, char *argv[])
 
     memset (&addr, 0, sizeof (addr));
     addr.sin_family = AF_INET;
-    addr.sin_port = htons (25025);
+    addr.sin_port = 0;
     addr.sin_addr = *(struct in_addr *) hostinfo->h_addr;
     err = bind (sock, (struct sockaddr *) &addr, sizeof (addr));
     if (err) {
@@ -202,6 +203,18 @@ main (int argc, char *argv[])
 	goto DONE;
     }
 
+    addrlen = sizeof (addr);
+    err = getsockname (sock, (struct sockaddr *) &addr, &addrlen);
+    if (err) {
+	fprintf (stderr, "Error: getsockname() failed: %s\n",
+		 strerror (errno));
+	close (sock);
+	ret = 1;
+	goto DONE;
+    }
+
+    printf ("smtp_dummy_port='%d'\n", ntohs (addr.sin_port));
+
     err = listen (sock, 1);
     if (err) {
 	fprintf (stderr, "Error: listen() failed: %s\n",
diff --git a/test/test-lib.sh b/test/test-lib.sh
index f5d367aa..507886ba 100644
--- a/test/test-lib.sh
+++ b/test/test-lib.sh
@@ -326,7 +326,7 @@ emacs_deliver_message ()
 	"(let ((message-send-mail-function 'message-smtpmail-send-it)
                (mail-host-address \"example.com\")
 	       (smtpmail-smtp-server \"localhost\")
-	       (smtpmail-smtp-service \"25025\"))
+	       (smtpmail-smtp-service \"${smtp_dummy_port}\"))
 	   (notmuch-mua-mail)
 	   (message-goto-to)
 	   (insert \"test_suite at notmuchmail.org\nDate: 01 Jan 2000 12:00:00 -0000\")
-- 
2.20.1



More information about the notmuch mailing list