|
@ -9,6 +9,7 @@ import ( |
|
|
"time" |
|
|
"time" |
|
|
|
|
|
|
|
|
"github.com/fortytw2/leaktest" |
|
|
"github.com/fortytw2/leaktest" |
|
|
|
|
|
"github.com/stretchr/testify/assert" |
|
|
"github.com/stretchr/testify/require" |
|
|
"github.com/stretchr/testify/require" |
|
|
dbm "github.com/tendermint/tm-db" |
|
|
dbm "github.com/tendermint/tm-db" |
|
|
|
|
|
|
|
@ -1271,7 +1272,7 @@ func TestPeerManager_Ready(t *testing.T) { |
|
|
require.Equal(t, p2p.PeerStatusDown, peerManager.Status(a.NodeID)) |
|
|
require.Equal(t, p2p.PeerStatusDown, peerManager.Status(a.NodeID)) |
|
|
|
|
|
|
|
|
// Marking a as ready should transition it to PeerStatusUp and send an update.
|
|
|
// Marking a as ready should transition it to PeerStatusUp and send an update.
|
|
|
peerManager.Ready(a.NodeID) |
|
|
|
|
|
|
|
|
peerManager.Ready(a.NodeID, nil) |
|
|
require.Equal(t, p2p.PeerStatusUp, peerManager.Status(a.NodeID)) |
|
|
require.Equal(t, p2p.PeerStatusUp, peerManager.Status(a.NodeID)) |
|
|
require.Equal(t, p2p.PeerUpdate{ |
|
|
require.Equal(t, p2p.PeerUpdate{ |
|
|
NodeID: a.NodeID, |
|
|
NodeID: a.NodeID, |
|
@ -1283,11 +1284,31 @@ func TestPeerManager_Ready(t *testing.T) { |
|
|
require.NoError(t, err) |
|
|
require.NoError(t, err) |
|
|
require.True(t, added) |
|
|
require.True(t, added) |
|
|
require.Equal(t, p2p.PeerStatusDown, peerManager.Status(b.NodeID)) |
|
|
require.Equal(t, p2p.PeerStatusDown, peerManager.Status(b.NodeID)) |
|
|
peerManager.Ready(b.NodeID) |
|
|
|
|
|
|
|
|
peerManager.Ready(b.NodeID, nil) |
|
|
require.Equal(t, p2p.PeerStatusDown, peerManager.Status(b.NodeID)) |
|
|
require.Equal(t, p2p.PeerStatusDown, peerManager.Status(b.NodeID)) |
|
|
require.Empty(t, sub.Updates()) |
|
|
require.Empty(t, sub.Updates()) |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
func TestPeerManager_Ready_Channels(t *testing.T) { |
|
|
|
|
|
pm, err := p2p.NewPeerManager(selfID, dbm.NewMemDB(), p2p.PeerManagerOptions{}) |
|
|
|
|
|
require.NoError(t, err) |
|
|
|
|
|
|
|
|
|
|
|
sub := pm.Subscribe() |
|
|
|
|
|
|
|
|
|
|
|
a := p2p.NodeAddress{Protocol: "memory", NodeID: types.NodeID(strings.Repeat("a", 40))} |
|
|
|
|
|
added, err := pm.Add(a) |
|
|
|
|
|
require.NoError(t, err) |
|
|
|
|
|
require.True(t, added) |
|
|
|
|
|
require.NoError(t, pm.Accepted(a.NodeID)) |
|
|
|
|
|
|
|
|
|
|
|
pm.Ready(a.NodeID, p2p.ChannelIDSet{42: struct{}{}}) |
|
|
|
|
|
require.NotEmpty(t, sub.Updates()) |
|
|
|
|
|
update := <-sub.Updates() |
|
|
|
|
|
assert.Equal(t, a.NodeID, update.NodeID) |
|
|
|
|
|
require.True(t, update.Channels.Contains(42)) |
|
|
|
|
|
require.False(t, update.Channels.Contains(48)) |
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
// See TryEvictNext for most tests, this just tests blocking behavior.
|
|
|
// See TryEvictNext for most tests, this just tests blocking behavior.
|
|
|
func TestPeerManager_EvictNext(t *testing.T) { |
|
|
func TestPeerManager_EvictNext(t *testing.T) { |
|
|
a := p2p.NodeAddress{Protocol: "memory", NodeID: types.NodeID(strings.Repeat("a", 40))} |
|
|
a := p2p.NodeAddress{Protocol: "memory", NodeID: types.NodeID(strings.Repeat("a", 40))} |
|
@ -1299,7 +1320,7 @@ func TestPeerManager_EvictNext(t *testing.T) { |
|
|
require.NoError(t, err) |
|
|
require.NoError(t, err) |
|
|
require.True(t, added) |
|
|
require.True(t, added) |
|
|
require.NoError(t, peerManager.Accepted(a.NodeID)) |
|
|
require.NoError(t, peerManager.Accepted(a.NodeID)) |
|
|
peerManager.Ready(a.NodeID) |
|
|
|
|
|
|
|
|
peerManager.Ready(a.NodeID, nil) |
|
|
|
|
|
|
|
|
// Since there are no peers to evict, EvictNext should block until timeout.
|
|
|
// Since there are no peers to evict, EvictNext should block until timeout.
|
|
|
timeoutCtx, cancel := context.WithTimeout(ctx, 100*time.Millisecond) |
|
|
timeoutCtx, cancel := context.WithTimeout(ctx, 100*time.Millisecond) |
|
@ -1332,7 +1353,7 @@ func TestPeerManager_EvictNext_WakeOnError(t *testing.T) { |
|
|
require.NoError(t, err) |
|
|
require.NoError(t, err) |
|
|
require.True(t, added) |
|
|
require.True(t, added) |
|
|
require.NoError(t, peerManager.Accepted(a.NodeID)) |
|
|
require.NoError(t, peerManager.Accepted(a.NodeID)) |
|
|
peerManager.Ready(a.NodeID) |
|
|
|
|
|
|
|
|
peerManager.Ready(a.NodeID, nil) |
|
|
|
|
|
|
|
|
// Spawn a goroutine to error a peer after a delay.
|
|
|
// Spawn a goroutine to error a peer after a delay.
|
|
|
go func() { |
|
|
go func() { |
|
@ -1364,7 +1385,7 @@ func TestPeerManager_EvictNext_WakeOnUpgradeDialed(t *testing.T) { |
|
|
require.NoError(t, err) |
|
|
require.NoError(t, err) |
|
|
require.True(t, added) |
|
|
require.True(t, added) |
|
|
require.NoError(t, peerManager.Accepted(a.NodeID)) |
|
|
require.NoError(t, peerManager.Accepted(a.NodeID)) |
|
|
peerManager.Ready(a.NodeID) |
|
|
|
|
|
|
|
|
peerManager.Ready(a.NodeID, nil) |
|
|
|
|
|
|
|
|
// Spawn a goroutine to upgrade to b with a delay.
|
|
|
// Spawn a goroutine to upgrade to b with a delay.
|
|
|
go func() { |
|
|
go func() { |
|
@ -1402,7 +1423,7 @@ func TestPeerManager_EvictNext_WakeOnUpgradeAccepted(t *testing.T) { |
|
|
require.NoError(t, err) |
|
|
require.NoError(t, err) |
|
|
require.True(t, added) |
|
|
require.True(t, added) |
|
|
require.NoError(t, peerManager.Accepted(a.NodeID)) |
|
|
require.NoError(t, peerManager.Accepted(a.NodeID)) |
|
|
peerManager.Ready(a.NodeID) |
|
|
|
|
|
|
|
|
peerManager.Ready(a.NodeID, nil) |
|
|
|
|
|
|
|
|
// Spawn a goroutine to upgrade b with a delay.
|
|
|
// Spawn a goroutine to upgrade b with a delay.
|
|
|
go func() { |
|
|
go func() { |
|
@ -1434,7 +1455,7 @@ func TestPeerManager_TryEvictNext(t *testing.T) { |
|
|
|
|
|
|
|
|
// Connecting to a won't evict anything either.
|
|
|
// Connecting to a won't evict anything either.
|
|
|
require.NoError(t, peerManager.Accepted(a.NodeID)) |
|
|
require.NoError(t, peerManager.Accepted(a.NodeID)) |
|
|
peerManager.Ready(a.NodeID) |
|
|
|
|
|
|
|
|
peerManager.Ready(a.NodeID, nil) |
|
|
|
|
|
|
|
|
// But if a errors it should be evicted.
|
|
|
// But if a errors it should be evicted.
|
|
|
peerManager.Errored(a.NodeID, errors.New("foo")) |
|
|
peerManager.Errored(a.NodeID, errors.New("foo")) |
|
@ -1479,7 +1500,7 @@ func TestPeerManager_Disconnected(t *testing.T) { |
|
|
_, err = peerManager.Add(a) |
|
|
_, err = peerManager.Add(a) |
|
|
require.NoError(t, err) |
|
|
require.NoError(t, err) |
|
|
require.NoError(t, peerManager.Accepted(a.NodeID)) |
|
|
require.NoError(t, peerManager.Accepted(a.NodeID)) |
|
|
peerManager.Ready(a.NodeID) |
|
|
|
|
|
|
|
|
peerManager.Ready(a.NodeID, nil) |
|
|
require.Equal(t, p2p.PeerStatusUp, peerManager.Status(a.NodeID)) |
|
|
require.Equal(t, p2p.PeerStatusUp, peerManager.Status(a.NodeID)) |
|
|
require.NotEmpty(t, sub.Updates()) |
|
|
require.NotEmpty(t, sub.Updates()) |
|
|
require.Equal(t, p2p.PeerUpdate{ |
|
|
require.Equal(t, p2p.PeerUpdate{ |
|
@ -1531,7 +1552,7 @@ func TestPeerManager_Errored(t *testing.T) { |
|
|
require.Zero(t, evict) |
|
|
require.Zero(t, evict) |
|
|
|
|
|
|
|
|
require.NoError(t, peerManager.Accepted(a.NodeID)) |
|
|
require.NoError(t, peerManager.Accepted(a.NodeID)) |
|
|
peerManager.Ready(a.NodeID) |
|
|
|
|
|
|
|
|
peerManager.Ready(a.NodeID, nil) |
|
|
evict, err = peerManager.TryEvictNext() |
|
|
evict, err = peerManager.TryEvictNext() |
|
|
require.NoError(t, err) |
|
|
require.NoError(t, err) |
|
|
require.Zero(t, evict) |
|
|
require.Zero(t, evict) |
|
@ -1562,7 +1583,7 @@ func TestPeerManager_Subscribe(t *testing.T) { |
|
|
require.NoError(t, peerManager.Accepted(a.NodeID)) |
|
|
require.NoError(t, peerManager.Accepted(a.NodeID)) |
|
|
require.Empty(t, sub.Updates()) |
|
|
require.Empty(t, sub.Updates()) |
|
|
|
|
|
|
|
|
peerManager.Ready(a.NodeID) |
|
|
|
|
|
|
|
|
peerManager.Ready(a.NodeID, nil) |
|
|
require.NotEmpty(t, sub.Updates()) |
|
|
require.NotEmpty(t, sub.Updates()) |
|
|
require.Equal(t, p2p.PeerUpdate{NodeID: a.NodeID, Status: p2p.PeerStatusUp}, <-sub.Updates()) |
|
|
require.Equal(t, p2p.PeerUpdate{NodeID: a.NodeID, Status: p2p.PeerStatusUp}, <-sub.Updates()) |
|
|
|
|
|
|
|
@ -1579,7 +1600,7 @@ func TestPeerManager_Subscribe(t *testing.T) { |
|
|
require.NoError(t, peerManager.Dialed(a)) |
|
|
require.NoError(t, peerManager.Dialed(a)) |
|
|
require.Empty(t, sub.Updates()) |
|
|
require.Empty(t, sub.Updates()) |
|
|
|
|
|
|
|
|
peerManager.Ready(a.NodeID) |
|
|
|
|
|
|
|
|
peerManager.Ready(a.NodeID, nil) |
|
|
require.NotEmpty(t, sub.Updates()) |
|
|
require.NotEmpty(t, sub.Updates()) |
|
|
require.Equal(t, p2p.PeerUpdate{NodeID: a.NodeID, Status: p2p.PeerStatusUp}, <-sub.Updates()) |
|
|
require.Equal(t, p2p.PeerUpdate{NodeID: a.NodeID, Status: p2p.PeerStatusUp}, <-sub.Updates()) |
|
|
|
|
|
|
|
@ -1619,7 +1640,7 @@ func TestPeerManager_Subscribe_Close(t *testing.T) { |
|
|
require.NoError(t, peerManager.Accepted(a.NodeID)) |
|
|
require.NoError(t, peerManager.Accepted(a.NodeID)) |
|
|
require.Empty(t, sub.Updates()) |
|
|
require.Empty(t, sub.Updates()) |
|
|
|
|
|
|
|
|
peerManager.Ready(a.NodeID) |
|
|
|
|
|
|
|
|
peerManager.Ready(a.NodeID, nil) |
|
|
require.NotEmpty(t, sub.Updates()) |
|
|
require.NotEmpty(t, sub.Updates()) |
|
|
require.Equal(t, p2p.PeerUpdate{NodeID: a.NodeID, Status: p2p.PeerStatusUp}, <-sub.Updates()) |
|
|
require.Equal(t, p2p.PeerUpdate{NodeID: a.NodeID, Status: p2p.PeerStatusUp}, <-sub.Updates()) |
|
|
|
|
|
|
|
@ -1649,7 +1670,7 @@ func TestPeerManager_Subscribe_Broadcast(t *testing.T) { |
|
|
require.NoError(t, err) |
|
|
require.NoError(t, err) |
|
|
require.True(t, added) |
|
|
require.True(t, added) |
|
|
require.NoError(t, peerManager.Accepted(a.NodeID)) |
|
|
require.NoError(t, peerManager.Accepted(a.NodeID)) |
|
|
peerManager.Ready(a.NodeID) |
|
|
|
|
|
|
|
|
peerManager.Ready(a.NodeID, nil) |
|
|
|
|
|
|
|
|
expectUp := p2p.PeerUpdate{NodeID: a.NodeID, Status: p2p.PeerStatusUp} |
|
|
expectUp := p2p.PeerUpdate{NodeID: a.NodeID, Status: p2p.PeerStatusUp} |
|
|
require.NotEmpty(t, s1) |
|
|
require.NotEmpty(t, s1) |
|
|