From bc3aab72fc4a5635b02e152527fecea466cc9517 Mon Sep 17 00:00:00 2001 From: Diptanu Choudhury Date: Tue, 16 Aug 2016 16:05:37 -0700 Subject: [PATCH] get a free port from the kernel --- command/agent/agent_test.go | 16 ++++++++++++---- 1 file changed, 12 insertions(+), 4 deletions(-) diff --git a/command/agent/agent_test.go b/command/agent/agent_test.go index 2cd37c07a..25f119abc 100644 --- a/command/agent/agent_test.go +++ b/command/agent/agent_test.go @@ -3,9 +3,9 @@ package agent import ( "fmt" "io/ioutil" + "net" "os" "strings" - "sync/atomic" "testing" "time" @@ -13,10 +13,18 @@ import ( sconfig "github.com/hashicorp/nomad/nomad/structs/config" ) -var nextPort uint32 = 17000 - func getPort() int { - return int(atomic.AddUint32(&nextPort, 1)) + addr, err := net.ResolveTCPAddr("tcp", "localhost:0") + if err != nil { + panic(err) + } + + l, err := net.ListenTCP("tcp", addr) + if err != nil { + panic(err) + } + defer l.Close() + return l.Addr().(*net.TCPAddr).Port } func tmpDir(t testing.TB) string {