Skip to content

Commit 77cd685

Browse files
committed
Add unit tests for new logging in GaugeManager using GaugeCounter. (#6953)
- Fixes relevant unit tests - specifically requiring a definite `ApplicationProcessState` - Adds new unit tests for the updated behaviour - Deletes unit tests that *should* be OK to delete.
1 parent d398670 commit 77cd685

File tree

4 files changed

+221
-318
lines changed

4 files changed

+221
-318
lines changed

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

Lines changed: 18 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,8 @@
1414

1515
package com.google.firebase.perf.session.gauges
1616

17+
import androidx.annotation.VisibleForTesting
18+
import com.google.firebase.perf.logging.AndroidLogger
1719
import java.util.concurrent.atomic.AtomicInteger
1820

1921
/**
@@ -23,17 +25,31 @@ import java.util.concurrent.atomic.AtomicInteger
2325
object GaugeCounter {
2426
private const val MAX_METRIC_COUNT = 25
2527
private val counter = AtomicInteger(0)
26-
private val gaugeManager: GaugeManager = GaugeManager.getInstance()
28+
private val logger = AndroidLogger.getInstance()
29+
30+
@set:VisibleForTesting(otherwise = VisibleForTesting.NONE)
31+
@set:JvmStatic
32+
var gaugeManager: GaugeManager = GaugeManager.getInstance()
2733

2834
fun incrementCounter() {
2935
val metricsCount = counter.incrementAndGet()
3036

3137
if (metricsCount >= MAX_METRIC_COUNT) {
3238
gaugeManager.logGaugeMetrics()
3339
}
40+
41+
logger.debug("Incremented logger to $metricsCount")
3442
}
3543

3644
fun decrementCounter() {
37-
counter.decrementAndGet()
45+
val curr = counter.decrementAndGet()
46+
logger.debug("Decremented logger to $curr")
3847
}
48+
49+
@VisibleForTesting(otherwise = VisibleForTesting.NONE)
50+
fun resetCounter() {
51+
counter.set(0)
52+
}
53+
54+
@VisibleForTesting(otherwise = VisibleForTesting.NONE) fun count(): Int = counter.get()
3955
}

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

Lines changed: 15 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -100,13 +100,19 @@ public void initializeGaugeMetadataManager(Context appContext) {
100100

101101
@Override
102102
public void onUpdateAppState(ApplicationProcessState applicationProcessState) {
103-
this.applicationProcessState = applicationProcessState;
104-
103+
// If it isn't a verbose session (or unset) update the app state and return.
105104
if (session == null || !session.isVerbose()) {
105+
this.applicationProcessState = applicationProcessState;
106106
return;
107107
}
108108

109-
// If it's a verbose session, start collecting gauges for the new app state.
109+
// Log existing gauges to the current app state.
110+
logGaugeMetrics();
111+
112+
// Update App State.
113+
this.applicationProcessState = applicationProcessState;
114+
115+
// Start collecting gauges for the new app state.
110116
startCollectingGauges(this.applicationProcessState, session.getTimer());
111117
}
112118

@@ -132,6 +138,7 @@ public void startCollectingGauges(PerfSession session) {
132138
stopCollectingGauges();
133139
}
134140

141+
// TODO(b/394127311): Explore always setting the app state as FOREGROUND.
135142
ApplicationProcessState gaugeCollectionApplicationProcessState = applicationProcessState;
136143
if (gaugeCollectionApplicationProcessState
137144
== ApplicationProcessState.APPLICATION_PROCESS_STATE_UNKNOWN) {
@@ -419,4 +426,9 @@ private long getMemoryGaugeCollectionFrequencyMs(
419426
return memoryGaugeCollectionFrequency;
420427
}
421428
}
429+
430+
@VisibleForTesting
431+
void setApplicationProcessState(ApplicationProcessState applicationProcessState) {
432+
this.applicationProcessState = applicationProcessState;
433+
}
422434
}

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

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,9 +25,11 @@
2525
import com.google.firebase.perf.config.ConfigResolver;
2626
import com.google.firebase.perf.session.PerfSession;
2727
import com.google.firebase.perf.session.SessionManager;
28+
import com.google.firebase.perf.session.gauges.GaugeCounter;
2829
import com.google.firebase.perf.util.ImmutableBundle;
2930
import org.junit.After;
3031
import org.junit.Before;
32+
import org.junit.BeforeClass;
3133
import org.robolectric.shadows.ShadowPackageManager;
3234

3335
public class FirebasePerformanceTestBase {
@@ -54,6 +56,12 @@ public class FirebasePerformanceTestBase {
5456

5557
protected Context appContext;
5658

59+
@BeforeClass
60+
public static void setUpBeforeClass() {
61+
// TODO(b/394127311): Explore removing this.
62+
GaugeCounter.INSTANCE.resetCounter();
63+
}
64+
5765
@Before
5866
public void setUpFirebaseApp() {
5967
appContext = ApplicationProvider.getApplicationContext();

0 commit comments

Comments
 (0)