Skip to content

Commit

Permalink
test: fix NewRandPort
Browse files Browse the repository at this point in the history
reducing chance of bind: address already in use
  • Loading branch information
lidel committed Jan 22, 2025
1 parent 84d75b7 commit a906a43
Showing 1 changed file with 8 additions and 0 deletions.
8 changes: 8 additions & 0 deletions test/cli/harness/peering.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package harness
import (
"fmt"
"math/rand"
"net"
"testing"

"github.com/ipfs/kubo/config"
Expand All @@ -14,6 +15,13 @@ type Peering struct {
}

func NewRandPort() int {
if a, err := net.ResolveTCPAddr("tcp", "localhost:0"); err == nil {
var l *net.TCPListener
if l, err = net.ListenTCP("tcp", a); err == nil {
defer l.Close()
return l.Addr().(*net.TCPAddr).Port
}
}
n := rand.Int()
return 3000 + (n % 1000)
}
Expand Down

0 comments on commit a906a43

Please sign in to comment.