@ -46,7 +46,7 @@ public class PeerDiscoveryAgentTest {
public void neighborsPacketFromUnbondedPeerIsDropped ( ) {
// Start an agent with no bootstrap peers.
final MockPeerDiscoveryAgent agent = helper . startDiscoveryAgent ( Collections . emptyList ( ) ) ;
assertThat ( agent . get Peers( ) ) . isEmpty ( ) ;
assertThat ( agent . streamDiscovered Peers( ) ) . isEmpty ( ) ;
// Start a test peer
final MockPeerDiscoveryAgent otherNode = helper . startDiscoveryAgent ( ) ;
@ -57,7 +57,7 @@ public class PeerDiscoveryAgentTest {
final Packet packet = Packet . create ( PacketType . NEIGHBORS , data , otherNode . getKeyPair ( ) ) ;
helper . sendMessageBetweenAgents ( otherNode , agent , packet ) ;
assertThat ( agent . get Peers( ) ) . isEmpty ( ) ;
assertThat ( agent . streamDiscovered Peers( ) ) . isEmpty ( ) ;
}
@Test
@ -77,8 +77,9 @@ public class PeerDiscoveryAgentTest {
// list. By moving to a contains we make sure that all the peers are loaded with tolerance for
// duplicates. If we fix the duplication problem we should use containsExactlyInAnyOrder to
// hedge against missing one and duplicating another.
assertThat ( agent . getPeers ( ) ) . contains ( otherPeers . toArray ( new DiscoveryPeer [ 20 ] ) ) ;
assertThat ( agent . getPeers ( ) ) . allMatch ( p - > p . getStatus ( ) = = PeerDiscoveryStatus . BONDED ) ;
assertThat ( agent . streamDiscoveredPeers ( ) ) . contains ( otherPeers . toArray ( new DiscoveryPeer [ 20 ] ) ) ;
assertThat ( agent . streamDiscoveredPeers ( ) )
. allMatch ( p - > p . getStatus ( ) = = PeerDiscoveryStatus . BONDED ) ;
// Use additional agent to exchange messages with agent
final MockPeerDiscoveryAgent testAgent = helper . startDiscoveryAgent ( ) ;
@ -96,12 +97,12 @@ public class PeerDiscoveryAgentTest {
helper . sendMessageBetweenAgents ( testAgent , agent , packet ) ;
// Check response packet
List < IncomingPacket > incomingPackets =
final List < IncomingPacket > incomingPackets =
testAgent . getIncomingPackets ( ) . stream ( )
. filter ( p - > p . packet . getType ( ) . equals ( PacketType . NEIGHBORS ) )
. collect ( toList ( ) ) ;
assertThat ( incomingPackets . size ( ) ) . isEqualTo ( 1 ) ;
IncomingPacket neighborsPacket = incomingPackets . get ( 0 ) ;
final IncomingPacket neighborsPacket = incomingPackets . get ( 0 ) ;
assertThat ( neighborsPacket . fromAgent ) . isEqualTo ( agent ) ;
// Assert that we only received 16 items.
@ -128,12 +129,12 @@ public class PeerDiscoveryAgentTest {
final MockPeerDiscoveryAgent peerDiscoveryAgent2 = helper . startDiscoveryAgent ( peer ) ;
peerDiscoveryAgent2 . start ( BROADCAST_TCP_PORT ) . join ( ) ;
assertThat ( peerDiscoveryAgent2 . get Peers( ) . collect ( toList ( ) ) . size ( ) ) . isEqualTo ( 1 ) ;
assertThat ( peerDiscoveryAgent2 . streamDiscovered Peers( ) . collect ( toList ( ) ) . size ( ) ) . isEqualTo ( 1 ) ;
final PeerConnection peerConnection = createAnonymousPeerConnection ( peer . getId ( ) ) ;
peerDiscoveryAgent2 . onDisconnect ( peerConnection , DisconnectReason . REQUESTED , true ) ;
assertThat ( peerDiscoveryAgent2 . get Peers( ) . collect ( toList ( ) ) . size ( ) ) . isEqualTo ( 0 ) ;
assertThat ( peerDiscoveryAgent2 . streamDiscovered Peers( ) . collect ( toList ( ) ) . size ( ) ) . isEqualTo ( 0 ) ;
}
@Test
@ -148,24 +149,24 @@ public class PeerDiscoveryAgentTest {
// Bond to peer
bondViaIncomingPing ( agent , otherNode ) ;
assertThat ( agent . get Peers( ) ) . hasSize ( 1 ) ;
assertThat ( agent . streamDiscovered Peers( ) ) . hasSize ( 1 ) ;
// Disconnect with innocuous reason
blacklist . onDisconnect ( wirePeer , DisconnectReason . TOO_MANY_PEERS , false ) ;
agent . onDisconnect ( wirePeer , DisconnectReason . TOO_MANY_PEERS , false ) ;
// Confirm peer was removed
assertThat ( agent . get Peers( ) ) . hasSize ( 0 ) ;
assertThat ( agent . streamDiscovered Peers( ) ) . hasSize ( 0 ) ;
// Bond again
bondViaIncomingPing ( agent , otherNode ) ;
// Check peer was allowed to connect
assertThat ( agent . get Peers( ) ) . hasSize ( 1 ) ;
assertThat ( agent . streamDiscovered Peers( ) ) . hasSize ( 1 ) ;
}
protected void bondViaIncomingPing (
final MockPeerDiscoveryAgent agent , final MockPeerDiscoveryAgent otherNode ) {
Packet pingPacket = helper . createPingPacket ( otherNode , agent ) ;
final Packet pingPacket = helper . createPingPacket ( otherNode , agent ) ;
helper . sendMessageBetweenAgents ( otherNode , agent , pingPacket ) ;
}
@ -181,19 +182,19 @@ public class PeerDiscoveryAgentTest {
// Bond to peer
bondViaIncomingPing ( agent , otherNode ) ;
assertThat ( agent . get Peers( ) ) . hasSize ( 1 ) ;
assertThat ( agent . streamDiscovered Peers( ) ) . hasSize ( 1 ) ;
// Disconnect with problematic reason
blacklist . onDisconnect ( wirePeer , DisconnectReason . BREACH_OF_PROTOCOL , false ) ;
agent . onDisconnect ( wirePeer , DisconnectReason . BREACH_OF_PROTOCOL , false ) ;
// Confirm peer was removed
assertThat ( agent . get Peers( ) ) . hasSize ( 0 ) ;
assertThat ( agent . streamDiscovered Peers( ) ) . hasSize ( 0 ) ;
// Bond again
bondViaIncomingPing ( agent , otherNode ) ;
// Check peer was not allowed to connect
assertThat ( agent . get Peers( ) ) . hasSize ( 0 ) ;
assertThat ( agent . streamDiscovered Peers( ) ) . hasSize ( 0 ) ;
}
@Test
@ -208,19 +209,19 @@ public class PeerDiscoveryAgentTest {
// Bond to peer
bondViaIncomingPing ( agent , otherNode ) ;
assertThat ( agent . get Peers( ) ) . hasSize ( 1 ) ;
assertThat ( agent . streamDiscovered Peers( ) ) . hasSize ( 1 ) ;
// Disconnect with problematic reason
blacklist . onDisconnect ( wirePeer , DisconnectReason . BREACH_OF_PROTOCOL , true ) ;
agent . onDisconnect ( wirePeer , DisconnectReason . BREACH_OF_PROTOCOL , true ) ;
// Confirm peer was removed
assertThat ( agent . get Peers( ) ) . hasSize ( 0 ) ;
assertThat ( agent . streamDiscovered Peers( ) ) . hasSize ( 0 ) ;
// Bond again
bondViaIncomingPing ( agent , otherNode ) ;
// Check peer was allowed to connect
assertThat ( agent . get Peers( ) ) . hasSize ( 1 ) ;
assertThat ( agent . streamDiscovered Peers( ) ) . hasSize ( 1 ) ;
}
@Test
@ -235,19 +236,19 @@ public class PeerDiscoveryAgentTest {
// Bond to peer
bondViaIncomingPing ( agent , otherNode ) ;
assertThat ( agent . get Peers( ) ) . hasSize ( 1 ) ;
assertThat ( agent . streamDiscovered Peers( ) ) . hasSize ( 1 ) ;
// Disconnect
blacklist . onDisconnect ( wirePeer , DisconnectReason . INCOMPATIBLE_P2P_PROTOCOL_VERSION , false ) ;
agent . onDisconnect ( wirePeer , DisconnectReason . INCOMPATIBLE_P2P_PROTOCOL_VERSION , false ) ;
// Confirm peer was removed
assertThat ( agent . get Peers( ) ) . hasSize ( 0 ) ;
assertThat ( agent . streamDiscovered Peers( ) ) . hasSize ( 0 ) ;
// Bond again
bondViaIncomingPing ( agent , otherNode ) ;
// Check peer was not allowed to connect
assertThat ( agent . get Peers( ) ) . hasSize ( 0 ) ;
assertThat ( agent . streamDiscovered Peers( ) ) . hasSize ( 0 ) ;
}
@Test
@ -262,24 +263,24 @@ public class PeerDiscoveryAgentTest {
// Bond to peer
bondViaIncomingPing ( agent , otherNode ) ;
assertThat ( agent . get Peers( ) ) . hasSize ( 1 ) ;
assertThat ( agent . streamDiscovered Peers( ) ) . hasSize ( 1 ) ;
// Disconnect
blacklist . onDisconnect ( wirePeer , DisconnectReason . INCOMPATIBLE_P2P_PROTOCOL_VERSION , true ) ;
agent . onDisconnect ( wirePeer , DisconnectReason . INCOMPATIBLE_P2P_PROTOCOL_VERSION , true ) ;
// Confirm peer was removed
assertThat ( agent . get Peers( ) ) . hasSize ( 0 ) ;
assertThat ( agent . streamDiscovered Peers( ) ) . hasSize ( 0 ) ;
// Bond again
bondViaIncomingPing ( agent , otherNode ) ;
// Check peer was not allowed to connect
assertThat ( agent . get Peers( ) ) . hasSize ( 0 ) ;
assertThat ( agent . streamDiscovered Peers( ) ) . hasSize ( 0 ) ;
}
@Test
public void shouldBeActiveWhenConfigIsTrue ( ) {
AgentBuilder agentBuilder = helper . agentBuilder ( ) . active ( true ) ;
final AgentBuilder agentBuilder = helper . agentBuilder ( ) . active ( true ) ;
final MockPeerDiscoveryAgent agent = helper . startDiscoveryAgent ( agentBuilder ) ;
assertThat ( agent . isActive ( ) ) . isTrue ( ) ;
@ -287,15 +288,15 @@ public class PeerDiscoveryAgentTest {
@Test
public void shouldNotBeActiveWhenConfigIsFalse ( ) {
AgentBuilder agentBuilder = helper . agentBuilder ( ) . active ( false ) ;
final AgentBuilder agentBuilder = helper . agentBuilder ( ) . active ( false ) ;
final MockPeerDiscoveryAgent agent = helper . startDiscoveryAgent ( agentBuilder ) ;
assertThat ( agent . isActive ( ) ) . isFalse ( ) ;
}
private PeerConnection createAnonymousPeerConnection ( final BytesValue id ) {
PeerConnection conn = mock ( PeerConnection . class ) ;
PeerInfo peerInfo = new PeerInfo ( 0 , null , null , 0 , id ) ;
final PeerConnection conn = mock ( PeerConnection . class ) ;
final PeerInfo peerInfo = new PeerInfo ( 0 , null , null , 0 , id ) ;
when ( conn . getPeerInfo ( ) ) . thenReturn ( peerInfo ) ;
return conn ;
}