@ -211,31 +211,26 @@ func TestPEXReactorUsesSeedsIfNeeded(t *testing.T) {
defer os . RemoveAll ( dir ) // nolint: errcheck
// 1. create seed
seed := p2p . MakeSwitch (
cfg ,
0 ,
"127.0.0.1" ,
"123.123.123" ,
func ( i int , sw * p2p . Switch ) * p2p . Switch {
book := NewAddrBook ( filepath . Join ( dir , "addrbook0.json" ) , false )
book . SetLogger ( log . TestingLogger ( ) )
sw . SetAddrBook ( book )
sw . SetLogger ( log . TestingLogger ( ) )
r := NewPEXReactor ( book , & PEXReactorConfig { } )
r . SetLogger ( log . TestingLogger ( ) )
sw . AddReactor ( "pex" , r )
return sw
} ,
)
seed . AddListener (
p2p . NewDefaultListener ( "tcp://" + seed . NodeInfo ( ) . ListenAddr , "" , false , log . TestingLogger ( ) ) ,
)
seed := testCreateSeed ( dir , 0 , [ ] * p2p . NetAddress { } , [ ] * p2p . NetAddress { } )
require . Nil ( t , seed . Start ( ) )
defer seed . Stop ( )
// 2. create usual peer with only seed configured.
peer := testCreatePeerWithSeed ( dir , 1 , seed )
require . Nil ( t , peer . Start ( ) )
defer peer . Stop ( )
// 3. check that the peer connects to seed immediately
assertPeersWithTimeout ( t , [ ] * p2p . Switch { peer } , 10 * time . Millisecond , 3 * time . Second , 1 )
}
func TestConnectionSpeedForPeerReceivedFromSeed ( t * testing . T ) {
// directory to store address books
dir , err := ioutil . TempDir ( "" , "pex_reactor" )
require . Nil ( t , err )
defer os . RemoveAll ( dir ) // nolint: errcheck
// 1. create peer
peer := p2p . MakeSwitch (
cfg ,
1 ,
@ -250,20 +245,34 @@ func TestPEXReactorUsesSeedsIfNeeded(t *testing.T) {
r := NewPEXReactor (
book ,
& PEXReactorConfig {
Seeds : [ ] string { seed . NodeInfo ( ) . NetAddress ( ) . String ( ) } ,
} ,
& PEXReactorConfig { } ,
)
r . SetLogger ( log . TestingLogger ( ) )
sw . AddReactor ( "pex" , r )
return sw
} ,
)
peer . AddListener (
p2p . NewDefaultListener ( "tcp://" + peer . NodeInfo ( ) . ListenAddr , "" , false , log . TestingLogger ( ) ) ,
)
require . Nil ( t , peer . Start ( ) )
defer peer . Stop ( )
// 3. check that the peer connects to seed immediately
assertPeersWithTimeout ( t , [ ] * p2p . Switch { peer } , 10 * time . Millisecond , 3 * time . Second , 1 )
// 2. Create seed which knows about the peer
seed := testCreateSeed ( dir , 2 , [ ] * p2p . NetAddress { peer . NodeInfo ( ) . NetAddress ( ) } , [ ] * p2p . NetAddress { peer . NodeInfo ( ) . NetAddress ( ) } )
require . Nil ( t , seed . Start ( ) )
defer seed . Stop ( )
// 3. create another peer with only seed configured.
secondPeer := testCreatePeerWithSeed ( dir , 3 , seed )
require . Nil ( t , secondPeer . Start ( ) )
defer secondPeer . Stop ( )
// 4. check that the second peer connects to seed immediately
assertPeersWithTimeout ( t , [ ] * p2p . Switch { secondPeer } , 10 * time . Millisecond , 3 * time . Second , 1 )
// 5. check that the second peer connects to the first peer immediately
assertPeersWithTimeout ( t , [ ] * p2p . Switch { secondPeer } , 10 * time . Millisecond , 1 * time . Second , 2 )
}
func TestPEXReactorCrawlStatus ( t * testing . T ) {
@ -401,6 +410,7 @@ func assertPeersWithTimeout(
outbound , inbound , _ := s . NumPeers ( )
if outbound + inbound < nPeers {
allGood = false
break
}
}
remaining -= checkPeriod
@ -417,14 +427,77 @@ func assertPeersWithTimeout(
numPeersStr += fmt . Sprintf ( "%d => {outbound: %d, inbound: %d}, " , i , outbound , inbound )
}
t . Errorf (
"expected all switches to be connected to at least one peer (switches: %s)" ,
numPeersStr ,
"expected all switches to be connected to at least %d peer(s) (switches: %s)" ,
nPeers , n umPeersStr ,
)
return
}
}
}
// Creates a seed which knows about the provided addresses / source address pairs.
// Starting and stopping the seed is left to the caller
func testCreateSeed ( dir string , id int , knownAddrs , srcAddrs [ ] * p2p . NetAddress ) * p2p . Switch {
seed := p2p . MakeSwitch (
cfg ,
id ,
"127.0.0.1" ,
"123.123.123" ,
func ( i int , sw * p2p . Switch ) * p2p . Switch {
book := NewAddrBook ( filepath . Join ( dir , "addrbookSeed.json" ) , false )
book . SetLogger ( log . TestingLogger ( ) )
for j := 0 ; j < len ( knownAddrs ) ; j ++ {
book . AddAddress ( knownAddrs [ j ] , srcAddrs [ j ] )
book . MarkGood ( knownAddrs [ j ] )
}
sw . SetAddrBook ( book )
sw . SetLogger ( log . TestingLogger ( ) )
r := NewPEXReactor ( book , & PEXReactorConfig { } )
r . SetLogger ( log . TestingLogger ( ) )
sw . AddReactor ( "pex" , r )
return sw
} ,
)
seed . AddListener (
p2p . NewDefaultListener ( "tcp://" + seed . NodeInfo ( ) . ListenAddr , "" , false , log . TestingLogger ( ) ) ,
)
return seed
}
// Creates a peer which knows about the provided seed.
// Starting and stopping the peer is left to the caller
func testCreatePeerWithSeed ( dir string , id int , seed * p2p . Switch ) * p2p . Switch {
peer := p2p . MakeSwitch (
cfg ,
id ,
"127.0.0.1" ,
"123.123.123" ,
func ( i int , sw * p2p . Switch ) * p2p . Switch {
book := NewAddrBook ( filepath . Join ( dir , fmt . Sprintf ( "addrbook%d.json" , id ) ) , false )
book . SetLogger ( log . TestingLogger ( ) )
sw . SetAddrBook ( book )
sw . SetLogger ( log . TestingLogger ( ) )
r := NewPEXReactor (
book ,
& PEXReactorConfig {
Seeds : [ ] string { seed . NodeInfo ( ) . NetAddress ( ) . String ( ) } ,
} ,
)
r . SetLogger ( log . TestingLogger ( ) )
sw . AddReactor ( "pex" , r )
return sw
} ,
)
peer . AddListener (
p2p . NewDefaultListener ( "tcp://" + peer . NodeInfo ( ) . ListenAddr , "" , false , log . TestingLogger ( ) ) ,
)
return peer
}
func createReactor ( conf * PEXReactorConfig ) ( r * PEXReactor , book * addrBook ) {
// directory to store address book
dir , err := ioutil . TempDir ( "" , "pex_reactor" )