import test from 'ava' ; import { SC , startServer , stopServer } from './helpers/nats_server_control' ; import * as child_process from 'child_process' ; import url from 'url' ; import { connect } from '../src/nats' ; import { Lock } from './helpers/latch' ; test . before ( async ( t ) => { let server = await startServer ( ) ; t . context = { server : server } ; } ) ; test . after . always ( ( t ) => { stopServer ( t . context . server ) ; } ) ; test ( 'close quickly' , async ( t ) => { t . plan ( 1 ) ; let lock = new Lock ( ) ; let sc = t . context as SC ; let u = new url . URL ( sc . server . nats ) ; let port = u . port ; let nc = await connect ( { url : sc . server . nats , name : 'closer' } ) ; let sub = nc . subscribe ( 'started' , ( err , msg ) => { nc . publish ( 'close' ) ; } ) ; let timer = setTimeout ( ( ) => { t . fail ( 'process didn\'t exit quickly' ) ; lock . unlock ( ) ; } , 10000 ) ; let child = child_process . execFile ( process . argv [ 0 ] , [ __dirname + '/helpers/exiting_client.js' , port ] , ( error ) => { if ( error ) { nc . close ( ) ; t . fail ( error . message ) ; lock . unlock ( ) ; } } ) ; child . on ( 'error' , ( err ) => { t . log ( err ) ; } ) ; child . on ( 'exit' , ( code , signal ) => { if ( timer ) { clearTimeout ( timer ) ; } nc . close ( ) ; if ( code !== 0 ) { t . fail ( `Process didn't return a zero code: ${ code } signal: ${ signal }` ) ; } else { t . pass ( ) ; } lock . unlock ( ) ; } ) ; return lock . latch ; } ) ;