Skip to content

Commit a9e8354

Browse files
committed
Remove LinkedHashMap TODO.
2 parents 4c637aa + c2e355e commit a9e8354

File tree

1 file changed

+176
-11
lines changed

1 file changed

+176
-11
lines changed

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

Lines changed: 176 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,10 @@
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;
22+
import static org.mockito.Mockito.mock;
1923
import static org.mockito.Mockito.never;
2024
import static org.mockito.Mockito.spy;
2125
import static org.mockito.Mockito.times;
@@ -28,11 +32,15 @@
2832
import com.google.firebase.perf.application.AppStateMonitor;
2933
import com.google.firebase.perf.session.gauges.GaugeManager;
3034
import com.google.firebase.perf.util.Clock;
35+
import com.google.firebase.perf.util.Timer;
36+
import com.google.firebase.perf.v1.ApplicationProcessState;
3137
import java.lang.ref.WeakReference;
3238
import java.util.concurrent.ExecutionException;
39+
import java.util.concurrent.TimeUnit;
3340
import org.junit.Before;
3441
import org.junit.Test;
3542
import org.junit.runner.RunWith;
43+
import org.mockito.AdditionalMatchers;
3644
import org.mockito.ArgumentMatchers;
3745
import org.mockito.InOrder;
3846
import org.mockito.Mock;
@@ -66,7 +74,7 @@ public void testInstanceCreation() {
6674
}
6775

6876
@Test
69-
public void setApplicationContext_logGaugeMetadata_afterGaugeMetadataManagerIsInitialized()
77+
public void setApplicationContext_initializeGaugeMetadataManager()
7078
throws ExecutionException, InterruptedException {
7179
when(mockPerfSession.isGaugeAndEventCollectionEnabled()).thenReturn(true);
7280
InOrder inOrder = Mockito.inOrder(mockGaugeManager);
@@ -75,18 +83,171 @@ public void setApplicationContext_logGaugeMetadata_afterGaugeMetadataManagerIsIn
7583
testSessionManager.setApplicationContext(mockApplicationContext);
7684

7785
testSessionManager.getSyncInitFuture().get();
78-
inOrder.verify(mockGaugeManager).initializeGaugeMetadataManager(any(), any());
79-
inOrder.verify(mockGaugeManager).logGaugeMetadata(any());
86+
inOrder.verify(mockGaugeManager).initializeGaugeMetadataManager(any());
87+
}
88+
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+
178+
// LogGaugeData on new perf session when Verbose
179+
// NotLogGaugeData on new perf session when not Verbose
180+
// Mark Session as expired after time limit.
181+
182+
@Test
183+
public void testOnUpdateAppStateMakesGaugeManagerStopCollectingGaugesIfSessionIsNonVerbose() {
184+
forceNonVerboseSession();
185+
186+
SessionManager testSessionManager =
187+
new SessionManager(mockGaugeManager, mockPerfSession, mockAppStateMonitor);
188+
testSessionManager.updatePerfSession(PerfSession.createWithId("testSessionId"));
189+
190+
verify(mockGaugeManager).stopCollectingGauges();
191+
}
192+
193+
@Test
194+
public void testOnUpdateAppStateMakesGaugeManagerStopCollectingGaugesWhenSessionsDisabled() {
195+
forceSessionsFeatureDisabled();
196+
197+
SessionManager testSessionManager =
198+
new SessionManager(
199+
mockGaugeManager, PerfSession.createWithId("testSessionId"), mockAppStateMonitor);
200+
testSessionManager.updatePerfSession(PerfSession.createWithId("testSessionId2"));
201+
202+
verify(mockGaugeManager).stopCollectingGauges();
203+
}
204+
205+
@Test
206+
public void testSessionIdDoesNotUpdateIfPerfSessionRunsTooLong() {
207+
Timer mockTimer = mock(Timer.class);
208+
when(mockClock.getTime()).thenReturn(mockTimer);
209+
210+
PerfSession session = new PerfSession("sessionId", mockClock);
211+
SessionManager testSessionManager =
212+
new SessionManager(mockGaugeManager, session, mockAppStateMonitor);
213+
214+
assertThat(session.isSessionRunningTooLong()).isFalse();
215+
216+
when(mockTimer.getDurationMicros())
217+
.thenReturn(TimeUnit.HOURS.toMicros(5)); // Default Max Session Length is 4 hours
218+
assertThat(session.isSessionRunningTooLong()).isTrue();
219+
220+
assertThat(testSessionManager.perfSession().sessionId()).isEqualTo("sessionId");
221+
}
222+
223+
@Test
224+
public void testPerfSessionExpiredMakesGaugeManagerStopsCollectingGaugesIfSessionIsVerbose() {
225+
forceVerboseSession();
226+
Timer mockTimer = mock(Timer.class);
227+
when(mockClock.getTime()).thenReturn(mockTimer);
228+
229+
PerfSession session = new PerfSession("sessionId", mockClock);
230+
SessionManager testSessionManager =
231+
new SessionManager(mockGaugeManager, session, mockAppStateMonitor);
232+
233+
assertThat(session.isSessionRunningTooLong()).isFalse();
234+
235+
when(mockTimer.getDurationMicros())
236+
.thenReturn(TimeUnit.HOURS.toMicros(5)); // Default Max Session Length is 4 hours
237+
238+
assertThat(session.isSessionRunningTooLong()).isTrue();
239+
verify(mockGaugeManager, times(0)).logGaugeMetadata(any(), any());
80240
}
81241

