import test from 'ava' ; import { addClusterMember , SC , startServer , stopServer } from './helpers/nats_server_control' ; import { connect } from '../src/nats' ; import { join } from 'path' ; import { Lock } from './helpers/latch' ; import { readFileSync } from 'fs' ; test . before ( async ( t ) => { let cert = join ( __dirname , '../../test/helpers/certs/server_noip.pem' ) ; let key = join ( __dirname , '../../test/helpers/certs/key_noip.pem' ) ; let ca = join ( __dirname , '../../test/helpers/certs/ca.pem' ) ; t . context = { servers : [ ] , ca : readFileSync ( ca ) } ; let flags = [ '--tls' , '--tlscert' , cert , '--tlskey' , key ] ; let s1 = await startServer ( flags ) ; t . context . servers . push ( s1 ) ; let s2 = await addClusterMember ( s1 , flags ) ; t . context . servers . push ( s2 ) ; } ) ; test . after . always ( ( t ) => { ( t . context as SC ) . servers . forEach ( ( s ) => { stopServer ( s ) ; } ) ; } ) ; test ( 'tls connect with ips' , async ( t ) => { t . plan ( 1 ) ; let s1 = t . context . servers [ 0 ] ; let lock = new Lock ( ) ; try { let nc = await connect ( { url : `tls://localhost:${ s1 . port }` , tls : { ca : t . context . ca } , reconnectTimeWait : 500 } ) ; nc . on ( 'connect' , ( ) => { if ( nc . protocolHandler . servers . length ( ) > 1 ) { stopServer ( s1 ) ; } else { nc . on ( 'serversChanged' , ( sc ) => { if ( nc . protocolHandler . servers . length ( ) > 1 ) { stopServer ( s1 ) ; } } ) ; } } ) ; nc . on ( 'reconnect' , ( ) => { nc . close ( ) ; t . pass ( ) ; lock . unlock ( ) ; } ) ; } catch ( err ) { console . log ( err ) ; t . fail ( err ) ; lock . unlock ( ) ; } return lock . latch ; } ) ;