import { P2P , events } from '../../src/index' ; import { wait } from '../utils/helpers' ; import { createNetwork , destroyNetwork } from '../utils/network_setup' ; const { EVENT_DISCOVERED_PEER } = events ; describe ( 'Peer discovery threshold' , ( ) => { let p2pNodeList = [ ] ; const MINIMUM_PEER_DISCOVERY_THRESHOLD = 10 ; const MAX_PEER_DISCOVERY_RESPONSE_LENGTH = 100 ; const listOfPeers = [ ] ; beforeEach ( async ( ) => { const customConfig = ( ) => ( { minimumPeerDiscoveryThreshold : MINIMUM_PEER_DISCOVERY_THRESHOLD , maxPeerDiscoveryResponseLength : MAX_PEER_DISCOVERY_RESPONSE_LENGTH , fallbackSeedPeerDiscoveryInterval : 10000 , populatorInterval : 10000 , } ) ; p2pNodeList = await createNetwork ( { networkSize : 2 , networkDiscoveryWaitTime : 1 , customConfig , } ) ; for ( let i = 0 ; i < 1000 ; i += 1 ) { const generatedIP = `${ Math . floor ( Math . random ( ) * 254 ) + 1 }.${ Math . floor ( Math . random ( ) * 254 ) + 1 }.${ Math . floor ( Math . random ( ) * 254 ) + 1 }.${ Math . floor ( Math . random ( ) * 254 ) + 1 }` ; p2pNodeList [ 0 ] [ '_peerBook' ] . addPeer ( { peerId : `${ generatedIP }:5000` , ipAddress : generatedIP , port : 1000 , sharedState : { networkVersion : '1.1' , networkIdentifier : 'networkId' , nonce : 'nonce' , options : { } , } , } ) ; } p2pNodeList [ 1 ] . on ( EVENT_DISCOVERED_PEER , peer => { listOfPeers . push ( peer ) ; } ) ; await wait ( 1000 ) ; } ) ; afterEach ( async ( ) => { await destroyNetwork ( p2pNodeList ) ; } ) ; it ( `should return list of peers with size between ${ MINIMUM_PEER_DISCOVERY_THRESHOLD } - ${ MAX_PEER_DISCOVERY_RESPONSE_LENGTH }` , ( ) => { expect ( listOfPeers . length >= MINIMUM_PEER_DISCOVERY_THRESHOLD && listOfPeers . length <= MAX_PEER_DISCOVERY_RESPONSE_LENGTH ) . toEqual ( true ) ; } ) ; } ) ;