Skip to content

Commit 68ce5b7

Browse files
leotianlizhanDavid Motsonashvili
authored and
David Motsonashvili
committed
Deflake AppStateMonitorTest (#4294)
* fix flaky test * word smithing
1 parent ce89393 commit 68ce5b7

File tree

1 file changed

+13
-8
lines changed

1 file changed

+13
-8
lines changed

firebase-perf/src/test/java/com/google/firebase/perf/application/AppStateMonitorTest.java

Lines changed: 13 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
package com.google.firebase.perf.application;
1616

1717
import static com.google.common.truth.Truth.assertThat;
18+
import static com.google.firebase.perf.application.AppStateMonitor.AppStateCallback;
1819
import static com.google.firebase.perf.v1.ApplicationProcessState.FOREGROUND_BACKGROUND;
1920
import static org.mockito.ArgumentMatchers.any;
2021
import static org.mockito.ArgumentMatchers.nullable;
@@ -670,9 +671,11 @@ public void activityStateChanges_singleSubscriber_callbackIsCalled() {
670671
AppStateMonitor monitor = new AppStateMonitor(transportManager, clock);
671672
Map<Integer, ApplicationProcessState> subscriberState = new HashMap<>();
672673

674+
// Register callbacks, but note that each callback is saved in a local variable. Otherwise
675+
// WeakReference can get garbage collected, making this test flaky.
673676
final int subscriber1 = 1;
674-
monitor.registerForAppState(
675-
new WeakReference<>(newState -> subscriberState.put(subscriber1, newState)));
677+
AppStateCallback callback1 = newState -> subscriberState.put(subscriber1, newState);
678+
monitor.registerForAppState(new WeakReference<>(callback1));
676679

677680
// Activity comes to Foreground
678681
monitor.onActivityResumed(activity1);
@@ -688,17 +691,19 @@ public void activityStateChanges_multipleSubscribers_callbackCalledOnEachSubscri
688691
AppStateMonitor monitor = new AppStateMonitor(transportManager, clock);
689692
Map<Integer, ApplicationProcessState> subscriberState = new HashMap<>();
690693

694+
// Register callbacks, but note that each callback is saved in a local variable. Otherwise
695+
// WeakReference can get garbage collected, making this test flaky.
691696
final int subscriber1 = 1;
692-
monitor.registerForAppState(
693-
new WeakReference<>(newState -> subscriberState.put(subscriber1, newState)));
697+
AppStateCallback callback1 = newState -> subscriberState.put(subscriber1, newState);
698+
monitor.registerForAppState(new WeakReference<>(callback1));
694699

695700
final int subscriber2 = 2;
696-
monitor.registerForAppState(
697-
new WeakReference<>(newState -> subscriberState.put(subscriber2, newState)));
701+
AppStateCallback callback2 = newState -> subscriberState.put(subscriber2, newState);
702+
monitor.registerForAppState(new WeakReference<>(callback2));
698703

699704
final int subscriber3 = 3;
700-
monitor.registerForAppState(
701-
new WeakReference<>(newState -> subscriberState.put(subscriber3, newState)));
705+
AppStateCallback callback3 = newState -> subscriberState.put(subscriber3, newState);
706+
monitor.registerForAppState(new WeakReference<>(callback3));
702707

703708
// Activity comes to Foreground
704709
monitor.onActivityResumed(activity1);

0 commit comments

Comments
 (0)