Skip to content

Commit

Permalink
cmd: Fix flaky port finder
Browse files Browse the repository at this point in the history
Closes ory#1054

Signed-off-by: arekkas <[email protected]>
  • Loading branch information
arekkas authored and aeneasr committed Oct 9, 2018
1 parent c0b7a39 commit a68cca9
Showing 1 changed file with 19 additions and 6 deletions.
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

0 comments on commit a68cca9

Please sign in to comment.