diff --git a/testutil/server.go b/testutil/server.go index 727f1088e8..8ab196eca2 100644 --- a/testutil/server.go +++ b/testutil/server.go @@ -23,15 +23,11 @@ import ( "os" "os/exec" "strings" - "sync/atomic" "github.com/hashicorp/consul/consul/structs" "github.com/hashicorp/go-cleanhttp" ) -// offset is used to atomically increment the port numbers. -var offset uint64 - // TestPerformanceConfig configures the performance parameters. type TestPerformanceConfig struct { RaftMultiplier uint `json:"raft_multiplier,omitempty"` @@ -82,10 +78,8 @@ type ServerConfigCallback func(c *TestServerConfig) // defaultServerConfig returns a new TestServerConfig struct // with all of the listen ports incremented by one. func defaultServerConfig() *TestServerConfig { - idx := int(atomic.AddUint64(&offset, 1)) - return &TestServerConfig{ - NodeName: fmt.Sprintf("node%d", idx), + NodeName: fmt.Sprintf("node%d", randomPort()), DisableCheckpoint: true, Performance: &TestPerformanceConfig{ RaftMultiplier: 1, @@ -96,16 +90,26 @@ func defaultServerConfig() *TestServerConfig { Bind: "127.0.0.1", Addresses: &TestAddressConfig{}, Ports: &TestPortConfig{ - DNS: 20000 + idx, - HTTP: 21000 + idx, - RPC: 22000 + idx, - SerfLan: 23000 + idx, - SerfWan: 24000 + idx, - Server: 25000 + idx, + DNS: randomPort(), + HTTP: randomPort(), + RPC: randomPort(), + SerfLan: randomPort(), + SerfWan: randomPort(), + Server: randomPort(), }, } } +// randomPort asks the kernel for a random port to use. +func randomPort() int { + l, err := net.Listen("tcp", "127.0.0.1:0") + if err != nil { + panic(err) + } + defer l.Close() + return l.Addr().(*net.TCPAddr).Port +} + // TestService is used to serialize a service definition. type TestService struct { ID string `json:",omitempty"`