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