|
|
@ -2,6 +2,7 @@ package p2p |
|
|
|
|
|
|
|
import ( |
|
|
|
"math/rand" |
|
|
|
"net" |
|
|
|
"sync" |
|
|
|
"testing" |
|
|
|
|
|
|
@ -12,23 +13,32 @@ import ( |
|
|
|
) |
|
|
|
|
|
|
|
// Returns an empty kvstore peer
|
|
|
|
func randPeer() *peer { |
|
|
|
func randPeer(ip net.IP) *peer { |
|
|
|
if ip == nil { |
|
|
|
ip = net.IP{127, 0, 0, 1} |
|
|
|
} |
|
|
|
|
|
|
|
nodeKey := NodeKey{PrivKey: crypto.GenPrivKeyEd25519()} |
|
|
|
return &peer{ |
|
|
|
p := &peer{ |
|
|
|
nodeInfo: NodeInfo{ |
|
|
|
ID: nodeKey.ID(), |
|
|
|
ListenAddr: cmn.Fmt("%v.%v.%v.%v:46656", rand.Int()%256, rand.Int()%256, rand.Int()%256, rand.Int()%256), |
|
|
|
}, |
|
|
|
} |
|
|
|
|
|
|
|
p.ip = ip |
|
|
|
|
|
|
|
return p |
|
|
|
} |
|
|
|
|
|
|
|
func TestPeerSetAddRemoveOne(t *testing.T) { |
|
|
|
t.Parallel() |
|
|
|
|
|
|
|
peerSet := NewPeerSet() |
|
|
|
|
|
|
|
var peerList []Peer |
|
|
|
for i := 0; i < 5; i++ { |
|
|
|
p := randPeer() |
|
|
|
p := randPeer(net.IP{127, 0, 0, byte(i)}) |
|
|
|
if err := peerSet.Add(p); err != nil { |
|
|
|
t.Error(err) |
|
|
|
} |
|
|
@ -72,7 +82,7 @@ func TestPeerSetAddRemoveMany(t *testing.T) { |
|
|
|
peers := []Peer{} |
|
|
|
N := 100 |
|
|
|
for i := 0; i < N; i++ { |
|
|
|
peer := randPeer() |
|
|
|
peer := randPeer(net.IP{127, 0, 0, byte(i)}) |
|
|
|
if err := peerSet.Add(peer); err != nil { |
|
|
|
t.Errorf("Failed to add new peer") |
|
|
|
} |
|
|
@ -96,7 +106,7 @@ func TestPeerSetAddRemoveMany(t *testing.T) { |
|
|
|
func TestPeerSetAddDuplicate(t *testing.T) { |
|
|
|
t.Parallel() |
|
|
|
peerSet := NewPeerSet() |
|
|
|
peer := randPeer() |
|
|
|
peer := randPeer(nil) |
|
|
|
|
|
|
|
n := 20 |
|
|
|
errsChan := make(chan error) |
|
|
@ -112,25 +122,49 @@ func TestPeerSetAddDuplicate(t *testing.T) { |
|
|
|
} |
|
|
|
|
|
|
|
// Now collect and tally the results
|
|
|
|
errsTally := make(map[error]int) |
|
|
|
errsTally := make(map[string]int) |
|
|
|
for i := 0; i < n; i++ { |
|
|
|
err := <-errsChan |
|
|
|
errsTally[err]++ |
|
|
|
|
|
|
|
switch err.(type) { |
|
|
|
case ErrSwitchDuplicatePeerID: |
|
|
|
errsTally["duplicateID"]++ |
|
|
|
default: |
|
|
|
errsTally["other"]++ |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
// Our next procedure is to ensure that only one addition
|
|
|
|
// succeeded and that the rest are each ErrSwitchDuplicatePeer.
|
|
|
|
wantErrCount, gotErrCount := n-1, errsTally[ErrSwitchDuplicatePeer] |
|
|
|
wantErrCount, gotErrCount := n-1, errsTally["duplicateID"] |
|
|
|
assert.Equal(t, wantErrCount, gotErrCount, "invalid ErrSwitchDuplicatePeer count") |
|
|
|
|
|
|
|
wantNilErrCount, gotNilErrCount := 1, errsTally[nil] |
|
|
|
wantNilErrCount, gotNilErrCount := 1, errsTally["other"] |
|
|
|
assert.Equal(t, wantNilErrCount, gotNilErrCount, "invalid nil errCount") |
|
|
|
} |
|
|
|
|
|
|
|
func TestPeerSetGet(t *testing.T) { |
|
|
|
func TestPeerSetAddDuplicateIP(t *testing.T) { |
|
|
|
t.Parallel() |
|
|
|
|
|
|
|
peerSet := NewPeerSet() |
|
|
|
peer := randPeer() |
|
|
|
|
|
|
|
if err := peerSet.Add(randPeer(net.IP{172, 0, 0, 1})); err != nil { |
|
|
|
t.Fatal(err) |
|
|
|
} |
|
|
|
|
|
|
|
// Add peer with same IP.
|
|
|
|
err := peerSet.Add(randPeer(net.IP{172, 0, 0, 1})) |
|
|
|
assert.Equal(t, ErrSwitchDuplicatePeerIP{IP: net.IP{172, 0, 0, 1}}, err) |
|
|
|
} |
|
|
|
|
|
|
|
func TestPeerSetGet(t *testing.T) { |
|
|
|
t.Parallel() |
|
|
|
|
|
|
|
var ( |
|
|
|
peerSet = NewPeerSet() |
|
|
|
peer = randPeer(nil) |
|
|
|
) |
|
|
|
|
|
|
|
assert.Nil(t, peerSet.Get(peer.ID()), "expecting a nil lookup, before .Add") |
|
|
|
|
|
|
|
if err := peerSet.Add(peer); err != nil { |
|
|
@ -144,8 +178,8 @@ func TestPeerSetGet(t *testing.T) { |
|
|
|
wg.Add(1) |
|
|
|
go func(i int) { |
|
|
|
defer wg.Done() |
|
|
|
got, want := peerSet.Get(peer.ID()), peer |
|
|
|
assert.Equal(t, got, want, "#%d: got=%v want=%v", i, got, want) |
|
|
|
have, want := peerSet.Get(peer.ID()), peer |
|
|
|
assert.Equal(t, have, want, "%d: have %v, want %v", i, have, want) |
|
|
|
}(i) |
|
|
|
} |
|
|
|
wg.Wait() |
|
|
|