8000 Deflake `AppStateMonitorTest` by leotianlizhan · Pull Request #4294 · firebase/firebase-android-sdk · GitHub
[go: up one dir, main page]

Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
package com.google.firebase.perf.application;

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

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

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

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

final int subscriber2 = 2;
monitor.registerForAppState(
new WeakReference<>(newState -> subscriberState.put(subscriber2, newState)));
AppStateCallback callback2 = newState -> subscriberState.put(subscriber2, newState);
monitor.registerForAppState(new WeakReference<>(callback2));

final int subscriber3 = 3;
monitor.registerForAppState(
new WeakReference<>(newState -> subscriberState.put(subscriber3, newState)));
AppStateCallback callback3 = newState -> subscriberState.put(subscriber3, newState);
monitor.registerForAppState(new WeakReference<>(callback3));

// Activity comes to Foreground
monitor.onActivityResumed(activity1);
Expand Down
0