github.com/grafana/pyroscope@v1.18.0/pkg/test/integration/ports.go (about) 1 package integration 2 3 import "net" 4 5 // GetFreePorts returns a number of free local port for the tests to listen on. 6 // This will make sure the returned ports do not overlap, by stopping to listen once all ports are allocated 7 // 8 // Note: This function should only be used in integration tests. 9 // Use in-memory network connections in unittests. 10 func GetFreePorts(len int) (ports []int, err error) { 11 ports = make([]int, len) 12 for i := 0; i < len; i++ { 13 var a *net.TCPAddr 14 if a, err = net.ResolveTCPAddr("tcp", "127.0.0.1:0"); err == nil { 15 var l *net.TCPListener 16 if l, err = net.ListenTCP("tcp", a); err != nil { 17 return nil, err 18 } 19 defer l.Close() 20 ports[i] = l.Addr().(*net.TCPAddr).Port 21 } 22 } 23 return ports, nil 24 }