import { SCServerSocket } from 'socketcluster-server' ; import * as url from 'url' ; import { P2P } from '../../src/index' ; import { createNetwork , destroyNetwork } from '../utils/network_setup' ; import { OutboundPeer } from '../../src/peer' ; import cloneDeep = require ( 'lodash.clonedeep' ) ; describe ( 'Outbound IP limit' , ( ) => { const serverSocketPrototypeBackup = cloneDeep ( SCServerSocket . prototype ) ; let p2pNodeList = [ ] ; beforeAll ( ( ) => { const serverSocketPrototype = SCServerSocket . prototype as any ; const realResetPongTimeoutFunction = serverSocketPrototype . _resetPongTimeout ; serverSocketPrototype . _resetPongTimeout = function ( ) { const queryObject = url . parse ( this . request . url , true ) . query as any ; const ipSuffix = queryObject . port - 5000 ; this . remoteAddress = `127.0.0.${ ~ ~ ( ipSuffix / 2 ) + 1 }` ; return realResetPongTimeoutFunction . apply ( this , arguments ) ; } ; } ) ; beforeEach ( async ( ) => { const customSeedPeers = ( index , startPort ) => index !== 0 ? [ { ipAddress : '127.0.0.1' , port : startPort , } , ] : [ ] ; const customConfig = ( index , startPort ) => ( { hostIp : `127.0.0.${ ~ ~ ( ( index + 3 ) / 2 ) }` , seedPeers : customSeedPeers ( index , startPort ) , } ) ; p2pNodeList = await createNetwork ( { networkSize : 9 , customConfig } ) ; } ) ; afterEach ( async ( ) => { await destroyNetwork ( p2pNodeList ) ; } ) ; it ( 'should not have multiple Outbound connection for same IP addresses' , ( ) => { for ( const p2p of p2pNodeList ) { const uniqIpAddresses = [ ] ; p2p [ '_peerPool' ] . getPeers ( OutboundPeer ) . map ( peer => uniqIpAddresses . push ( peer . ipAddress ) ) ; expect ( [ ... new Set ( uniqIpAddresses ) ] ) . toHaveLength ( p2p [ '_peerPool' ] . getPeers ( OutboundPeer ) . length ) ; } } ) ; afterAll ( ( ) => { SCServerSocket . prototype = serverSocketPrototypeBackup ; } ) ; } ) ;