Skip to content

Commit 97bc643

Browse files
committed
Fix tests
1 parent a0ee2b2 commit 97bc643

File tree

8 files changed

+80
-53
lines changed

8 files changed

+80
-53
lines changed

firebase-perf/src/main/java/com/google/firebase/perf/FirebasePerformance.java

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,7 @@
4646
import com.google.firebase.remoteconfig.RemoteConfigComponent;
4747
import com.google.firebase.sessions.BuildConfig;
4848
import com.google.firebase.sessions.api.FirebaseSessionsDependencies;
49+
import com.google.firebase.sessions.api.SessionSubscriber;
4950
import java.lang.annotation.Retention;
5051
import java.lang.annotation.RetentionPolicy;
5152
import java.net.URL;
@@ -96,6 +97,8 @@ public class FirebasePerformance implements FirebasePerformanceAttributable {
9697
// once during initialization and cache it.
9798
private final ImmutableBundle mMetadataBundle;
9899

100+
private final SessionSubscriber sessionSubscriber;
101+
99102
/** Valid HttpMethods for manual network APIs */
100103
@StringDef({
101104
HttpMethod.GET,
@@ -169,6 +172,7 @@ public static FirebasePerformance getInstance() {
169172
this.mPerformanceCollectionForceEnabledState = false;
170173
this.configResolver = configResolver;
171174
this.mMetadataBundle = new ImmutableBundle(new Bundle());
175+
this.sessionSubscriber = new FirebasePerformanceSessionSubscriber(false);
172176
return;
173177
}
174178
DebugEnforcementCheck.setEnforcement(BuildConfig.DEBUG);
@@ -186,8 +190,8 @@ public static FirebasePerformance getInstance() {
186190
sessionManager.setApplicationContext(appContext);
187191

188192
mPerformanceCollectionForceEnabledState = configResolver.getIsPerformanceCollectionEnabled();
189-
FirebaseSessionsDependencies.register(
190-
new FirebasePerformanceSessionSubscriber(isPerformanceCollectionEnabled()));
193+
sessionSubscriber = new FirebasePerformanceSessionSubscriber(isPerformanceCollectionEnabled());
194+
FirebaseSessionsDependencies.register(sessionSubscriber);
191195

192196
if (logger.isLogcatEnabled() && isPerformanceCollectionEnabled()) {
193197
logger.info(
@@ -463,4 +467,9 @@ private static ImmutableBundle extractMetadata(Context appContext) {
463467
Boolean getPerformanceCollectionForceEnabledState() {
464468
return mPerformanceCollectionForceEnabledState;
465469
}
470+
471+
@VisibleForTesting
472+
SessionSubscriber getSessionSubscriber() {
473+
return sessionSubscriber;
474+
}
466475
}

firebase-perf/src/main/java/com/google/firebase/perf/session/AqsUtils.kt

Lines changed: 0 additions & 20 deletions
This file was deleted.
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
package com.google.firebase.perf.session
2+
3+
import com.google.firebase.perf.util.Constants
4+
import java.util.UUID
5+
6+
/** Identifies whether the [PerfSession] is an AQS or not. */
7+
fun PerfSession.isAQS(): Boolean {
8+
return !this.sessionId().startsWith(Constants.UNDEFINED_AQS_ID_PREFIX)
9+
}
10+
11+
fun createLegacySessionId(): String {
12+
val uuid = UUID.randomUUID().toString().replace("-", "")
13+
return uuid.replaceRange(
14+
0,
15+
Constants.UNDEFINED_AQS_ID_PREFIX.length,
16+
Constants.UNDEFINED_AQS_ID_PREFIX
17+
)
18+
}

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

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,6 @@
2424
import com.google.firebase.perf.util.Timer;
2525
import com.google.firebase.perf.v1.SessionVerbosity;
2626
import java.util.List;
27-
import java.util.UUID;
2827
import java.util.concurrent.TimeUnit;
2928

3029
/** Details of a session including a unique Id and related information. */
@@ -37,11 +36,9 @@ public class PerfSession implements Parcelable {
3736
* Creates a PerfSession object and decides what metrics to collect.
3837
*/
3938
public static PerfSession createWithId(@Nullable String aqsSessionId) {
40-
String sessionId;
41-
if (aqsSessionId == null) {
42-
sessionId = AqsUtilsKt.createSessionId();
43-
} else {
44-
sessionId = AqsUtilsKt.createSessionId(aqsSessionId);
39+
String sessionId = aqsSessionId;
40+
if (sessionId == null) {
41+
sessionId = FirebaseSessionsHelperKt.createLegacySessionId();
4542
}
4643
PerfSession session = new PerfSession(sessionId, new Clock());
4744
session.setGaugeAndEventCollectionEnabled(session.shouldCollectGaugesAndEvents());
@@ -63,6 +60,7 @@ private PerfSession(@NonNull Parcel in) {
6360
}
6461

6562
/** Returns the sessionId for the given session. */
63+
@NonNull
6664
public String sessionId() {
6765
return sessionId;
6866
}
@@ -157,7 +155,8 @@ public static com.google.firebase.perf.v1.PerfSession[] buildAndSort(
157155
public boolean shouldCollectGaugesAndEvents() {
158156
ConfigResolver configResolver = ConfigResolver.getInstance();
159157
return configResolver.isPerformanceMonitoringEnabled()
160-
&& (Math.abs(this.sessionId.hashCode() % 100) < configResolver.getSessionsSamplingRate() * 100);
158+
&& (Math.abs(this.sessionId.hashCode() % 100)
159+
< configResolver.getSessionsSamplingRate() * 100);
161160
}
162161

163162
/**

firebase-perf/src/main/java/com/google/firebase/perf/session/gauges/GaugeManager.java

Lines changed: 13 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@
2222
import com.google.firebase.components.Lazy;
2323
import com.google.firebase.perf.config.ConfigResolver;
2424
import com.google.firebase.perf.logging.AndroidLogger;
25+
import com.google.firebase.perf.session.FirebaseSessionsHelperKt;
2526
import com.google.firebase.perf.session.PerfSession;
2627
import com.google.firebase.perf.transport.TransportManager;
2728
import com.google.firebase.perf.util.Constants;
@@ -111,9 +112,7 @@ public void updateGaugeCollection(ApplicationProcessState applicationProcessStat
111112
gaugeManagerDataCollectionJob.cancel(false);
112113
}
113114

114-
if (session == null
115-
|| !session.isGaugeAndEventCollectionEnabled()
116-
|| session.sessionId().equals(Constants.UNDEFINED_AQS_ID_PREFIX)) {
115+
if (!isValidSessionForLogging()) {
117116
logger.warn("Not starting gauge collection.");
118117
stopCollectingGauges();
119118
return;
@@ -123,7 +122,7 @@ public void updateGaugeCollection(ApplicationProcessState applicationProcessStat
123122
Math.min(
124123
getCpuGaugeCollectionFrequencyMs(applicationProcessState),
125124
getMemoryGaugeCollectionFrequencyMs(applicationProcessState));
126-
String sessionIdForScheduledTask = session.aqsSessionId();
125+
String sessionIdForScheduledTask = session.sessionId();
127126

128127
try {
129128
gaugeManagerDataCollectionJob =
@@ -254,9 +253,9 @@ public void stopCollectingGauges() {
254253
* @param appState The app state for which these gauges are collected.
255254
*/
256255
private void syncFlush(String sessionId, ApplicationProcessState appState) {
257-
if (sessionId.equals(Constants.UNDEFINED_AQS_ID_PREFIX)) {
256+
if (sessionId.contains(Constants.UNDEFINED_AQS_ID_PREFIX)) {
258257
// TODO(b/394127311): Use DebugEnforcementCheck.
259-
logger.debug("Flushing gauge metrics to an undefined session ID.");
258+
logger.debug("Flushing gauge metrics to a legacy session ID.");
260259
}
261260
GaugeMetric.Builder gaugeMetricBuilder = GaugeMetric.newBuilder();
262261

@@ -279,16 +278,16 @@ private void syncFlush(String sessionId, ApplicationProcessState appState) {
279278
/**
280279
* Log the Gauge Metadata information to the transport.
281280
*
282-
* @param aqsSessionId The {@link PerfSession#aqsSessionId()} ()} to which the collected Gauge Metrics
281+
* @param sessionId The {@link PerfSession#sessionId()} ()} to which the collected Gauge Metrics
283282
* should be associated with.
284283
* @param appState The {@link ApplicationProcessState} for which these gauges are collected.
285284
* @return true if GaugeMetadata was logged, false otherwise.
286285
*/
287-
public boolean logGaugeMetadata(String aqsSessionId, ApplicationProcessState appState) {
286+
public boolean logGaugeMetadata(String sessionId, ApplicationProcessState appState) {
288287
if (gaugeMetadataManager != null) {
289288
GaugeMetric gaugeMetric =
290289
GaugeMetric.newBuilder()
291-
.setSessionId(aqsSessionId)
290+
.setSessionId(sessionId)
292291
.setGaugeMetadata(getGaugeMetadata())
293292
.build();
294293
transportManager.log(gaugeMetric, appState);
@@ -432,6 +431,11 @@ private long getMemoryGaugeCollectionFrequencyMs(
432431
}
433432
}
434433

434+
private boolean isValidSessionForLogging() {
435+
if (session == null) return false;
436+
return session.isGaugeAndEventCollectionEnabled() && FirebaseSessionsHelperKt.isAQS(session);
437+
}
438+
435439
@VisibleForTesting
436440
void setApplicationProcessState(ApplicationProcessState applicationProcessState) {
437441
this.applicationProcessState = applicationProcessState;

firebase-perf/src/test/java/com/google/firebase/perf/FirebasePerformanceTestBase.java

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -96,8 +96,7 @@ protected static void forceNonVerboseSession() {
9696
}
9797

9898
protected static void forceAppQualitySession() {
99-
PerfSession existingPerfSession = PerfSession.createWithId(UUID.randomUUID().toString());
100-
existingPerfSession.setAQSId(new SessionSubscriber.SessionDetails("notnull"));
99+
PerfSession existingPerfSession = createPerfSession(false);
101100
SessionManager.getInstance().setPerfSession(existingPerfSession);
102101
SessionSubscriber sessionSubscriber = FirebasePerformance.getInstance().getSessionSubscriber();
103102
sessionSubscriber.onSessionChanged(new SessionSubscriber.SessionDetails("fakeAQS"));
@@ -109,4 +108,13 @@ private static void forceVerboseSessionWithSamplingPercentage(long samplingPerce
109108
ConfigResolver.getInstance().setMetadataBundle(new ImmutableBundle(bundle));
110109
forceAppQualitySession();
111110
}
111+
112+
private static PerfSession createPerfSession(boolean isLegacy) {
113+
if (isLegacy) {
114+
return PerfSession.createWithId(null);
115+
}
116+
117+
String aqsId = UUID.randomUUID().toString().replace("-", "");
118+
return PerfSession.createWithId(aqsId);
119+
}
112120
}

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

Lines changed: 20 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -62,12 +62,24 @@ public void setUp() {
6262

6363
@Test
6464
public void instanceCreation() {
65-
PerfSession session = new PerfSession("sessionId", mockClock, true);
65+
PerfSession session = PerfSession.createWithId("sessionId");
6666
assertThat(session).isNotNull();
6767
session.setGaugeAndEventCollectionEnabled(true);
68-
Assert.assertTrue(session.isGaugeAndEventCollectionEnabled());
68+
assertThat(session.isVerbose()).isTrue();
6969
session.setGaugeAndEventCollectionEnabled(false);
70-
Assert.assertFalse(session.isGaugeAndEventCollectionEnabled());
70+
assertThat(session.isVerbose()).isFalse();
71+
assertThat(FirebaseSessionsHelperKt.isAQS(session)).isTrue();
72+
}
73+
74+
@Test
75+
public void legacyInstanceCreation() {
76+
PerfSession perfSession = PerfSession.createWithId(null);
77+
assertThat(perfSession).isNotNull();
78+
perfSession.setGaugeAndEventCollectionEnabled(true);
79+
assertThat(perfSession.isVerbose()).isTrue();
80+
perfSession.setGaugeAndEventCollectionEnabled(false);
81+
assertThat(perfSession.isVerbose()).isFalse();
82+
assertThat(FirebaseSessionsHelperKt.isAQS(perfSession)).isFalse();
7183
}
7284

7385
@Test
@@ -142,7 +154,7 @@ public void shouldCollectGaugesAndEvents_perfMonDeactivated_sessionNotVerbose()
142154

143155
@Test
144156
public void testPerfSessionConversion() {
145-
PerfSession session1 = new PerfSession("sessionId", mockClock, true);
157+
PerfSession session1 = PerfSession.createWithId("aqsSessionId");
146158
session1.setGaugeAndEventCollectionEnabled(true);
147159

148160
com.google.firebase.perf.v1.PerfSession perfSession = session1.build();
@@ -153,7 +165,7 @@ public void testPerfSessionConversion() {
153165

154166
@Test
155167
public void testPerfSessionConversionWithoutVerbosity() {
156-
PerfSession session1 = new PerfSession("sessionId", mockClock, true);
168+
PerfSession session1 = PerfSession.createWithId("sessionId");
157169

158170
com.google.firebase.perf.v1.PerfSession perfSession = session1.build();
159171
Assert.assertEquals(session1.sessionId(), perfSession.getSessionId());
@@ -219,7 +231,7 @@ public void testIsExpiredReturnsFalseWhenCurrentSessionLengthIsLessThanMaxSessio
219231
- TimeUnit.MINUTES.toMicros(1)); // Default Max Session Length is 4 hours
220232
when(mockClock.getTime()).thenReturn(mockTimer);
221233

222-
PerfSession session = new PerfSession("sessionId", mockClock, true);
234+
PerfSession session = PerfSession.createWithId("sessionId");
223235
assertThat(session.isSessionRunningTooLong()).isFalse();
224236
}
225237

@@ -230,7 +242,7 @@ public void testIsExpiredReturnsFalseWhenCurrentSessionLengthIsEqualToMaxSession
230242
.thenReturn(TimeUnit.HOURS.toMicros(4)); // Default Max Session Length is 4 hours
231243
when(mockClock.getTime()).thenReturn(mockTimer);
232244

233-
PerfSession session = new PerfSession("sessionId", mockClock, true);
245+
PerfSession session = PerfSession.createWithId("sessionId");
234246
assertThat(session.isSessionRunningTooLong()).isFalse();
235247
}
236248

@@ -241,7 +253,7 @@ public void testIsExpiredReturnsTrueWhenCurrentSessionLengthIsGreaterThanMaxSess
241253
.thenReturn(TimeUnit.HOURS.toMicros(5)); // Default Max Session Length is 4 hours
242254
when(mockClock.getTime()).thenReturn(mockTimer);
243255

244-
PerfSession session = new PerfSession("sessionId", mockClock, true);
256+
PerfSession session = PerfSession.createWithId("sessionId");
245257
assertThat(session.isSessionRunningTooLong()).isTrue();
246258
}
247259
}

firebase-perf/src/test/java/com/google/firebase/perf/transport/TransportManagerTest.java

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,6 @@
5353
import com.google.firebase.perf.v1.PerfMetric;
5454
import com.google.firebase.perf.v1.PerfSession;
5555
import com.google.firebase.perf.v1.TraceMetric;
56-
import com.google.firebase.sessions.api.SessionSubscriber;
5756
import com.google.testing.timing.FakeScheduledExecutorService;
5857
import java.util.ArrayList;
5958
import java.util.List;
@@ -1175,7 +1174,6 @@ public void logTraceMetric_sessionEnabled_doesNotStripOffSessionId() {
11751174
List<PerfSession> perfSessions = new ArrayList<>();
11761175
com.google.firebase.perf.session.PerfSession testSession =
11771176
com.google.firebase.perf.session.PerfSession.createWithId("fakeSessionId");
1178-
testSession.setAQSId(new SessionSubscriber.SessionDetails("fakeAqsSessionId"));
11791177
perfSessions.add(testSession.build());
11801178
validTrace.addAllPerfSessions(perfSessions);
11811179

@@ -1185,7 +1183,7 @@ public void logTraceMetric_sessionEnabled_doesNotStripOffSessionId() {
11851183
PerfMetric loggedPerfMetric = getLastLoggedEvent(times(1));
11861184
assertThat(loggedPerfMetric.getTraceMetric().getPerfSessionsCount()).isEqualTo(1);
11871185
assertThat(loggedPerfMetric.getTraceMetric().getPerfSessions(0).getSessionId())
1188-
.isEqualTo("fakeAqsSessionId");
1186+
.isEqualTo("fakeSessionId");
11891187
}
11901188

11911189
@Test
@@ -1195,7 +1193,6 @@ public void logNetworkMetric_sessionEnabled_doesNotStripOffSessionId() {
11951193
List<PerfSession> perfSessions = new ArrayList<>();
11961194
com.google.firebase.perf.session.PerfSession testSession =
11971195
com.google.firebase.perf.session.PerfSession.createWithId("fakeSessionId");
1198-
testSession.setAQSId(new SessionSubscriber.SessionDetails("fakeAqsSessionId"));
11991196
perfSessions.add(testSession.build());
12001197
validNetworkRequest.clearPerfSessions().addAllPerfSessions(perfSessions);
12011198

@@ -1205,7 +1202,7 @@ public void logNetworkMetric_sessionEnabled_doesNotStripOffSessionId() {
12051202
PerfMetric loggedPerfMetric = getLastLoggedEvent(times(1));
12061203
assertThat(loggedPerfMetric.getNetworkRequestMetric().getPerfSessionsCount()).isEqualTo(1);
12071204
assertThat(loggedPerfMetric.getNetworkRequestMetric().getPerfSessions(0).getSessionId())
1208-
.isEqualTo("fakeAqsSessionId");
1205+
.isEqualTo("fakeSessionId");
12091206
}
12101207

12111208
@Test

0 commit comments

Comments
 (0)