@@ -366,20 +366,17 @@ public void testGaugeCounterStartsAJobToConsumeTheGeneratedMetrics() {
366366 }
367367
368368 @ Test
369- public void testUpdateAppStateFlushesMetricsInTheCurrentAppState () {
369+ public void testUpdateAppStateHandlesMultipleAppStates () {
370370 PerfSession fakeSession = createTestSession (1 );
371371 fakeSession .setGaugeAndEventCollectionEnabled (true );
372372 testGaugeManager .setApplicationProcessState (ApplicationProcessState .FOREGROUND );
373373 testGaugeManager .startCollectingGauges (fakeSession );
374374 GaugeCounter .INSTANCE .setGaugeManager (testGaugeManager );
375375
376- // There's no job to log the gauges.
377- assertThat (fakeScheduledExecutorService .isEmpty ()).isTrue ();
378-
379376 // Generate metrics that don't exceed the GaugeCounter.MAX_COUNT.
380377 generateMetricsAndIncrementCounter (10 );
381378
382- // There's still no job to log the gauges.
379+ // There's no job to log the gauges.
383380 assertThat (fakeScheduledExecutorService .isEmpty ()).isTrue ();
384381
385382 testGaugeManager .onUpdateAppState (ApplicationProcessState .BACKGROUND );
@@ -389,9 +386,10 @@ public void testUpdateAppStateFlushesMetricsInTheCurrentAppState() {
389386 .isEqualTo (TIME_TO_WAIT_BEFORE_FLUSHING_GAUGES_QUEUE_MS );
390387
391388 fakeScheduledExecutorService .simulateSleepExecutingAtMostOneTask ();
389+ shadowOf (Looper .getMainLooper ()).idle ();
392390
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 );
395393
396394 GaugeMetric recordedGaugeMetric =
397395 getLastRecordedGaugeMetric (ApplicationProcessState .FOREGROUND );
@@ -403,6 +401,73 @@ public void testUpdateAppStateFlushesMetricsInTheCurrentAppState() {
403401 assertThat (recordedGaugeMetricsCount ).isEqualTo (10 );
404402
405403 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 ));
406471 }
407472
408473 @ Test
0 commit comments