Skip to content

Commit 01fa0b0

Browse files
committed
fix tests
1 parent e1483fa commit 01fa0b0

File tree

1 file changed

+50
-4
lines changed

1 file changed

+50
-4
lines changed

firebase-perf/src/test/java/com/google/firebase/perf/session/gauges/GaugeManagerTest.java

Lines changed: 50 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -403,6 +403,44 @@ public void testGaugeCounterIsDecrementedWhenLogged() {
403403
assertThat(GaugeCounter.count()).isEqualTo(priorGaugeCounter);
404404
}
405405

406+
@Test
407+
public void testDuplicateGaugeLoggingIsAvoided() {
408+
int priorGaugeCounter = GaugeCounter.count();
409+
PerfSession fakeSession = createTestSession(1);
410+
testGaugeManager.setApplicationProcessState(ApplicationProcessState.FOREGROUND);
411+
testGaugeManager.startCollectingGauges(fakeSession);
412+
GaugeCounter.setGaugeManager(testGaugeManager);
413+
414+
// There's no job to log the gauges.
415+
assertThat(fakeScheduledExecutorService.isEmpty()).isTrue();
416+
417+
generateMetricsAndIncrementCounter(MAX_GAUGE_COUNTER_BEFORE_LOGGING - 20);
418+
419+
// There's still no job to log the gauges.
420+
assertThat(fakeScheduledExecutorService.isEmpty()).isTrue();
421+
422+
generateMetricsAndIncrementCounter(MAX_GAUGE_COUNTER_BEFORE_LOGGING);
423+
424+
assertThat(fakeScheduledExecutorService.isEmpty()).isFalse();
425+
assertThat(fakeScheduledExecutorService.getDelayToNextTask(TimeUnit.MILLISECONDS))
426+
.isEqualTo(TIME_TO_WAIT_BEFORE_FLUSHING_GAUGES_QUEUE_MS);
427+
428+
fakeScheduledExecutorService.simulateSleepExecutingAtMostOneTask();
429+
assertThat(fakeScheduledExecutorService.isEmpty()).isTrue();
430+
431+
GaugeMetric recordedGaugeMetric =
432+
getLastRecordedGaugeMetric(ApplicationProcessState.FOREGROUND);
433+
434+
// It flushes all the metrics in the ConcurrentLinkedQueues that were added.
435+
int recordedGaugeMetricsCount =
436+
recordedGaugeMetric.getAndroidMemoryReadingsCount()
437+
+ recordedGaugeMetric.getCpuMetricReadingsCount();
438+
assertThat(recordedGaugeMetricsCount).isEqualTo(2 * MAX_GAUGE_COUNTER_BEFORE_LOGGING - 20);
439+
440+
assertThat(recordedGaugeMetric.getSessionId()).isEqualTo(testSessionId(1));
441+
assertThat(GaugeCounter.count()).isEqualTo(priorGaugeCounter);
442+
}
443+
406444
@Test
407445
public void testUpdateAppStateHandlesMultipleAppStates() {
408446
PerfSession fakeSession = createTestSession(1);
@@ -564,7 +602,7 @@ public void testLogGaugeMetadataSendDataToTransport() {
564602
testGaugeManager.logGaugeMetadata(testSessionId(1));
565603

566604
GaugeMetric recordedGaugeMetric =
567-
getLastRecordedGaugeMetric(ApplicationProcessState.FOREGROUND);
605+
getLastRecordedGaugeMetric(ApplicationProcessState.APPLICATION_PROCESS_STATE_UNKNOWN);
568606
GaugeMetadata recordedGaugeMetadata = recordedGaugeMetric.getGaugeMetadata();
569607

570608
assertThat(recordedGaugeMetric.getSessionId()).isEqualTo(testSessionId(1));
@@ -609,7 +647,7 @@ public void testLogGaugeMetadataLogsAfterApplicationContextIsSet() {
609647
assertThat(testGaugeManager.logGaugeMetadata(testSessionId(1))).isTrue();
610648

611649
GaugeMetric recordedGaugeMetric =
612-
getLastRecordedGaugeMetric(ApplicationProcessState.FOREGROUND);
650+
getLastRecordedGaugeMetric(ApplicationProcessState.APPLICATION_PROCESS_STATE_UNKNOWN);
613651
GaugeMetadata recordedGaugeMetadata = recordedGaugeMetric.getGaugeMetadata();
614652

615653
assertThat(recordedGaugeMetric.getSessionId()).isEqualTo(testSessionId(1));
@@ -675,8 +713,16 @@ private AndroidMemoryReading createFakeAndroidMetricReading(int currentUsedAppJa
675713
private GaugeMetric getLastRecordedGaugeMetric(
676714
ApplicationProcessState expectedApplicationProcessState) {
677715
ArgumentCaptor<GaugeMetric> argMetric = ArgumentCaptor.forClass(GaugeMetric.class);
678-
verify(mockTransportManager, times(1))
679-
.log(argMetric.capture(), eq(expectedApplicationProcessState));
716+
717+
// TODO(b/394127311): Revisit transportManager.log method which is only being called in unit
718+
// tests.
719+
if (expectedApplicationProcessState
720+
== ApplicationProcessState.APPLICATION_PROCESS_STATE_UNKNOWN) {
721+
verify(mockTransportManager, times(1)).log(argMetric.capture());
722+
} else {
723+
verify(mockTransportManager, times(1))
724+
.log(argMetric.capture(), eq(expectedApplicationProcessState));
725+
}
680726
reset(mockTransportManager);
681727
// Required after resetting the mock. By default we assume that Transport is initialized.
682728
when(mockTransportManager.isInitialized()).thenReturn(true);

0 commit comments

Comments
 (0)