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.
This commit is contained in:
Daniel McCarney 2016-10-03 18:04:24 -04:00 committed by Roland Bracewell Shoemaker
parent d2cf6ee126
commit d58987e087
1 changed files with 17 additions and 12 deletions

View File

@ -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
}