import { SC , startServer , stopServer } from './helpers/nats_server_control' ; import test , { ExecutionContext } from 'ava' ; import { connect } from '../src/nats' ; import { Lock } from './helpers/latch' ; test . before ( async ( t ) => { let s1 = await startServer ( ) ; let s2 = await startServer ( ) ; t . context = { server : s1 , servers : [ s1 , s2 ] } ; } ) ; test . after . always ( ( t ) => { ( t . context as SC ) . servers . forEach ( ( s ) => { stopServer ( s ) ; } ) ; } ) ; function getServers ( t ) { let a = [ ] ; let servers = ( t . context as SC ) . servers ; servers . forEach ( ( s ) => { a . push ( s . nats ) ; } ) ; return a ; } test ( 'has multiple servers' , async ( t ) => { t . plan ( 1 ) ; let a = getServers ( t ) ; let nc = await connect ( { servers : a } ) ; let servers = nc . protocolHandler . servers ; t . is ( servers . length ( ) , a . length ) ; nc . close ( ) ; } ) ; test ( 'connects to first valid server' , async ( t ) => { let lock = new Lock ( ) ; let a = getServers ( t ) ; a . splice ( 0 , 0 , 'nats://localhost:7' ) ; let nc = await connect ( { servers : a } ) ; nc . on ( 'connect' , ( ) => { t . pass ( ) ; nc . close ( ) ; lock . unlock ( ) ; } ) ; return lock . latch ; } ) ; test ( 'reject if no valid server' , async ( t ) => { t . plan ( 1 ) ; let a = [ 'nats://localhost:7' ] ; await t . throwsAsync ( ( ) => { return connect ( { servers : a , reconnectTimeWait : 50 } ) ; } , { code : 'ECONNREFUSED' } ) ; } ) ; test ( 'throws if no valid server' , async ( t ) => { t . plan ( 1 ) ; let a = [ 'nats://localhost:7' , 'nats://localhost:9' ] ; try { await connect ( { servers : a , reconnectTimeWait : 50 } ) ; } catch ( ex ) { t . is ( ex . code , 'ECONNREFUSED' ) ; } } ) ; test ( 'random server connections' , async ( t ) => { let first = getServers ( t ) [ 0 ] ; let count = 0 ; let other = 0 ; for ( let i = 0 ; i < 100 ; i ++ ) { let nc = await connect ( { servers : getServers ( t ) } ) ; let s = nc . protocolHandler . servers . getCurrentServer ( ) ; if ( s . toString ( ) === first ) { count ++ ; } else { other ++ ; } nc . close ( ) ; } t . is ( count + other , 100 ) ; t . true ( count >= 35 ) ; t . true ( count <= 65 ) ; } ) ; test ( 'no random server if noRandomize' , async ( t ) => { let servers = getServers ( t ) ; let first = servers [ 0 ] . toString ( ) ; for ( let i = 0 ; i < 100 ; i ++ ) { let nc = await connect ( { servers : servers , noRandomize : true } ) ; let s = nc . protocolHandler . currentServer ; t . is ( s . toString ( ) , first . toString ( ) ) ; nc . close ( ) ; } } ) ;