@@ -5,13 +5,11 @@ import { NotificationManager } from "../../../../src/core/NotificationManager";
55
66export class SdkStatsNotificationCbkTests extends AITestClass {
77 private _trackedItems : ITelemetryItem [ ] ;
8- private _flushCalled : boolean ;
98 private _listener : ISdkStatsNotifCbk ;
109
1110 public testInitialize ( ) {
1211 super . testInitialize ( ) ;
1312 this . _trackedItems = [ ] ;
14- this . _flushCalled = false ;
1513 this . _listener = null ;
1614 }
1715
@@ -22,7 +20,6 @@ export class SdkStatsNotificationCbkTests extends AITestClass {
2220 this . _listener = null ;
2321 }
2422 this . _trackedItems = [ ] ;
25- this . _flushCalled = false ;
2623 }
2724
2825 public registerTests ( ) {
@@ -31,6 +28,7 @@ export class SdkStatsNotificationCbkTests extends AITestClass {
3128 this . _testEventsDiscarded ( ) ;
3229 this . _testEventsRetry ( ) ;
3330 this . _testFlush ( ) ;
31+ this . _testTimerBasedFlush ( ) ;
3432 this . _testUnload ( ) ;
3533 this . _testBaseTypeMapping ( ) ;
3634 this . _testSdkStatsMetricFiltering ( ) ;
@@ -45,10 +43,7 @@ export class SdkStatsNotificationCbkTests extends AITestClass {
4543 } ,
4644 lang : "JavaScript" ,
4745 ver : "3.3.6" ,
48- int : 100 , // short interval for testing
49- fnFlush : function ( ) {
50- _self . _flushCalled = true ;
51- }
46+ int : 100 // short interval for testing
5247 } ;
5348
5449 if ( overrides ) {
@@ -309,9 +304,62 @@ export class SdkStatsNotificationCbkTests extends AITestClass {
309304 } ) ;
310305 }
311306
307+ private _testTimerBasedFlush ( ) {
308+ this . testCase ( {
309+ name : "SdkStatsNotifCbk: metrics are automatically flushed after the configured timer interval" ,
310+ useFakeTimers : true ,
311+ test : ( ) => {
312+ let listener = this . _createListener ( ) ; // interval = 100ms
313+
314+ // Queue some events — this starts the internal timer
315+ listener . eventsSent ( [
316+ this . _makeItem ( "EventData" ) ,
317+ this . _makeItem ( "ExceptionData" )
318+ ] ) ;
319+
320+ // No flush called yet — nothing should have been emitted
321+ Assert . equal ( 0 , this . _trackedItems . length , "No metrics should be emitted before timer fires" ) ;
322+
323+ // Advance the clock past the configured interval (100ms)
324+ this . clock . tick ( 101 ) ;
325+
326+ // The timer should have fired and flushed the accumulated counts
327+ Assert . equal ( 2 , this . _trackedItems . length , "Metrics should be emitted after timer fires" ) ;
328+
329+ let names = this . _trackedItems . map ( function ( item ) { return item . name ; } ) ;
330+ Assert . ok ( names . indexOf ( "Item_Success_Count" ) >= 0 , "Should contain Item_Success_Count" ) ;
331+ }
332+ } ) ;
333+
334+ this . testCase ( {
335+ name : "SdkStatsNotifCbk: timer resets after flush and accumulates next interval independently" ,
336+ useFakeTimers : true ,
337+ test : ( ) => {
338+ let listener = this . _createListener ( ) ; // interval = 100ms
339+
340+ // First interval
341+ listener . eventsSent ( [ this . _makeItem ( "EventData" ) ] ) ;
342+ this . clock . tick ( 101 ) ;
343+
344+ Assert . equal ( 1 , this . _trackedItems . length , "First interval should emit 1 metric" ) ;
345+ Assert . equal ( 1 , this . _trackedItems [ 0 ] . baseData . average , "First interval count should be 1" ) ;
346+
347+ // Reset tracking for second interval
348+ this . _trackedItems = [ ] ;
349+
350+ // Second interval — new events
351+ listener . eventsSent ( [ this . _makeItem ( "EventData" ) , this . _makeItem ( "EventData" ) ] ) ;
352+ this . clock . tick ( 101 ) ;
353+
354+ Assert . equal ( 1 , this . _trackedItems . length , "Second interval should emit 1 metric" ) ;
355+ Assert . equal ( 2 , this . _trackedItems [ 0 ] . baseData . average , "Second interval count should be 2" ) ;
356+ }
357+ } ) ;
358+ }
359+
312360 private _testUnload ( ) {
313361 this . testCase ( {
314- name : "SdkStatsNotifCbk: unload flushes remaining counts and calls fnFlush " ,
362+ name : "SdkStatsNotifCbk: unload flushes remaining counts" ,
315363 test : ( ) => {
316364 let listener = this . _createListener ( ) ;
317365
@@ -321,20 +369,18 @@ export class SdkStatsNotificationCbkTests extends AITestClass {
321369 this . _listener = null ;
322370
323371 Assert . equal ( 1 , this . _trackedItems . length , "Should flush remaining counts on unload" ) ;
324- Assert . ok ( this . _flushCalled , "fnFlush should be called on unload" ) ;
325372 }
326373 } ) ;
327374
328375 this . testCase ( {
329- name : "SdkStatsNotifCbk: unload with no pending data still calls fnFlush " ,
376+ name : "SdkStatsNotifCbk: unload with no pending data emits nothing " ,
330377 test : ( ) => {
331378 let listener = this . _createListener ( ) ;
332379
333380 listener . unload ( ) ;
334381 this . _listener = null ;
335382
336383 Assert . equal ( 0 , this . _trackedItems . length , "Should not emit any metrics when no data" ) ;
337- Assert . ok ( this . _flushCalled , "fnFlush should still be called" ) ;
338384 }
339385 } ) ;
340386 }
0 commit comments