11import * as messages from '../messages' ;
22
3- import { withCloseable } from 'launchdarkly-js-test-helpers' ;
3+ import { withCloseable , sleepAsync } from 'launchdarkly-js-test-helpers' ;
44
55import { respond , respondJson } from './mockHttp' ;
66import * as stubPlatform from './stubPlatform' ;
@@ -426,6 +426,28 @@ describe('LDClient events', () => {
426426 } ) ;
427427 } ) ;
428428
429+ async function expectDiagnosticEventAndDiscardRegularEvent ( server ) {
430+ const req0 = await server . nextRequest ( ) ;
431+ const req1 = await server . nextRequest ( ) ;
432+ const expectedPath = '/events/diagnostic/' + envName ;
433+ const otherPath = '/events/bulk/' + envName ;
434+ let initEventReq ;
435+ if ( req0 . path === expectedPath ) {
436+ expect ( req1 . path ) . toEqual ( otherPath ) ;
437+ initEventReq = req0 ;
438+ } else {
439+ expect ( req0 . path ) . toEqual ( otherPath ) ;
440+ expect ( req1 . path ) . toEqual ( expectedPath ) ;
441+ initEventReq = req1 ;
442+ }
443+ return JSON . parse ( initEventReq . body ) ;
444+ }
445+
446+ async function expectNoMoreRequests ( server , timeout ) {
447+ await sleepAsync ( timeout ) ;
448+ expect ( server . requests . length ( ) ) . toEqual ( 0 ) ;
449+ }
450+
429451 it ( 'sends diagnostic init event on startup' , async ( ) => {
430452 const server = platform . testing . http . newServer ( ) ;
431453 server . byDefault ( respond ( 202 ) ) ;
@@ -439,15 +461,11 @@ describe('LDClient events', () => {
439461 await withCloseable ( client , async ( ) => {
440462 await client . waitForInitialization ( ) ;
441463 await client . flush ( ) ;
442- const req0 = await server . nextRequest ( ) ;
443- const req1 = await server . nextRequest ( ) ;
444- const expectedPath = '/events/diagnostic/' + envName ;
445- expect ( [ req0 . path , req1 . path ] ) . toContain ( expectedPath ) ;
446- const req = req0 . path === expectedPath ? req0 : req1 ;
447- const data = JSON . parse ( req . body ) ;
464+ const data = await expectDiagnosticEventAndDiscardRegularEvent ( server ) ;
448465 expect ( data . kind ) . toEqual ( 'diagnostic-init' ) ;
449466 expect ( data . platform ) . toEqual ( platform . diagnosticPlatformData ) ;
450467 expect ( data . sdk ) . toEqual ( platform . diagnosticSdkData ) ;
468+ await expectNoMoreRequests ( server , 50 ) ;
451469 } ) ;
452470 } ) ;
453471 } ) ;
@@ -467,15 +485,11 @@ describe('LDClient events', () => {
467485 await withCloseable ( client , async ( ) => {
468486 await client . waitForInitialization ( ) ;
469487 await client . flush ( ) ;
470- const req0 = await server . nextRequest ( ) ;
471- const req1 = await server . nextRequest ( ) ;
472- const expectedPath = '/events/diagnostic/' + envName ;
473- expect ( [ req0 . path , req1 . path ] ) . toContain ( expectedPath ) ;
474- const req = req0 . path === expectedPath ? req0 : req1 ;
475- const data = JSON . parse ( req . body ) ;
488+ const data = await expectDiagnosticEventAndDiscardRegularEvent ( server ) ;
476489 expect ( data . kind ) . toEqual ( 'diagnostic-combined' ) ;
477490 expect ( data . platform ) . toEqual ( platform1 . diagnosticPlatformData ) ;
478491 expect ( data . sdk ) . toEqual ( platform1 . diagnosticSdkData ) ;
492+ await expectNoMoreRequests ( server , 50 ) ;
479493 } ) ;
480494 } ) ;
481495 } ) ;
@@ -497,6 +511,7 @@ describe('LDClient events', () => {
497511 expect ( server . requests . length ( ) ) . toEqual ( 1 ) ;
498512 const req = await server . nextRequest ( ) ;
499513 expect ( req . path ) . toEqual ( '/events/bulk/' + envName ) ;
514+ await expectNoMoreRequests ( server , 50 ) ;
500515 } ) ;
501516 } ) ;
502517 } ) ;
0 commit comments