82242
@Test
83243
public void testPerfSession_sessionAwareObjects_doesntNotifyIfNotRegistered() {
84-
SessionManager testSessionManager = SessionManager.getInstance();
244+
SessionManager testSessionManager =
245+
new SessionManager(mockGaugeManager, mockPerfSession, mockAppStateMonitor);
85246

86247
FakeSessionAwareObject spySessionAwareObjectOne = spy(new FakeSessionAwareObject());
87248
FakeSessionAwareObject spySessionAwareObjectTwo = spy(new FakeSessionAwareObject());
88249

89-
testSessionManager.updatePerfSession(PerfSession.createNewSession());
250+
testSessionManager.updatePerfSession(PerfSession.createWithId("testSessionId1"));
90251

91252
verify(spySessionAwareObjectOne, never())
92253
.updateSession(ArgumentMatchers.nullable(PerfSession.class));
@@ -96,15 +257,17 @@ public void testPerfSession_sessionAwareObjects_doesntNotifyIfNotRegistered() {
96257

97258
@Test
98259
public void testPerfSession_sessionAwareObjects_NotifiesIfRegistered() {
99-
SessionManager testSessionManager = SessionManager.getInstance();
260+
SessionManager testSessionManager =
261+
new SessionManager(mockGaugeManager, mockPerfSession, mockAppStateMonitor);
262+
100263
FakeSessionAwareObject spySessionAwareObjectOne = spy(new FakeSessionAwareObject());
101264
FakeSessionAwareObject spySessionAwareObjectTwo = spy(new FakeSessionAwareObject());
102265

103266
testSessionManager.registerForSessionUpdates(new WeakReference<>(spySessionAwareObjectOne));
104267
testSessionManager.registerForSessionUpdates(new WeakReference<>(spySessionAwareObjectTwo));
105268

106-
triggerAqsSession();
107-
triggerAqsSession();
269+
testSessionManager.updatePerfSession(PerfSession.createWithId("testSessionId1"));
270+
testSessionManager.updatePerfSession(PerfSession.createWithId("testSessionId2"));
108271

109272
verify(spySessionAwareObjectOne, times(2))
110273
.updateSession(ArgumentMatchers.nullable(PerfSession.class));
@@ -114,7 +277,9 @@ public void testPerfSession_sessionAwareObjects_NotifiesIfRegistered() {
114277

115278
@Test
116279
public void testPerfSession_sessionAwareObjects_DoesNotNotifyIfUnregistered() {
117-
SessionManager testSessionManager = SessionManager.getInstance();
280+
SessionManager testSessionManager =
281+
new SessionManager(mockGaugeManager, mockPerfSession, mockAppStateMonitor);
282+
118283
FakeSessionAwareObject spySessionAwareObjectOne = spy(new FakeSessionAwareObject());
119284
FakeSessionAwareObject spySessionAwareObjectTwo = spy(new FakeSessionAwareObject());
120285

@@ -126,11 +291,11 @@ public void testPerfSession_sessionAwareObjects_DoesNotNotifyIfUnregistered() {
126291
testSessionManager.registerForSessionUpdates(weakSpySessionAwareObjectOne);
127292
testSessionManager.registerForSessionUpdates(weakSpySessionAwareObjectTwo);
128293

129-
triggerAqsSession();
294+
testSessionManager.updatePerfSession(PerfSession.createWithId("testSessionId1"));
130295

131296
testSessionManager.unregisterForSessionUpdates(weakSpySessionAwareObjectOne);
132297
testSessionManager.unregisterForSessionUpdates(weakSpySessionAwareObjectTwo);
133-
triggerAqsSession();
298+
testSessionManager.updatePerfSession(PerfSession.createWithId("testSessionId2"));
134299

135300
verify(spySessionAwareObjectOne, times(1))
136301
.updateSession(ArgumentMatchers.nullable(PerfSession.class));

0 commit comments

Comments
 (0)