@ -9,6 +9,7 @@ import (
"time"
"github.com/fortytw2/leaktest"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
dbm "github.com/tendermint/tm-db"
@ -1311,7 +1312,7 @@ func TestPeerManager_Ready(t *testing.T) {
require . Equal ( t , p2p . PeerStatusDown , peerManager . Status ( a . NodeID ) )
// Marking a as ready should transition it to PeerStatusUp and send an update.
peerManager . Ready ( ctx , a . NodeID )
peerManager . Ready ( ctx , a . NodeID , nil )
require . Equal ( t , p2p . PeerStatusUp , peerManager . Status ( a . NodeID ) )
require . Equal ( t , p2p . PeerUpdate {
NodeID : a . NodeID ,
@ -1323,11 +1324,34 @@ func TestPeerManager_Ready(t *testing.T) {
require . NoError ( t , err )
require . True ( t , added )
require . Equal ( t , p2p . PeerStatusDown , peerManager . Status ( b . NodeID ) )
peerManager . Ready ( ctx , b . NodeID )
peerManager . Ready ( ctx , b . NodeID , nil )
require . Equal ( t , p2p . PeerStatusDown , peerManager . Status ( b . NodeID ) )
require . Empty ( t , sub . Updates ( ) )
}
func TestPeerManager_Ready_Channels ( t * testing . T ) {
ctx , cancel := context . WithCancel ( context . Background ( ) )
defer cancel ( )
pm , err := p2p . NewPeerManager ( selfID , dbm . NewMemDB ( ) , p2p . PeerManagerOptions { } )
require . NoError ( t , err )
sub := pm . Subscribe ( ctx )
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 ( ctx , 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.
func TestPeerManager_EvictNext ( t * testing . T ) {
ctx , cancel := context . WithCancel ( context . Background ( ) )
@ -1342,7 +1366,7 @@ func TestPeerManager_EvictNext(t *testing.T) {
require . NoError ( t , err )
require . True ( t , added )
require . NoError ( t , peerManager . Accepted ( a . NodeID ) )
peerManager . Ready ( ctx , a . NodeID )
peerManager . Ready ( ctx , a . NodeID , nil )
// Since there are no peers to evict, EvictNext should block until timeout.
timeoutCtx , cancel := context . WithTimeout ( ctx , 100 * time . Millisecond )
@ -1378,7 +1402,7 @@ func TestPeerManager_EvictNext_WakeOnError(t *testing.T) {
require . NoError ( t , err )
require . True ( t , added )
require . NoError ( t , peerManager . Accepted ( a . NodeID ) )
peerManager . Ready ( ctx , a . NodeID )
peerManager . Ready ( ctx , a . NodeID , nil )
// Spawn a goroutine to error a peer after a delay.
go func ( ) {
@ -1413,7 +1437,7 @@ func TestPeerManager_EvictNext_WakeOnUpgradeDialed(t *testing.T) {
require . NoError ( t , err )
require . True ( t , added )
require . NoError ( t , peerManager . Accepted ( a . NodeID ) )
peerManager . Ready ( ctx , a . NodeID )
peerManager . Ready ( ctx , a . NodeID , nil )
// Spawn a goroutine to upgrade to b with a delay.
go func ( ) {
@ -1454,7 +1478,7 @@ func TestPeerManager_EvictNext_WakeOnUpgradeAccepted(t *testing.T) {
require . NoError ( t , err )
require . True ( t , added )
require . NoError ( t , peerManager . Accepted ( a . NodeID ) )
peerManager . Ready ( ctx , a . NodeID )
peerManager . Ready ( ctx , a . NodeID , nil )
// Spawn a goroutine to upgrade b with a delay.
go func ( ) {
@ -1489,7 +1513,7 @@ func TestPeerManager_TryEvictNext(t *testing.T) {
// Connecting to a won't evict anything either.
require . NoError ( t , peerManager . Accepted ( a . NodeID ) )
peerManager . Ready ( ctx , a . NodeID )
peerManager . Ready ( ctx , a . NodeID , nil )
// But if a errors it should be evicted.
peerManager . Errored ( a . NodeID , errors . New ( "foo" ) )
@ -1536,7 +1560,7 @@ func TestPeerManager_Disconnected(t *testing.T) {
_ , err = peerManager . Add ( a )
require . NoError ( t , err )
require . NoError ( t , peerManager . Accepted ( a . NodeID ) )
peerManager . Ready ( ctx , a . NodeID )
peerManager . Ready ( ctx , a . NodeID , nil )
require . Equal ( t , p2p . PeerStatusUp , peerManager . Status ( a . NodeID ) )
require . NotEmpty ( t , sub . Updates ( ) )
require . Equal ( t , p2p . PeerUpdate {
@ -1591,7 +1615,7 @@ func TestPeerManager_Errored(t *testing.T) {
require . Zero ( t , evict )
require . NoError ( t , peerManager . Accepted ( a . NodeID ) )
peerManager . Ready ( ctx , a . NodeID )
peerManager . Ready ( ctx , a . NodeID , nil )
evict , err = peerManager . TryEvictNext ( )
require . NoError ( t , err )
require . Zero ( t , evict )
@ -1624,7 +1648,7 @@ func TestPeerManager_Subscribe(t *testing.T) {
require . NoError ( t , peerManager . Accepted ( a . NodeID ) )
require . Empty ( t , sub . Updates ( ) )
peerManager . Ready ( ctx , a . NodeID )
peerManager . Ready ( ctx , a . NodeID , nil )
require . NotEmpty ( t , sub . Updates ( ) )
require . Equal ( t , p2p . PeerUpdate { NodeID : a . NodeID , Status : p2p . PeerStatusUp } , <- sub . Updates ( ) )
@ -1641,7 +1665,7 @@ func TestPeerManager_Subscribe(t *testing.T) {
require . NoError ( t , peerManager . Dialed ( a ) )
require . Empty ( t , sub . Updates ( ) )
peerManager . Ready ( ctx , a . NodeID )
peerManager . Ready ( ctx , a . NodeID , nil )
require . NotEmpty ( t , sub . Updates ( ) )
require . Equal ( t , p2p . PeerUpdate { NodeID : a . NodeID , Status : p2p . PeerStatusUp } , <- sub . Updates ( ) )
@ -1683,7 +1707,7 @@ func TestPeerManager_Subscribe_Close(t *testing.T) {
require . NoError ( t , peerManager . Accepted ( a . NodeID ) )
require . Empty ( t , sub . Updates ( ) )
peerManager . Ready ( ctx , a . NodeID )
peerManager . Ready ( ctx , a . NodeID , nil )
require . NotEmpty ( t , sub . Updates ( ) )
require . Equal ( t , p2p . PeerUpdate { NodeID : a . NodeID , Status : p2p . PeerStatusUp } , <- sub . Updates ( ) )
@ -1716,7 +1740,7 @@ func TestPeerManager_Subscribe_Broadcast(t *testing.T) {
require . NoError ( t , err )
require . True ( t , added )
require . NoError ( t , peerManager . Accepted ( a . NodeID ) )
peerManager . Ready ( ctx , a . NodeID )
peerManager . Ready ( ctx , a . NodeID , nil )
expectUp := p2p . PeerUpdate { NodeID : a . NodeID , Status : p2p . PeerStatusUp }
require . NotEmpty ( t , s1 )