import { P2P } from '../../../src/index' ; import { createNetwork , destroyNetwork , NETWORK_START_PORT , NETWORK_PEER_COUNT , } from '../../utils/network_setup' ; import { wait } from '../../utils/helpers' ; describe ( 'PeerPool actions' , ( ) => { let p2pNodeList = [ ] ; describe ( 'getConnectedPeers' , ( ) => { const ALL_NODE_PORTS = [ ... new Array ( NETWORK_PEER_COUNT ) . keys ( ) ] . map ( index => NETWORK_START_PORT + index ) ; beforeEach ( async ( ) => { p2pNodeList = await createNetwork ( ) ; await wait ( 1000 ) ; } ) ; afterEach ( async ( ) => { await destroyNetwork ( p2pNodeList ) ; } ) ; it ( 'should discover all peers and add them to the connectedPeers list within each node' , ( ) => { const firstNode = p2pNodeList [ 0 ] ; const peerPorts = firstNode . getConnectedPeers ( ) . map ( peerInfo => peerInfo . port ) . sort ( ) ; const expectedPeerPorts = ALL_NODE_PORTS . filter ( port => { return port !== firstNode . config . port ; } ) ; expect ( peerPorts ) . toEqual ( expectedPeerPorts ) ; } ) ; } ) ; describe ( 'getDisconnectedPeers' , ( ) => { const LIMITED_CONNECTIONS = 3 ; beforeEach ( async ( ) => { const customSeedPeers = ( index , startPort , networkSize ) => [ { ipAddress : '127.0.0.1' , port : startPort + ( ( index + 1 ) % networkSize ) , } , ] ; const customConfig = ( index , startPort , networkSize ) => ( { populatorInterlatencyProtectionRatio : 0 , productivityProtectionRatio : 0 , longevityProtectionRatio : 0 , maxOutboundConnections : LIMITED_CONNECTIONS , maxInboundConnections : LIMITED_CONNECTIONS , seedPeers : customSeedPeers ( index , startPort , networkSize ) , } ) ; p2pNodeList = await createNetwork ( { customConfig , } ) ; } ) ; afterEach ( async ( ) => { await destroyNetwork ( p2pNodeList ) ; } ) ; it ( 'should have disjoint connected and disconnected peers' , ( ) => { for ( const p2p of p2pNodeList ) { const connectedPeers = p2p . getConnectedPeers ( ) ; const disconnectedPeers = p2p . getDisconnectedPeers ( ) ; for ( const connectedPeer of connectedPeers ) { expect ( disconnectedPeers ) . toEqual ( expect . not . arrayContaining ( [ connectedPeer ] ) ) ; } } } ) ; } ) ; } ) ;