get a free port from the kernel

This commit is contained in:
Diptanu Choudhury
2016-08-16 16:05:37 -07:00
parent 950a5abfa8
commit bc3aab72fc

View File

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