Skip to content

Commit 4446dbd

Browse files
committed
Fix flaky tests
1 parent 3fb01a6 commit 4446dbd

File tree

1 file changed

+59
-54
lines changed

1 file changed

+59
-54
lines changed

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

Lines changed: 59 additions & 54 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,7 @@
4545
import com.google.testing.timing.FakeScheduledExecutorService;
4646
import java.util.Random;
4747
import java.util.concurrent.TimeUnit;
48+
import org.junit.After;
4849
import org.junit.Before;
4950
import org.junit.Test;
5051
import org.junit.runner.RunWith;
@@ -126,6 +127,11 @@ public void setUp() {
126127
new Lazy<>(() -> fakeMemoryGaugeCollector));
127128
}
128129

130+
@After
131+
public void tearDown() {
132+
shadowOf(Looper.getMainLooper()).idle();
133+
}
134+
129135
@Test
130136
public void testStartCollectingGaugesStartsCollectingMetricsDefault() {
131137
PerfSession fakeSession = createTestSession(1);
@@ -347,7 +353,6 @@ public void testGaugeCounterStartsAJobToConsumeTheGeneratedMetrics() {
347353
.isEqualTo(TIME_TO_WAIT_BEFORE_FLUSHING_GAUGES_QUEUE_MS);
348354

349355
fakeScheduledExecutorService.simulateSleepExecutingAtMostOneTask();
350-
shadowOf(Looper.getMainLooper()).idle();
351356

352357
// Generate additional metrics, but doesn't start logging them as it hasn't met the threshold.
353358
generateMetricsAndIncrementCounter(5);
@@ -418,59 +423,59 @@ public void testUpdateAppStateHandlesMultipleAppStates() {
418423
assertThat(recordedGaugeMetric.getSessionId()).isEqualTo(testSessionId(1));
419424
}
420425

421-
// @Test
422-
// public void testGaugeManagerHandlesMultipleSessionIds() {
423-
// PerfSession fakeSession = createTestSession(1);
424-
// fakeSession.setGaugeAndEventCollectionEnabled(true);
425-
// testGaugeManager.setApplicationProcessState(ApplicationProcessState.BACKGROUND);
426-
// testGaugeManager.startCollectingGauges(fakeSession);
427-
// GaugeCounter.INSTANCE.setGaugeManager(testGaugeManager);
428-
//
429-
// // Generate metrics that don't exceed the GaugeCounter.MAX_COUNT.
430-
// generateMetricsAndIncrementCounter(10);
431-
//
432-
// PerfSession updatedPerfSession = createTestSession(2);
433-
// updatedPerfSession.setGaugeAndEventCollectionEnabled(true);
434-
//
435-
// // A new session and updated app state.
436-
// testGaugeManager.startCollectingGauges(updatedPerfSession);
437-
// testGaugeManager.setApplicationProcessState(ApplicationProcessState.FOREGROUND);
438-
//
439-
// assertThat(fakeScheduledExecutorService.isEmpty()).isFalse();
440-
// assertThat(fakeScheduledExecutorService.getDelayToNextTask(TimeUnit.MILLISECONDS))
441-
// .isEqualTo(TIME_TO_WAIT_BEFORE_FLUSHING_GAUGES_QUEUE_MS);
442-
//
443-
// fakeScheduledExecutorService.simulateSleepExecutingAtMostOneTask();
444-
// shadowOf(Looper.getMainLooper()).idle();
445-
//
446-
// // Generate metrics for the new session.
447-
// generateMetricsAndIncrementCounter(26);
448-
//
449-
// GaugeMetric recordedGaugeMetric =
450-
// getLastRecordedGaugeMetric(ApplicationProcessState.BACKGROUND);
451-
//
452-
// // It flushes all metrics in the ConcurrentLinkedQueues.
453-
// int recordedGaugeMetricsCount =
454-
// recordedGaugeMetric.getAndroidMemoryReadingsCount()
455-
// + recordedGaugeMetric.getCpuMetricReadingsCount();
456-
// assertThat(recordedGaugeMetricsCount).isEqualTo(10);
457-
//
458-
// assertThat(recordedGaugeMetric.getSessionId()).isEqualTo(testSessionId(1));
459-
//
460-
// // Simulate gauges collected in the new app state.
461-
// fakeScheduledExecutorService.simulateSleepExecutingAtMostOneTask();
462-
// shadowOf(Looper.getMainLooper()).idle();
463-
//
464-
// recordedGaugeMetric = getLastRecordedGaugeMetric(ApplicationProcessState.BACKGROUND);
465-
//
466-
// // Verify the metrics in the new app state.
467-
// recordedGaugeMetricsCount =
468-
// recordedGaugeMetric.getAndroidMemoryReadingsCount()
469-
// + recordedGaugeMetric.getCpuMetricReadingsCount();
470-
// assertThat(recordedGaugeMetricsCount).isEqualTo(26);
471-
//
472-
// assertThat(recordedGaugeMetric.getSessionId()).isEqualTo(testSessionId(2));
473-
// }
426+
@Test
427+
public void testGaugeManagerHandlesMultipleSessionIds() {
428+
PerfSession fakeSession = createTestSession(1);
429+
fakeSession.setGaugeAndEventCollectionEnabled(true);
430+
testGaugeManager.setApplicationProcessState(ApplicationProcessState.BACKGROUND);
431+
testGaugeManager.startCollectingGauges(fakeSession);
432+
GaugeCounter.INSTANCE.setGaugeManager(testGaugeManager);
433+
434+
// Generate metrics that don't exceed the GaugeCounter.MAX_COUNT.
435+
generateMetricsAndIncrementCounter(10);
436+
437+
PerfSession updatedPerfSession = createTestSession(2);
438+
updatedPerfSession.setGaugeAndEventCollectionEnabled(true);
439+
440+
// A new session and updated app state.
441+
testGaugeManager.startCollectingGauges(updatedPerfSession);
442+
testGaugeManager.setApplicationProcessState(ApplicationProcessState.FOREGROUND);
443+
444+
assertThat(fakeScheduledExecutorService.isEmpty()).isFalse();
445+
assertThat(fakeScheduledExecutorService.getDelayToNextTask(TimeUnit.MILLISECONDS))
446+
.isEqualTo(TIME_TO_WAIT_BEFORE_FLUSHING_GAUGES_QUEUE_MS);
447+
448+
fakeScheduledExecutorService.simulateSleepExecutingAtMostOneTask();
449+
shadowOf(Looper.getMainLooper()).idle();
450+
451+
// Generate metrics for the new session.
452+
generateMetricsAndIncrementCounter(26);
453+
454+
GaugeMetric recordedGaugeMetric =
455+
getLastRecordedGaugeMetric(ApplicationProcessState.BACKGROUND);
456+
457+
// It flushes all metrics in the ConcurrentLinkedQueues.
458+
int recordedGaugeMetricsCount =
459+
recordedGaugeMetric.getAndroidMemoryReadingsCount()
460+
+ recordedGaugeMetric.getCpuMetricReadingsCount();
461+
assertThat(recordedGaugeMetricsCount).isEqualTo(10);
462+
463+
assertThat(recordedGaugeMetric.getSessionId()).isEqualTo(testSessionId(1));
464+
465+
// Simulate gauges collected in the new app state.
466+
fakeScheduledExecutorService.simulateSleepExecutingAtMostOneTask();
467+
shadowOf(Looper.getMainLooper()).idle();
468+
469+
recordedGaugeMetric = getLastRecordedGaugeMetric(ApplicationProcessState.FOREGROUND);
470+
471+
// Verify the metrics in the new app state.
472+
recordedGaugeMetricsCount =
473+
recordedGaugeMetric.getAndroidMemoryReadingsCount()
474+
+ recordedGaugeMetric.getCpuMetricReadingsCount();
475+
assertThat(recordedGaugeMetricsCount).isEqualTo(26);
476+
477+
assertThat(recordedGaugeMetric.getSessionId()).isEqualTo(testSessionId(2));
478+
}
474479

475480
@Test
476481
public void testStopCollectingGaugesStopsCollectingAllGaugeMetrics() {

0 commit comments

Comments
 (0)