import { SC , startServer , stopServer } from './helpers/nats_server_control' ; import test from 'ava' ; import { connect , Payload } from '../src/nats' ; import { randomBytes } from 'crypto' ; 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 ) ; } ) ; async function macro ( t , input , payload ) { let max = 10000 ; t . plan ( max ) ; let sc = t . context as SC ; let nc = await connect ( { url : sc . server . nats , payload : payload } ) ; let subj = next ( ) ; nc . subscribe ( subj , ( err , msg ) => { if ( err ) { t . fail ( err . message ) ; } t . deepEqual ( msg . data , input ) ; } , { max : max } ) ; for ( let i = 0 ; i < max ; i ++ ) { nc . publish ( subj , input ) ; } await nc . flush ( ) ; nc . close ( ) ; } test ( 'large # of utf8 messages from split buffers' , macro , '½ + ¼ = ¾' , Payload . STRING ) ; test ( 'large # of messages from split buffers' , macro , 'hello world' , Payload . STRING ) ; test ( 'large # of binary messages from split buffers' , macro , randomBytes ( 50 ) , Payload . BINARY ) ;