5c5f21088c
This should cut down on test flakiness. Problems handled: - If you had enough parallel test cases running, the former circular approach to handling the port block could hand out the same port to multiple cases before they each had a chance to bind them, leading to one of the two tests to fail. - The freeport library would allocate out of the ephemeral port range. This has been corrected for Linux (which should cover CI). - The library now waits until a formerly-in-use port is verified to be free before putting it back into circulation.
37 lines
734 B
Go
37 lines
734 B
Go
//+build linux
|
|
|
|
package freeport
|
|
|
|
import (
|
|
"fmt"
|
|
"os/exec"
|
|
"regexp"
|
|
"strconv"
|
|
)
|
|
|
|
const ephemeralPortRangeSysctlKey = "net.ipv4.ip_local_port_range"
|
|
|
|
var ephemeralPortRangePatt = regexp.MustCompile(`^\s*(\d+)\s+(\d+)\s*$`)
|
|
|
|
func getEphemeralPortRange() (int, int, error) {
|
|
cmd := exec.Command("sysctl", "-n", ephemeralPortRangeSysctlKey)
|
|
out, err := cmd.Output()
|
|
if err != nil {
|
|
return 0, 0, err
|
|
}
|
|
|
|
val := string(out)
|
|
|
|
m := ephemeralPortRangePatt.FindStringSubmatch(val)
|
|
if m != nil {
|
|
min, err1 := strconv.Atoi(m[1])
|
|
max, err2 := strconv.Atoi(m[2])
|
|
|
|
if err1 == nil && err2 == nil {
|
|
return min, max, nil
|
|
}
|
|
}
|
|
|
|
return 0, 0, fmt.Errorf("unexpected sysctl value %q for key %q", val, ephemeralPortRangeSysctlKey)
|
|
}
|