import test from 'ava' ; import { Lock , sleep } from './helpers/latch' ; import { SC , startServer , stopServer } from './helpers/nats_server_control' ; import { connect , Payload } from '../src/nats' ; import { next } from 'nuid' ; test . before ( async ( t ) => { let server = await startServer ( ) ; t . context = { server : server } ; } ) ; test . after . always ( ( t ) => { stopServer ( ( t . context as SC ) . server ) ; } ) ; test ( 'should yield to other events' , async ( t ) => { t . plan ( 2 ) ; let sc = t . context as SC ; let nc = await connect ( { url : sc . server . nats , payload : Payload . JSON , yieldTime : 5 } ) ; let lock = new Lock ( ) ; let last = - 1 ; let yields = 0 ; nc . on ( 'yield' , ( ) => { yields ++ ; } ) ; let interval = setInterval ( ( ) => { if ( last > 0 ) { clearInterval ( interval ) ; nc . close ( ) ; t . true ( last < 256 ) ; t . truthy ( yields ) ; lock . unlock ( ) ; } } , 10 ) ; let subj = next ( ) ; nc . subscribe ( subj , async ( err , msg ) => { last = msg . data ; sleep ( 1 ) ; } ) ; for ( let i = 0 ; i < 256 ; i ++ ) { nc . publish ( subj , i ) ; } nc . flush ( ) ; await lock . latch ; nc . close ( ) ; } ) ;