Skip to content

Commit fa39f77

Browse files
committed
More tests
1 parent c22a3eb commit fa39f77

File tree

2 files changed

+11
-182
lines changed

2 files changed

+11
-182
lines changed

firebase-perf/src/main/java/com/google/firebase/perf/session/FirebasePerformanceSessionSubscriber.kt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,7 @@ class FirebasePerformanceSessionSubscriber(override val isDataCollectionEnabled:
4747

4848
val updatedSession = PerfSession.createWithId(UUID.randomUUID().toString())
4949
updatedSession.setAQSId(sessionDetails)
50-
sessionManager.updatePerfSession(updatedSession)
5150
gaugeManager.logGaugeMetadata(updatedSession.aqsSessionId(), ApplicationProcessState.FOREGROUND)
51+
sessionManager.updatePerfSession(updatedSession)
5252
}
5353
}

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

Lines changed: 10 additions & 181 deletions
Original file line numberDiff line numberDiff line change
@@ -140,6 +140,13 @@ public void testStartCollectingGaugesStartsCollectingMetrics() {
140140
ArgumentMatchers.nullable(Timer.class));
141141
}
142142

143+
@Test
144+
public void testStartCollectingGaugesDoesNotLogMetrics() {
145+
PerfSession fakeSession = createTestPerfSession();
146+
testGaugeManager.startCollectingGaugeMetrics(fakeSession);
147+
assertThat(fakeScheduledExecutorService.isEmpty()).isTrue();
148+
}
149+
143150
@Test
144151
public void
145152
stopCollectingCPUMetric_invalidCPUCaptureFrequency_OtherMetricsWithValidFrequencyInForeground() {
@@ -355,7 +362,7 @@ public void startCollectingGauges_validGaugeCollectionFrequency_appInForeground(
355362
@Test
356363
public void testStartCollectingGaugesDoesNotStartAJobToConsumeTheGeneratedMetrics() {
357364
PerfSession fakeSession = createTestPerfSession();
358-
testGaugeManager.setApplicationProcessState(ApplicationProcessState.BACKGROUND);
365+
testGaugeManager.setApplicationProcessState(ApplicationProcessState.FOREGROUND);
359366
testGaugeManager.startCollectingGaugeMetrics(fakeSession);
360367

361368
assertThat(fakeScheduledExecutorService.isEmpty()).isTrue();
@@ -373,7 +380,7 @@ public void testStartCollectingGaugesDoesNotStartAJobToConsumeTheGeneratedMetric
373380
fakeMemoryGaugeCollector.memoryMetricReadings.add(fakeMemoryMetricReading2);
374381

375382
fakeScheduledExecutorService.simulateSleepExecutingAtMostOneTask();
376-
assertThatNothingWasLoggedToTransport(ApplicationProcessState.BACKGROUND);
383+
assertThatNothingWasLoggedToTransport(ApplicationProcessState.FOREGROUND);
377384
}
378385

379386
@Test
@@ -450,184 +457,6 @@ public void testGaugeManagerDoesNotClearTheQueueUnlessUpdated() {
450457
assertThat(fakeMemoryGaugeCollector.memoryMetricReadings).isEmpty();
451458
}
452459

453-
@Test
454-
public void testStartingGaugeManagerWithNewSessionIdButSameAppState() {
455-
PerfSession fakeSession1 = createTestPerfSession();
456-
457-
// Start collecting Gauges.
458-
testGaugeManager.setApplicationProcessState(ApplicationProcessState.BACKGROUND);
459-
testGaugeManager.startCollectingGaugeMetrics(fakeSession1);
460-
CpuMetricReading fakeCpuMetricReading1 = createFakeCpuMetricReading(200, 100);
461-
fakeCpuGaugeCollector.cpuMetricReadings.add(fakeCpuMetricReading1);
462-
AndroidMemoryReading fakeMemoryMetricReading1 =
463-
createFakeAndroidMetricReading(/* currentUsedAppJavaHeapMemoryKb= */ 1234);
464-
fakeMemoryGaugeCollector.memoryMetricReadings.add(fakeMemoryMetricReading1);
465-
466-
fakeScheduledExecutorService.simulateSleepExecutingAtMostOneTask();
467-
GaugeMetric recordedGaugeMetric1 =
468-
getLastRecordedGaugeMetric(ApplicationProcessState.BACKGROUND, 1);
469-
assertThatCpuGaugeMetricWasSentToTransport(
470-
fakeSession1.sessionId(), recordedGaugeMetric1, fakeCpuMetricReading1);
471-
assertThatMemoryGaugeMetricWasSentToTransport(
472-
fakeSession1.sessionId(), recordedGaugeMetric1, fakeMemoryMetricReading1);
473-
474-
// One Cpu and Memory metric was added when the gauge was collecting for the previous sessionId.
475-
CpuMetricReading fakeCpuMetricReading2 = createFakeCpuMetricReading(400, 500);
476-
fakeCpuGaugeCollector.cpuMetricReadings.add(fakeCpuMetricReading2);
477-
AndroidMemoryReading fakeMemoryMetricReading2 =
478-
createFakeAndroidMetricReading(/* currentUsedAppJavaHeapMemoryKb= */ 2345);
479-
fakeMemoryGaugeCollector.memoryMetricReadings.add(fakeMemoryMetricReading2);
480-
481-
PerfSession fakeSession2 = createTestPerfSession();
482-
483-
// Start collecting gauges for new session, but same app state.
484-
testGaugeManager.setApplicationProcessState(ApplicationProcessState.BACKGROUND);
485-
testGaugeManager.startCollectingGaugeMetrics(fakeSession2);
486-
487-
// The next sweep conducted by GaugeManager still associates metrics to old sessionId and state.
488-
fakeScheduledExecutorService.simulateSleepExecutingAtMostOneTask();
489-
GaugeMetric recordedGaugeMetric2 =
490-
getLastRecordedGaugeMetric(ApplicationProcessState.BACKGROUND, 1);
491-
assertThatCpuGaugeMetricWasSentToTransport(
492-
fakeSession1.sessionId(), recordedGaugeMetric2, fakeCpuMetricReading2);
493-
assertThatMemoryGaugeMetricWasSentToTransport(
494-
fakeSession1.sessionId(), recordedGaugeMetric2, fakeMemoryMetricReading2);
495-
496-
// Collect some more Cpu and Memory metrics and verify that they're associated with new
497-
// sessionId and state.
498-
CpuMetricReading fakeCpuMetricReading3 = createFakeCpuMetricReading(500, 600);
499-
fakeCpuGaugeCollector.cpuMetricReadings.add(fakeCpuMetricReading3);
500-
AndroidMemoryReading fakeMemoryMetricReading3 =
501-
createFakeAndroidMetricReading(/* currentUsedAppJavaHeapMemoryKb= */ 3456);
502-
fakeMemoryGaugeCollector.memoryMetricReadings.add(fakeMemoryMetricReading3);
503-
504-
fakeScheduledExecutorService.simulateSleepExecutingAtMostOneTask();
505-
GaugeMetric recordedGaugeMetric3 =
506-
getLastRecordedGaugeMetric(ApplicationProcessState.BACKGROUND, 1);
507-
assertThatCpuGaugeMetricWasSentToTransport(
508-
fakeSession2.sessionId(), recordedGaugeMetric3, fakeCpuMetricReading3);
509-
assertThatMemoryGaugeMetricWasSentToTransport(
510-
fakeSession2.sessionId(), recordedGaugeMetric3, fakeMemoryMetricReading3);
511-
}
512-
513-
@Test
514-
public void testStartGaugeManagerWithSameSessionIdButDifferentAppState() {
515-
PerfSession fakeSession = createTestPerfSession();
516-
517-
// Start collecting Gauges.
518-
testGaugeManager.setApplicationProcessState(ApplicationProcessState.BACKGROUND);
519-
testGaugeManager.startCollectingGaugeMetrics(fakeSession);
520-
CpuMetricReading fakeCpuMetricReading1 = createFakeCpuMetricReading(200, 100);
521-
fakeCpuGaugeCollector.cpuMetricReadings.add(fakeCpuMetricReading1);
522-
AndroidMemoryReading fakeMemoryMetricReading1 =
523-
createFakeAndroidMetricReading(/* currentUsedAppJavaHeapMemoryKb= */ 1234);
524-
fakeMemoryGaugeCollector.memoryMetricReadings.add(fakeMemoryMetricReading1);
525-
526-
fakeScheduledExecutorService.simulateSleepExecutingAtMostOneTask();
527-
GaugeMetric recordedGaugeMetric1 =
528-
getLastRecordedGaugeMetric(ApplicationProcessState.BACKGROUND, 1);
529-
assertThatCpuGaugeMetricWasSentToTransport(
530-
fakeSession.sessionId(), recordedGaugeMetric1, fakeCpuMetricReading1);
531-
assertThatMemoryGaugeMetricWasSentToTransport(
532-
fakeSession.sessionId(), recordedGaugeMetric1, fakeMemoryMetricReading1);
533-
534-
// One Cpu and Memory metric was added when the gauge was collecting for the previous sessionId.
535-
CpuMetricReading fakeCpuMetricReading2 = createFakeCpuMetricReading(400, 500);
536-
fakeCpuGaugeCollector.cpuMetricReadings.add(fakeCpuMetricReading2);
537-
AndroidMemoryReading fakeMemoryMetricReading2 =
538-
createFakeAndroidMetricReading(/* currentUsedAppJavaHeapMemoryKb= */ 2345);
539-
fakeMemoryGaugeCollector.memoryMetricReadings.add(fakeMemoryMetricReading2);
540-
541-
// Start collecting gauges for same session, but new app state
542-
testGaugeManager.setApplicationProcessState(ApplicationProcessState.FOREGROUND);
543-
testGaugeManager.startCollectingGaugeMetrics(fakeSession);
544-
545-
// The next sweep conducted by GaugeManager still associates metrics to old sessionId and state.
546-
fakeScheduledExecutorService.simulateSleepExecutingAtMostOneTask();
547-
GaugeMetric recordedGaugeMetric2 =
548-
getLastRecordedGaugeMetric(ApplicationProcessState.BACKGROUND, 1);
549-
assertThatCpuGaugeMetricWasSentToTransport(
550-
fakeSession.sessionId(), recordedGaugeMetric2, fakeCpuMetricReading2);
551-
assertThatMemoryGaugeMetricWasSentToTransport(
552-
fakeSession.sessionId(), recordedGaugeMetric2, fakeMemoryMetricReading2);
553-
554-
// Collect some more Cpu and Memory metrics and verify that they're associated with new
555-
// sessionId and state.
556-
CpuMetricReading fakeCpuMetricReading3 = createFakeCpuMetricReading(500, 600);
557-
fakeCpuGaugeCollector.cpuMetricReadings.add(fakeCpuMetricReading3);
558-
AndroidMemoryReading fakeMemoryMetricReading3 =
559-
createFakeAndroidMetricReading(/* currentUsedAppJavaHeapMemoryKb= */ 3456);
560-
fakeMemoryGaugeCollector.memoryMetricReadings.add(fakeMemoryMetricReading3);
561-
562-
fakeScheduledExecutorService.simulateSleepExecutingAtMostOneTask();
563-
GaugeMetric recordedGaugeMetric3 =
564-
getLastRecordedGaugeMetric(ApplicationProcessState.FOREGROUND, 1);
565-
assertThatCpuGaugeMetricWasSentToTransport(
566-
fakeSession.sessionId(), recordedGaugeMetric3, fakeCpuMetricReading3);
567-
assertThatMemoryGaugeMetricWasSentToTransport(
568-
fakeSession.sessionId(), recordedGaugeMetric3, fakeMemoryMetricReading3);
569-
}
570-
571-
@Test
572-
public void testStartGaugeManagerWithNewSessionIdAndNewAppState() {
573-
PerfSession fakeSession1 = createTestPerfSession();
574-
575-
// Start collecting Gauges.
576-
testGaugeManager.startCollectingGaugeMetrics(fakeSession1);
577-
testGaugeManager.updateGaugeCollection(ApplicationProcessState.BACKGROUND);
578-
CpuMetricReading fakeCpuMetricReading1 = createFakeCpuMetricReading(200, 100);
579-
fakeCpuGaugeCollector.cpuMetricReadings.add(fakeCpuMetricReading1);
580-
AndroidMemoryReading fakeMemoryMetricReading1 =
581-
createFakeAndroidMetricReading(/* currentUsedAppJavaHeapMemoryKb= */ 1234);
582-
fakeMemoryGaugeCollector.memoryMetricReadings.add(fakeMemoryMetricReading1);
583-
584-
fakeScheduledExecutorService.simulateSleepExecutingAtMostOneTask();
585-
GaugeMetric recordedGaugeMetric1 =
586-
getLastRecordedGaugeMetric(ApplicationProcessState.BACKGROUND, 1);
587-
assertThatCpuGaugeMetricWasSentToTransport(
588-
fakeSession1.sessionId(), recordedGaugeMetric1, fakeCpuMetricReading1);
589-
assertThatMemoryGaugeMetricWasSentToTransport(
590-
fakeSession1.sessionId(), recordedGaugeMetric1, fakeMemoryMetricReading1);
591-
592-
// One Cpu and Memory metric was added when the gauge was collecting for the previous sessionId.
593-
CpuMetricReading fakeCpuMetricReading2 = createFakeCpuMetricReading(400, 500);
594-
fakeCpuGaugeCollector.cpuMetricReadings.add(fakeCpuMetricReading2);
595-
AndroidMemoryReading fakeMemoryMetricReading2 =
596-
createFakeAndroidMetricReading(/* currentUsedAppJavaHeapMemoryKb= */ 2345);
597-
fakeMemoryGaugeCollector.memoryMetricReadings.add(fakeMemoryMetricReading2);
598-
599-
PerfSession fakeSession2 = createTestPerfSession();
600-
601-
// Start collecting gauges for new session and new app state
602-
testGaugeManager.startCollectingGaugeMetrics(fakeSession2);
603-
testGaugeManager.updateGaugeCollection(ApplicationProcessState.FOREGROUND);
604-
605-
// The next sweep conducted by GaugeManager still associates metrics to old sessionId and state.
606-
fakeScheduledExecutorService.simulateSleepExecutingAtMostOneTask();
607-
GaugeMetric recordedGaugeMetric2 =
608-
getLastRecordedGaugeMetric(ApplicationProcessState.BACKGROUND, 1);
609-
assertThatCpuGaugeMetricWasSentToTransport(
610-
fakeSession1.sessionId(), recordedGaugeMetric2, fakeCpuMetricReading2);
611-
assertThatMemoryGaugeMetricWasSentToTransport(
612-
fakeSession1.sessionId(), recordedGaugeMetric2, fakeMemoryMetricReading2);
613-
614-
// Collect some more Cpu and Memory metrics and verify that they're associated with new
615-
// sessionId and state.
616-
CpuMetricReading fakeCpuMetricReading3 = createFakeCpuMetricReading(500, 600);
617-
fakeCpuGaugeCollector.cpuMetricReadings.add(fakeCpuMetricReading3);
618-
AndroidMemoryReading fakeMemoryMetricReading3 =
619-
createFakeAndroidMetricReading(/* currentUsedAppJavaHeapMemoryKb= */ 3456);
620-
fakeMemoryGaugeCollector.memoryMetricReadings.add(fakeMemoryMetricReading3);
621-
622-
fakeScheduledExecutorService.simulateSleepExecutingAtMostOneTask();
623-
GaugeMetric recordedGaugeMetric3 =
624-
getLastRecordedGaugeMetric(ApplicationProcessState.FOREGROUND, 1);
625-
assertThatCpuGaugeMetricWasSentToTransport(
626-
fakeSession2.sessionId(), recordedGaugeMetric3, fakeCpuMetricReading3);
627-
assertThatMemoryGaugeMetricWasSentToTransport(
628-
fakeSession2.sessionId(), recordedGaugeMetric3, fakeMemoryMetricReading3);
629-
}
630-
631460
@Test
632461
public void testLogGaugeMetadataSendDataToTransport() {
633462
when(fakeGaugeMetadataManager.getDeviceRamSizeKb()).thenReturn(2000);
@@ -651,7 +480,7 @@ public void testLogGaugeMetadataSendDataToTransport() {
651480
}
652481

653482
@Test
654-
public void testLogGaugeMetadataDoesntLogWhenGaugeMetadataManagerNotAvailable() {
483+
public void testLogGaugeMetadataDoesNotLogWhenGaugeMetadataManagerNotAvailable() {
655484

656485
testGaugeManager =
657486
new GaugeManager(

0 commit comments

Comments
 (0)