Commit c43e8fa3 authored by Juan Batiz-Benet's avatar Juan Batiz-Benet

fixing race in testutil port

parent fd2875f2
......@@ -6,6 +6,7 @@ import (
"errors"
"fmt"
"io"
"sync"
"testing"
ci "github.com/jbenet/go-ipfs/crypto"
......@@ -49,17 +50,24 @@ func RandLocalTCPAddress() ma.Multiaddr {
// most ports above 10000 aren't in use by long running processes, so yay.
// (maybe there should be a range of "loopback" ports that are guaranteed
// to be open for the process, but naturally can only talk to self.)
if lastPort == 0 {
lastPort = 10000 + SeededRand.Intn(50000)
lastPort.Lock()
if lastPort.port == 0 {
lastPort.port = 10000 + SeededRand.Intn(50000)
}
lastPort++
port := lastPort.port
lastPort.port++
lastPort.Unlock()
addr := fmt.Sprintf("/ip4/127.0.0.1/tcp/%d", lastPort)
addr := fmt.Sprintf("/ip4/127.0.0.1/tcp/%d", port)
maddr, _ := ma.NewMultiaddr(addr)
return maddr
}
var lastPort = 0
var lastPort = struct {
port int
sync.Mutex
}{}
// PeerNetParams is a struct to bundle together the four things
// you need to run a connection with a peer: id, 2keys, and addr.
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment