@@ -366,20 +366,17 @@ public void testGaugeCounterStartsAJobToConsumeTheGeneratedMetrics() {
366
366
}
367
367
368
368
@ Test
369
- public void testUpdateAppStateFlushesMetricsInTheCurrentAppState () {
369
+ public void testUpdateAppStateHandlesMultipleAppStates () {
370
370
PerfSession fakeSession = createTestSession (1 );
371
371
fakeSession .setGaugeAndEventCollectionEnabled (true );
372
372
testGaugeManager .setApplicationProcessState (ApplicationProcessState .FOREGROUND );
373
373
testGaugeManager .startCollectingGauges (fakeSession );
374
374
GaugeCounter .INSTANCE .setGaugeManager (testGaugeManager );
375
375
376
- // There's no job to log the gauges.
377
- assertThat (fakeScheduledExecutorService .isEmpty ()).isTrue ();
378
-
379
376
// Generate metrics that don't exceed the GaugeCounter.MAX_COUNT.
380
377
generateMetricsAndIncrementCounter (10 );
381
378
382
- // There's still no job to log the gauges.
379
+ // There's no job to log the gauges.
383
380
assertThat (fakeScheduledExecutorService .isEmpty ()).isTrue ();
384
381
385
382
testGaugeManager .onUpdateAppState (ApplicationProcessState .BACKGROUND );
@@ -389,9 +386,10 @@ public void testUpdateAppStateFlushesMetricsInTheCurrentAppState() {
389
386
.isEqualTo (TIME_TO_WAIT_BEFORE_FLUSHING_GAUGES_QUEUE_MS );
390
387
391
388
fakeScheduledExecutorService .simulateSleepExecutingAtMostOneTask ();
389
+ shadowOf (Looper .getMainLooper ()).idle ();
392
390
393
- // Generate additional metrics that shouldn't be included in the flush .
394
- generateMetricsAndIncrementCounter (5 );
391
+ // Generate additional metrics in the new app state .
392
+ generateMetricsAndIncrementCounter (26 );
395
393
396
394
GaugeMetric recordedGaugeMetric =
397
395
getLastRecordedGaugeMetric (ApplicationProcessState .FOREGROUND );
@@ -403,6 +401,73 @@ public void testUpdateAppStateFlushesMetricsInTheCurrentAppState() {
403
401
assertThat (recordedGaugeMetricsCount ).isEqualTo (10 );
404
402
405
403
assertThat (recordedGaugeMetric .getSessionId ()).isEqualTo (testSessionId (1 ));
404
+
405
+ // Simulate gauges collected in the new app state.
406
+ fakeScheduledExecutorService .simulateSleepExecutingAtMostOneTask ();
407
+ shadowOf (Looper .getMainLooper ()).idle ();
408
+
409
+ recordedGaugeMetric = getLastRecordedGaugeMetric (ApplicationProcessState .BACKGROUND );
410
+
411
+ // Verify the metrics in the new app state.
412
+ recordedGaugeMetricsCount =
413
+ recordedGaugeMetric .getAndroidMemoryReadingsCount ()
414
+ + recordedGaugeMetric .getCpuMetricReadingsCount ();
415
+ assertThat (recordedGaugeMetricsCount ).isEqualTo (26 );
416
+
417
+ assertThat (recordedGaugeMetric .getSessionId ()).isEqualTo (testSessionId (1 ));
418
+ }
419
+
420
+ @ Test
421
+ public void testGaugeManagerHandlesMultipleSessionIds () {
422
+ PerfSession fakeSession = createTestSession (1 );
423
+ fakeSession .setGaugeAndEventCollectionEnabled (true );
424
+ testGaugeManager .setApplicationProcessState (ApplicationProcessState .BACKGROUND );
425
+ testGaugeManager .startCollectingGauges (fakeSession );
426
+ GaugeCounter .INSTANCE .setGaugeManager (testGaugeManager );
427
+
428
+ // Generate metrics that don't exceed the GaugeCounter.MAX_COUNT.
429
+ generateMetricsAndIncrementCounter (10 );
430
+
431
+ PerfSession updatedPerfSession = createTestSession (2 );
432
+ updatedPerfSession .setGaugeAndEventCollectionEnabled (true );
433
+
434
+ // A new session and updated app state.
435
+ testGaugeManager .startCollectingGauges (updatedPerfSession );
436
+
437
+ assertThat (fakeScheduledExecutorService .isEmpty ()).isFalse ();
438
+ assertThat (fakeScheduledExecutorService .getDelayToNextTask (TimeUnit .MILLISECONDS ))
439
+ .isEqualTo (TIME_TO_WAIT_BEFORE_FLUSHING_GAUGES_QUEUE_MS );
440
+
441
+ fakeScheduledExecutorService .simulateSleepExecutingAtMostOneTask ();
442
+ shadowOf (Looper .getMainLooper ()).idle ();
443
+
444
+ // Generate metrics for the new session.
445
+ generateMetricsAndIncrementCounter (26 );
446
+
447
+ GaugeMetric recordedGaugeMetric =
448
+ getLastRecordedGaugeMetric (ApplicationProcessState .BACKGROUND );
449
+
450
+ // It flushes all metrics in the ConcurrentLinkedQueues.
451
+ int recordedGaugeMetricsCount =
452
+ recordedGaugeMetric .getAndroidMemoryReadingsCount ()
453
+ + recordedGaugeMetric .getCpuMetricReadingsCount ();
454
+ assertThat (recordedGaugeMetricsCount ).isEqualTo (10 );
455
+
456
+ assertThat (recordedGaugeMetric .getSessionId ()).isEqualTo (testSessionId (1 ));
457
+
458
+ // Simulate gauges collected in the new app state.
459
+ fakeScheduledExecutorService .simulateSleepExecutingAtMostOneTask ();
460
+ shadowOf (Looper .getMainLooper ()).idle ();
461
+
462
+ recordedGaugeMetric = getLastRecordedGaugeMetric (ApplicationProcessState .BACKGROUND );
463
+
464
+ // Verify the metrics in the new app state.
465
+ recordedGaugeMetricsCount =
466
+ recordedGaugeMetric .getAndroidMemoryReadingsCount ()
467
+ + recordedGaugeMetric .getCpuMetricReadingsCount ();
468
+ assertThat (recordedGaugeMetricsCount ).isEqualTo (26 );
469
+
470
+ assertThat (recordedGaugeMetric .getSessionId ()).isEqualTo (testSessionId (2 ));
406
471
}
407
472
408
473
@ Test
0 commit comments