import * as semver from "semver" ; import * as zmq from "../../src" ; import { assert } from "chai" ; import { testProtos , uniqAddress } from "./helpers" ; for ( const proto of testProtos ( "tcp" , "ipc" , "inproc" ) ) { describe ( `proxy with ${ proto } router/dealer` , function ( ) { let proxy ; let frontAddress ; let backAddress ; let req ; let rep ; beforeEach ( async function ( ) { if ( semver . satisfies ( zmq . version , "< 4.0.5" ) ) this . skip ( ) ; proxy = new zmq . Proxy ( new zmq . Router ( ) , new zmq . Dealer ( ) ) ; frontAddress = uniqAddress ( proto ) ; backAddress = uniqAddress ( proto ) ; req = new zmq . Request ( ) ; rep = new zmq . Reply ( ) ; } ) ; afterEach ( function ( ) { proxy . frontEnd . close ( ) ; proxy . backEnd . close ( ) ; req . close ( ) ; rep . close ( ) ; global . gc ?. ( ) ; } ) ; describe ( "run" , function ( ) { it ( "should proxy messages" , async function ( ) { await proxy . frontEnd . bind ( frontAddress ) ; await proxy . backEnd . bind ( backAddress ) ; const done = proxy . run ( ) ; const messages = [ "foo" , "bar" , "baz" , "qux" ] ; const received = [ ] ; await req . connect ( frontAddress ) ; await rep . connect ( backAddress ) ; const echo = async ( ) => { for await ( const msg of rep ) { await rep . send ( msg ) ; } } ; const send = async ( ) => { for ( const msg of messages ) { if ( received . length === 2 ) { proxy . pause ( ) ; proxy . resume ( ) ; } await req . send ( Buffer . from ( msg ) ) ; const [ res ] = await req . receive ( ) ; received . push ( res . toString ( ) ) ; if ( received . length === messages . length ) break ; } rep . close ( ) ; } ; await Promise . all ( [ echo ( ) , send ( ) ] ) ; assert . deepEqual ( received , messages ) ; proxy . terminate ( ) ; await done ; } ) ; } ) ; } ) ; }