whisper: made peer_test.go more portable

* Made tests more portable by using random free port
instead of hardcoded port 30303 to fix #15685
This commit is contained in:
Armin 2017-12-19 16:23:55 +01:00
parent af28d12847
commit 0931bcf554
2 changed files with 37 additions and 74 deletions

View file

@ -19,7 +19,6 @@ package whisperv5
import (
"bytes"
"crypto/ecdsa"
"fmt"
"net"
"sync"
"testing"
@ -108,8 +107,6 @@ func TestSimulation(t *testing.T) {
func initialize(t *testing.T) {
var err error
ip := net.IPv4(127, 0, 0, 1)
port0 := 30303
for i := 0; i < NumNodes; i++ {
var node TestNode
@ -128,29 +125,15 @@ func initialize(t *testing.T) {
if err != nil {
t.Fatalf("failed convert the key: %s.", keys[i])
}
port := port0 + i
addr := fmt.Sprintf(":%d", port) // e.g. ":30303"
name := common.MakeName("whisper-go", "2.0")
var peers []*discover.Node
if i > 0 {
peerNodeId := nodes[i-1].id
peerPort := uint16(port - 1)
peerNode := discover.PubkeyID(&peerNodeId.PublicKey)
peer := discover.NewNode(peerNode, ip, peerPort, peerPort)
peers = append(peers, peer)
}
node.server = &p2p.Server{
Config: p2p.Config{
PrivateKey: node.id,
MaxPeers: NumNodes/2 + 1,
Name: name,
Protocols: node.shh.Protocols(),
ListenAddr: addr,
NAT: nat.Any(),
BootstrapNodes: peers,
StaticNodes: peers,
TrustedNodes: peers,
PrivateKey: node.id,
MaxPeers: NumNodes/2 + 1,
Name: name,
Protocols: node.shh.Protocols(),
ListenAddr: "127.0.0.1:0",
NAT: nat.Any(),
},
}
@ -159,6 +142,15 @@ func initialize(t *testing.T) {
t.Fatalf("failed to start server %d.", i)
}
for j := 0; j < i; j++ {
peerNodeId := nodes[j].id
address, _ := net.ResolveTCPAddr("tcp", nodes[j].server.ListenAddr)
peerPort := uint16(address.Port)
peerNode := discover.PubkeyID(&peerNodeId.PublicKey)
peer := discover.NewNode(peerNode, address.IP, peerPort, peerPort)
node.server.AddPeer(peer)
}
nodes[i] = &node
}
}

View file

@ -21,12 +21,12 @@ import (
"crypto/ecdsa"
"fmt"
mrand "math/rand"
"net"
"sync"
"sync/atomic"
"testing"
"time"
"net"
"github.com/ethereum/go-ethereum/common"
"github.com/ethereum/go-ethereum/common/hexutil"
"github.com/ethereum/go-ethereum/crypto"
@ -173,8 +173,6 @@ func initialize(t *testing.T) {
initBloom(t)
var err error
ip := net.IPv4(127, 0, 0, 1)
port0 := 30303
for i := 0; i < NumNodes; i++ {
var node TestNode
@ -199,49 +197,35 @@ func initialize(t *testing.T) {
if err != nil {
t.Fatalf("failed convert the key: %s.", keys[i])
}
port := port0 + i
addr := fmt.Sprintf(":%d", port) // e.g. ":30303"
name := common.MakeName("whisper-go", "2.0")
var peers []*discover.Node
if i > 0 {
peerNodeID := nodes[i-1].id
peerPort := uint16(port - 1)
peerNode := discover.PubkeyID(&peerNodeID.PublicKey)
peer := discover.NewNode(peerNode, ip, peerPort, peerPort)
peers = append(peers, peer)
}
node.server = &p2p.Server{
Config: p2p.Config{
PrivateKey: node.id,
MaxPeers: NumNodes/2 + 1,
Name: name,
Protocols: node.shh.Protocols(),
ListenAddr: addr,
NAT: nat.Any(),
BootstrapNodes: peers,
StaticNodes: peers,
TrustedNodes: peers,
PrivateKey: node.id,
MaxPeers: NumNodes/2 + 1,
Name: name,
Protocols: node.shh.Protocols(),
ListenAddr: "127.0.0.1:0",
NAT: nat.Any(),
},
}
err = node.server.Start()
if err != nil {
t.Fatalf("failed to start server %d.", i)
}
for j := 0; j < i; j++ {
peerNodeId := nodes[j].id
address, _ := net.ResolveTCPAddr("tcp", nodes[j].server.ListenAddr)
peerPort := uint16(address.Port)
peerNode := discover.PubkeyID(&peerNodeId.PublicKey)
peer := discover.NewNode(peerNode, address.IP, peerPort, peerPort)
node.server.AddPeer(peer)
}
nodes[i] = &node
}
for i := 0; i < NumNodes; i++ {
go startServer(t, nodes[i].server)
}
waitForServersToStart(t)
}
func startServer(t *testing.T, s *p2p.Server) {
err := s.Start()
if err != nil {
t.Fatalf("failed to start the fisrt server.")
}
atomic.AddInt64(&result.started, 1)
}
func stopServers() {
@ -499,16 +483,3 @@ func checkBloomFilterExchange(t *testing.T) {
time.Sleep(50 * time.Millisecond)
}
}
func waitForServersToStart(t *testing.T) {
const iterations = 200
var started int64
for j := 0; j < iterations; j++ {
time.Sleep(50 * time.Millisecond)
started = atomic.LoadInt64(&result.started)
if started == NumNodes {
return
}
}
t.Fatalf("Failed to start all the servers, running: %d", started)
}