From d58987e0871e97b5934e0d77cf569edea0c1dccf Mon Sep 17 00:00:00 2001 From: Daniel McCarney Date: Mon, 3 Oct 2016 18:04:24 -0400 Subject: [PATCH] Allows OS to choose listener port. (#2223) This commit fixes #2190 by allowing the OS to choose what port the test Listener's bind to. This allows the tests to be run concurrently without panicing when they all try to bind the same TCP port. --- mail/mailer_test.go | 29 +++++++++++++++++------------ 1 file changed, 17 insertions(+), 12 deletions(-) diff --git a/mail/mailer_test.go b/mail/mailer_test.go index 51509f3fb..f412b18f5 100644 --- a/mail/mailer_test.go +++ b/mail/mailer_test.go @@ -159,22 +159,12 @@ func disconnectHandler(closeFirst int) connHandler { } func setup(t *testing.T) (*MailerImpl, net.Listener, func()) { - const port = "16632" stats := metrics.NewNoopScope() fromAddress, _ := mail.ParseAddress("you-are-a-winner@example.com") log := blog.UseMock() - m := New( - "localhost", - port, - "user@example.com", - "paswd", - *fromAddress, - log, - stats, - time.Second*2, time.Second*10) - - l, err := net.Listen("tcp", ":"+port) + // Listen on port 0 to get any free available port + l, err := net.Listen("tcp", ":0") if err != nil { t.Fatalf("listen: %s", err) } @@ -185,6 +175,21 @@ func setup(t *testing.T) (*MailerImpl, net.Listener, func()) { } } + // We can look at the listener Addr() to figure out which free port was + // assigned by the operating system + addr := l.Addr().(*net.TCPAddr) + port := addr.Port + + m := New( + "localhost", + fmt.Sprintf("%d", port), + "user@example.com", + "paswd", + *fromAddress, + log, + stats, + time.Second*2, time.Second*10) + return m, l, cleanUp }