import { NODE_NAME , SOCKET_EMIT_INTERVAL } from 'test/lab/config' ; import { TEST_RUNNER_NAME } from 'test/lab/utils/constants' ; import socketFactory from 'test/lab/utils/socket/client' ; import testSocketServer from 'test/lab/utils/socket/testSocketServer' ; export interface NetworkSynchronizer { syncAll ( testStageName ? ) : Promise < void > ; syncTestRunnerNode ( testStageName ? ) : Promise < void > ; syncPeerNode ( testStageName ? ) : Promise < void > ; } export class NodesNetworkSynchronizer implements NetworkSynchronizer { private testName : string ; constructor ( testName ) { this . testName = testName ; } async syncAll ( testStageName = this . testName ) { NODE_NAME === TEST_RUNNER_NAME ? await this . syncTestRunnerNode ( testStageName ) : await this . syncPeerNode ( testStageName ) ; } syncPeerNode ( testStageName = this . testName ) { return new Promise ( resolve => { const data = { node : NODE_NAME } ; const socket = socketFactory . socketConnection ; const timer = setInterval ( ( ) => { socket . emit ( testStageName , data ) ; } , SOCKET_EMIT_INTERVAL ) ; socket . on ( testStageName , ( response ) => { clearInterval ( timer ) ; if ( response . success ) { resolve ( ) ; } } ) ; socket . emit ( testStageName , data ) ; } ) ; } syncTestRunnerNode ( testStageName = this . testName ) { return new Promise ( resolve => { const peerResponses = new Map ( ) ; testSocketServer . register ( testStageName , ( socket , response ) => { const peerName = response . node ; peerResponses . set ( peerName , true ) ; const resultCount = peerResponses . size ; socket . emit ( testStageName , { success : true } ) ; if ( resultCount === 2 ) { resolve ( ) ; } } ) ; } ) ; } }