Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

cmd: Fix flaky port finder #1076

Merged
merged 1 commit into from
Oct 9, 2018
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 19 additions & 6 deletions cmd/root_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,17 +34,30 @@ import (

var frontendPort, backendPort int

func init() {
func freePort() (int, int) {
var err error
frontendPort, err = freeport.GetFreePort()
if err != nil {
r := make([]int, 2)

if r[0], err = freeport.GetFreePort(); err != nil {
panic(err.Error())
}

backendPort, err = freeport.GetFreePort()
if err != nil {
panic(err.Error())
tries := 0
for {
r[1], err = freeport.GetFreePort()
if r[0] != r[1] {
break
}
tries++
if tries > 10 {
panic("Unable to find free port")
}
}
return r[0], r[1]
}

func init() {
frontendPort, backendPort = freePort()

os.Setenv("PUBLIC_PORT", fmt.Sprintf("%d", frontendPort))
os.Setenv("ADMIN_PORT", fmt.Sprintf("%d", backendPort))
Expand Down