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