Skip to content

Commit 09a9dec

Browse files
committed
Remove app updates from session manager and add unit tests
1 parent 99a541e commit 09a9dec

File tree

4 files changed

+19
-151
lines changed

4 files changed

+19
-151
lines changed

firebase-perf/firebase-perf.gradle

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -118,7 +118,7 @@ dependencies {
118118
api("com.google.firebase:firebase-components:18.0.0")
119119
api("com.google.firebase:firebase-config:21.5.0")
120120
api("com.google.firebase:firebase-installations:17.2.0")
121-
api("com.google.firebase:firebase-sessions:2.0.7") {
121+
api(project(":firebase-sessions")) {
122122
exclude group: 'com.google.firebase', module: 'firebase-common'
123123
exclude group: 'com.google.firebase', module: 'firebase-common-ktx'
124124
exclude group: 'com.google.firebase', module: 'firebase-components'

firebase-perf/src/main/java/com/google/firebase/perf/session/SessionManager.java

Lines changed: 3 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -19,21 +19,21 @@
1919
import androidx.annotation.Keep;
2020
import androidx.annotation.VisibleForTesting;
2121
import com.google.firebase.perf.application.AppStateMonitor;
22-
import com.google.firebase.perf.application.AppStateUpdateHandler;
2322
import com.google.firebase.perf.session.gauges.GaugeManager;
2423
import com.google.firebase.perf.v1.ApplicationProcessState;
2524
import com.google.firebase.perf.v1.GaugeMetadata;
2625
import com.google.firebase.perf.v1.GaugeMetric;
2726
import java.lang.ref.WeakReference;
2827
import java.util.HashSet;
2928
import java.util.Iterator;
29+
import java.util.Objects;
3030
import java.util.Set;
3131
import java.util.UUID;
3232
import java.util.concurrent.Future;
3333

3434
/** Session manager to generate sessionIDs and broadcast to the application. */
3535
@Keep // Needed because of b/117526359.
36-
public class SessionManager extends AppStateUpdateHandler {
36+
public class SessionManager {
3737

3838
@SuppressLint("StaticFieldLeak")
3939
private static final SessionManager instance = new SessionManager();
@@ -69,7 +69,6 @@ public SessionManager(
6969
this.gaugeManager = gaugeManager;
7070
this.perfSession = perfSession;
7171
this.appStateMonitor = appStateMonitor;
72-
registerForAppState();
7372
}
7473

7574
/**
@@ -80,34 +79,6 @@ public void setApplicationContext(final Context appContext) {
8079
gaugeManager.initializeGaugeMetadataManager(appContext);
8180
}
8281

83-
@Override
84-
public void onUpdateAppState(ApplicationProcessState newAppState) {
85-
super.onUpdateAppState(newAppState);
86-
87-
if (appStateMonitor.isColdStart()) {
88-
// We want the Session to remain unchanged if this is a cold start of the app since we already
89-
// update the PerfSession in FirebasePerfProvider#onAttachInfo().
90-
return;
91-
}
92-
93-
if (newAppState == ApplicationProcessState.FOREGROUND) {
94-
// A new foregrounding of app will force a new sessionID generation.
95-
PerfSession session = PerfSession.createWithId(UUID.randomUUID().toString());
96-
updatePerfSession(session);
97-
} else {
98-
// If the session is running for too long, generate a new session and collect gauges as
99-
// necessary.
100-
if (perfSession.isSessionRunningTooLong()) {
101-
PerfSession session = PerfSession.createWithId(UUID.randomUUID().toString());
102-
updatePerfSession(session);
103-
} else {
104-
// For any other state change of the application, modify gauge collection state as
105-
// necessary.
106-
startOrStopCollectingGauges(newAppState);
107-
}
108-
}
109-
}
110-
11182
/**
11283
* Checks if the current {@link PerfSession} is expired/timed out. If so, stop collecting gauges.
11384
*
@@ -129,7 +100,7 @@ public void stopGaugeCollectionIfSessionRunningTooLong() {
129100
*/
130101
public void updatePerfSession(PerfSession perfSession) {
131102
// Do not update the perf session if it is the exact same sessionId.
132-
if (perfSession.sessionId() == this.perfSession.sessionId()) {
103+
if (Objects.equals(perfSession.sessionId(), this.perfSession.sessionId())) {
133104
return;
134105
}
135106

firebase-perf/src/test/java/com/google/firebase/perf/session/SessionManagerTest.java

Lines changed: 15 additions & 105 deletions
Original file line numberDiff line numberDiff line change
@@ -16,9 +16,6 @@
1616

1717
import static com.google.common.truth.Truth.assertThat;
1818
import static org.mockito.ArgumentMatchers.any;
19-
import static org.mockito.ArgumentMatchers.anyString;
20-
import static org.mockito.ArgumentMatchers.eq;
21-
import static org.mockito.ArgumentMatchers.nullable;
2219
import static org.mockito.Mockito.mock;
2320
import static org.mockito.Mockito.never;
2421
import static org.mockito.Mockito.spy;
@@ -40,7 +37,6 @@
4037
import org.junit.Before;
4138
import org.junit.Test;
4239
import org.junit.runner.RunWith;
43-
import org.mockito.AdditionalMatchers;
4440
import org.mockito.ArgumentMatchers;
4541
import org.mockito.InOrder;
4642
import org.mockito.Mock;
@@ -86,101 +82,12 @@ public void setApplicationContext_initializeGaugeMetadataManager()
8682
inOrder.verify(mockGaugeManager).initializeGaugeMetadataManager(any());
8783
}
8884

89-
@Test
90-
public void testOnUpdateAppStateDoesNothingDuringAppStart() {
91-
String oldSessionId = SessionManager.getInstance().perfSession().sessionId();
92-
93-
assertThat(oldSessionId).isNotNull();
94-
assertThat(oldSessionId).isEqualTo(SessionManager.getInstance().perfSession().sessionId());
95-
96-
AppStateMonitor.getInstance().setIsColdStart(true);
97-
98-
SessionManager.getInstance().onUpdateAppState(ApplicationProcessState.FOREGROUND);
99-
assertThat(oldSessionId).isEqualTo(SessionManager.getInstance().perfSession().sessionId());
100-
}
101-
102-
@Test
103-
public void testOnUpdateAppStateGeneratesNewSessionIdOnForegroundState() {
104-
String oldSessionId = SessionManager.getInstance().perfSession().sessionId();
105-
106-
assertThat(oldSessionId).isNotNull();
107-
assertThat(oldSessionId).isEqualTo(SessionManager.getInstance().perfSession().sessionId());
108-
109-
SessionManager.getInstance().onUpdateAppState(ApplicationProcessState.FOREGROUND);
110-
assertThat(oldSessionId).isNotEqualTo(SessionManager.getInstance().perfSession().sessionId());
111-
}
112-
113-
@Test
114-
public void testOnUpdateAppStateDoesntGenerateNewSessionIdOnBackgroundState() {
115-
String oldSessionId = SessionManager.getInstance().perfSession().sessionId();
116-
117-
assertThat(oldSessionId).isNotNull();
118-
assertThat(oldSessionId).isEqualTo(SessionManager.getInstance().perfSession().sessionId());
119-
120-
SessionManager.getInstance().onUpdateAppState(ApplicationProcessState.BACKGROUND);
121-
assertThat(oldSessionId).isEqualTo(SessionManager.getInstance().perfSession().sessionId());
122-
}
123-
124-
@Test
125-
public void testOnUpdateAppStateGeneratesNewSessionIdOnBackgroundStateIfPerfSessionExpires() {
126-
when(mockPerfSession.isSessionRunningTooLong()).thenReturn(true);
127-
SessionManager testSessionManager =
128-
new SessionManager(mockGaugeManager, mockPerfSession, mockAppStateMonitor);
129-
String oldSessionId = testSessionManager.perfSession().sessionId();
130-
131-
assertThat(oldSessionId).isNotNull();
132-
assertThat(oldSessionId).isEqualTo(testSessionManager.perfSession().sessionId());
133-
134-
testSessionManager.onUpdateAppState(ApplicationProcessState.BACKGROUND);
135-
assertThat(oldSessionId).isNotEqualTo(testSessionManager.perfSession().sessionId());
136-
}
137-
138-
@Test
139-
public void
140-
testOnUpdateAppStateDoesntMakeGaugeManagerLogGaugeMetadataOnForegroundStateIfSessionIsNonVerbose() {
141-
forceNonVerboseSession();
142-
143-
SessionManager testSessionManager =
144-
new SessionManager(mockGaugeManager, mockPerfSession, mockAppStateMonitor);
145-
testSessionManager.onUpdateAppState(ApplicationProcessState.FOREGROUND);
146-
147-
verify(mockGaugeManager, never())
148-
.logGaugeMetadata(
149-
anyString(), nullable(com.google.firebase.perf.v1.ApplicationProcessState.class));
150-
}
151-
152-
@Test
153-
public void
154-
testOnUpdateAppStateDoesntMakeGaugeManagerLogGaugeMetadataOnBackgroundStateEvenIfSessionIsVerbose() {
155-
forceVerboseSession();
156-
157-
SessionManager testSessionManager =
158-
new SessionManager(mockGaugeManager, mockPerfSession, mockAppStateMonitor);
159-
testSessionManager.onUpdateAppState(ApplicationProcessState.BACKGROUND);
160-
161-
verify(mockGaugeManager, never())
162-
.logGaugeMetadata(
163-
anyString(), nullable(com.google.firebase.perf.v1.ApplicationProcessState.class));
164-
}
165-
166-
@Test
167-
public void testOnUpdateAppStateMakesGaugeManagerStartCollectingGaugesIfSessionIsVerbose() {
168-
forceVerboseSession();
169-
170-
SessionManager testSessionManager =
171-
new SessionManager(mockGaugeManager, mockPerfSession, mockAppStateMonitor);
172-
testSessionManager.onUpdateAppState(ApplicationProcessState.FOREGROUND);
173-
174-
verify(mockGaugeManager)
175-
.startCollectingGauges(AdditionalMatchers.not(eq(mockPerfSession)), any());
176-
}
177-
17885
// LogGaugeData on new perf session when Verbose
17986
// NotLogGaugeData on new perf session when not Verbose
18087
// Mark Session as expired after time limit.
18188

18289
@Test
183-
public void testOnUpdateAppStateMakesGaugeManagerStopCollectingGaugesIfSessionIsNonVerbose() {
90+
public void testUpdatePerfSessionMakesGaugeManagerStopCollectingGaugesIfSessionIsNonVerbose() {
18491
forceNonVerboseSession();
18592

18693
SessionManager testSessionManager =
@@ -191,7 +98,7 @@ public void testOnUpdateAppStateMakesGaugeManagerStopCollectingGaugesIfSessionIs
19198
}
19299

193100
@Test
194-
public void testOnUpdateAppStateMakesGaugeManagerStopCollectingGaugesWhenSessionsDisabled() {
101+
public void testUpdatePerfSessionMakesGaugeManagerStopCollectingGaugesWhenSessionsDisabled() {
195102
forceSessionsFeatureDisabled();
196103

197104
SessionManager testSessionManager =
@@ -221,22 +128,25 @@ public void testSessionIdDoesNotUpdateIfPerfSessionRunsTooLong() {
221128
}
222129

223130
@Test
224-
public void testPerfSessionExpiredMakesGaugeManagerStopsCollectingGaugesIfSessionIsVerbose() {
225-
forceVerboseSession();
131+
public void testUpdatePerfSessionStartsCollectingGaugesIfSessionIsVerbose() {
226132
Timer mockTimer = mock(Timer.class);
227133
when(mockClock.getTime()).thenReturn(mockTimer);
134+
when(mockAppStateMonitor.getAppState()).thenReturn(ApplicationProcessState.FOREGROUND);
228135

229-
PerfSession session = new PerfSession("sessionId", mockClock);
230-
SessionManager testSessionManager =
231-
new SessionManager(mockGaugeManager, session, mockAppStateMonitor);
136+
PerfSession previousSession = new PerfSession("previousSession", mockClock);
137+
previousSession.setGaugeAndEventCollectionEnabled(false);
232138

233-
assertThat(session.isSessionRunningTooLong()).isFalse();
139+
PerfSession newSession = new PerfSession("newSession", mockClock);
140+
newSession.setGaugeAndEventCollectionEnabled(true);
234141

235-
when(mockTimer.getDurationMicros())
236-
.thenReturn(TimeUnit.HOURS.toMicros(5)); // Default Max Session Length is 4 hours
142+
SessionManager testSessionManager =
143+
new SessionManager(mockGaugeManager, previousSession, mockAppStateMonitor);
144+
testSessionManager.updatePerfSession(newSession);
145+
testSessionManager.setApplicationContext(mockApplicationContext);
237146

238-
assertThat(session.isSessionRunningTooLong()).isTrue();
239-
verify(mockGaugeManager, times(0)).logGaugeMetadata(any(), any());
147+
verify(mockGaugeManager, times(1)).initializeGaugeMetadataManager(mockApplicationContext);
148+
verify(mockGaugeManager, times(1))
149+
.startCollectingGauges(newSession, ApplicationProcessState.FOREGROUND);
240150
}
241151

242152
@Test

firebase-sessions/src/main/kotlin/com/google/firebase/sessions/api/FirebaseSessionsDependencies.kt

Lines changed: 0 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -40,19 +40,6 @@ object FirebaseSessionsDependencies {
4040
*/
4141
@JvmStatic
4242
fun addDependency(subscriberName: SessionSubscriber.Name) {
43-
if (subscriberName == SessionSubscriber.Name.PERFORMANCE) {
44-
throw IllegalArgumentException(
45-
"""
46-
Incompatible versions of Firebase Perf and Firebase Sessions.
47-
A safe combination would be:
48-
firebase-sessions:1.1.0
49-
firebase-crashlytics:18.5.0
50-
firebase-perf:20.5.0
51-
For more information contact Firebase Support.
52-
"""
53-
.trimIndent()
54-
)
55-
}
5643
if (dependencies.containsKey(subscriberName)) {
5744
Log.d(TAG, "Dependency $subscriberName already added.")
5845
return

0 commit comments

Comments
 (0)