@@ -875,7 +875,7 @@ describe('BatchEventProcessor', async () => {
875875 } ) ;
876876
877877 describe ( 'retryFailedEvents' , ( ) => {
878- it ( 'should disptach only failed events from the store and not dispatch queued events' , async ( ) => {
878+ it ( 'should dispatch only failed events from the store and not dispatch queued events' , async ( ) => {
879879 const eventDispatcher = getMockDispatcher ( ) ;
880880 const mockDispatch : MockInstance < typeof eventDispatcher . dispatchEvent > = eventDispatcher . dispatchEvent ;
881881 mockDispatch . mockResolvedValue ( { } ) ;
@@ -921,7 +921,7 @@ describe('BatchEventProcessor', async () => {
921921 ] ) ) ;
922922 } ) ;
923923
924- it ( 'should disptach only failed events from the store and not dispatch events that are being dispatched' , async ( ) => {
924+ it ( 'should dispatch only failed events from the store and not dispatch events that are being dispatched' , async ( ) => {
925925 const eventDispatcher = getMockDispatcher ( ) ;
926926 const mockDispatch : MockInstance < typeof eventDispatcher . dispatchEvent > = eventDispatcher . dispatchEvent ;
927927 const mockResult1 = resolvablePromise ( ) ;
@@ -977,7 +977,7 @@ describe('BatchEventProcessor', async () => {
977977 ] ) ) ;
978978 } ) ;
979979
980- it ( 'should disptach events in correct batch size and separate events with differnt contexts in separate batch' , async ( ) => {
980+ it ( 'should dispatch events in correct batch size and separate events with differnt contexts in separate batch' , async ( ) => {
981981 const eventDispatcher = getMockDispatcher ( ) ;
982982 const mockDispatch : MockInstance < typeof eventDispatcher . dispatchEvent > = eventDispatcher . dispatchEvent ;
983983 mockDispatch . mockResolvedValue ( { } ) ;
@@ -1023,7 +1023,7 @@ describe('BatchEventProcessor', async () => {
10231023 } ) ;
10241024
10251025 describe ( 'when failedEventRepeater is fired' , ( ) => {
1026- it ( 'should disptach only failed events from the store and not dispatch queued events' , async ( ) => {
1026+ it ( 'should dispatch only failed events from the store and not dispatch queued events' , async ( ) => {
10271027 const eventDispatcher = getMockDispatcher ( ) ;
10281028 const mockDispatch : MockInstance < typeof eventDispatcher . dispatchEvent > = eventDispatcher . dispatchEvent ;
10291029 mockDispatch . mockResolvedValue ( { } ) ;
@@ -1071,7 +1071,7 @@ describe('BatchEventProcessor', async () => {
10711071 ] ) ) ;
10721072 } ) ;
10731073
1074- it ( 'should disptach only failed events from the store and not dispatch events that are being dispatched' , async ( ) => {
1074+ it ( 'should dispatch only failed events from the store and not dispatch events that are being dispatched' , async ( ) => {
10751075 const eventDispatcher = getMockDispatcher ( ) ;
10761076 const mockDispatch : MockInstance < typeof eventDispatcher . dispatchEvent > = eventDispatcher . dispatchEvent ;
10771077 const mockResult1 = resolvablePromise ( ) ;
@@ -1129,7 +1129,7 @@ describe('BatchEventProcessor', async () => {
11291129 ] ) ) ;
11301130 } ) ;
11311131
1132- it ( 'should disptach events in correct batch size and separate events with differnt contexts in separate batch' , async ( ) => {
1132+ it ( 'should dispatch events in correct batch size and separate events with differnt contexts in separate batch' , async ( ) => {
11331133 const eventDispatcher = getMockDispatcher ( ) ;
11341134 const mockDispatch : MockInstance < typeof eventDispatcher . dispatchEvent > = eventDispatcher . dispatchEvent ;
11351135 mockDispatch . mockResolvedValue ( { } ) ;
@@ -1277,7 +1277,7 @@ describe('BatchEventProcessor', async () => {
12771277 expect ( failedEventRepeater . stop ) . toHaveBeenCalledOnce ( ) ;
12781278 } ) ;
12791279
1280- it ( 'should disptach the events in queue using the closing dispatcher if available' , async ( ) => {
1280+ it ( 'should dispatch the events in queue using the closing dispatcher if available' , async ( ) => {
12811281 const eventDispatcher = getMockDispatcher ( ) ;
12821282 const closingEventDispatcher = getMockDispatcher ( ) ;
12831283 closingEventDispatcher . dispatchEvent . mockResolvedValue ( { } ) ;
@@ -1408,4 +1408,76 @@ describe('BatchEventProcessor', async () => {
14081408 await expect ( processor . onTerminated ( ) ) . resolves . not . toThrow ( ) ;
14091409 } ) ;
14101410 } ) ;
1411+
1412+ describe ( 'flushImmediately' , ( ) => {
1413+ it ( 'should dispatch the events in queue using the closing dispatcher if available' , async ( ) => {
1414+ const eventDispatcher = getMockDispatcher ( ) ;
1415+ const closingEventDispatcher = getMockDispatcher ( ) ;
1416+ closingEventDispatcher . dispatchEvent . mockResolvedValue ( { } ) ;
1417+
1418+ const dispatchRepeater = getMockRepeater ( ) ;
1419+ const failedEventRepeater = getMockRepeater ( ) ;
1420+
1421+ const processor = new BatchEventProcessor ( {
1422+ eventDispatcher,
1423+ closingEventDispatcher,
1424+ dispatchRepeater,
1425+ failedEventRepeater,
1426+ batchSize : 100 ,
1427+ } ) ;
1428+
1429+ processor . start ( ) ;
1430+ await processor . onRunning ( ) ;
1431+
1432+ const events : ProcessableEvent [ ] = [ ] ;
1433+ for ( let i = 0 ; i < 10 ; i ++ ) {
1434+ const event = createImpressionEvent ( `id-${ i } ` ) ;
1435+ events . push ( event ) ;
1436+ await processor . process ( event ) ;
1437+ }
1438+
1439+ expect ( eventDispatcher . dispatchEvent ) . toHaveBeenCalledTimes ( 0 ) ;
1440+ expect ( closingEventDispatcher . dispatchEvent ) . toHaveBeenCalledTimes ( 0 ) ;
1441+
1442+ processor . flushImmediately ( ) ;
1443+ expect ( closingEventDispatcher . dispatchEvent ) . toHaveBeenCalledTimes ( 1 ) ;
1444+ expect ( closingEventDispatcher . dispatchEvent ) . toHaveBeenCalledWith ( buildLogEvent ( events ) ) ;
1445+
1446+ expect ( processor . isRunning ( ) ) . toBe ( true ) ;
1447+ } ) ;
1448+
1449+
1450+ it ( 'should dispatch the events in queue using eventDispatcher if closingEventDispatcher is not available' , async ( ) => {
1451+ const eventDispatcher = getMockDispatcher ( ) ;
1452+ eventDispatcher . dispatchEvent . mockResolvedValue ( { } ) ;
1453+
1454+ const dispatchRepeater = getMockRepeater ( ) ;
1455+ const failedEventRepeater = getMockRepeater ( ) ;
1456+
1457+ const processor = new BatchEventProcessor ( {
1458+ eventDispatcher,
1459+ dispatchRepeater,
1460+ failedEventRepeater,
1461+ batchSize : 100 ,
1462+ } ) ;
1463+
1464+ processor . start ( ) ;
1465+ await processor . onRunning ( ) ;
1466+
1467+ const events : ProcessableEvent [ ] = [ ] ;
1468+ for ( let i = 0 ; i < 10 ; i ++ ) {
1469+ const event = createImpressionEvent ( `id-${ i } ` ) ;
1470+ events . push ( event ) ;
1471+ await processor . process ( event ) ;
1472+ }
1473+
1474+ expect ( eventDispatcher . dispatchEvent ) . toHaveBeenCalledTimes ( 0 ) ;
1475+
1476+ processor . flushImmediately ( ) ;
1477+ expect ( eventDispatcher . dispatchEvent ) . toHaveBeenCalledTimes ( 1 ) ;
1478+ expect ( eventDispatcher . dispatchEvent ) . toHaveBeenCalledWith ( buildLogEvent ( events ) ) ;
1479+
1480+ expect ( processor . isRunning ( ) ) . toBe ( true ) ;
1481+ } ) ;
1482+ } ) ;
14111483} ) ;
0 commit comments