Skip to content

Commit 450ced4

Browse files
committed
Attempt to debug failing tests
1 parent fd0282d commit 450ced4

File tree

3 files changed

+28
-28
lines changed

3 files changed

+28
-28
lines changed

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

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414

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

17+
import com.google.firebase.perf.logging.AndroidLogger
1718
import java.util.concurrent.atomic.AtomicInteger
1819

1920
/**
@@ -23,6 +24,7 @@ import java.util.concurrent.atomic.AtomicInteger
2324
object GaugeCounter {
2425
private const val MAX_METRIC_COUNT = 25
2526
private val counter = AtomicInteger(0)
27+
private val logger = AndroidLogger.getInstance()
2628
// TODO(b/394127311): Setting this as a var for a unit test. Refactor it.
2729
var gaugeManager: GaugeManager = GaugeManager.getInstance()
2830

@@ -32,9 +34,20 @@ object GaugeCounter {
3234
if (metricsCount >= MAX_METRIC_COUNT) {
3335
gaugeManager.logGaugeMetrics()
3436
}
37+
38+
logger.debug("Incremented logger to $metricsCount")
3539
}
3640

3741
fun decrementCounter() {
38-
counter.decrementAndGet()
42+
val curr = counter.decrementAndGet()
43+
logger.debug("Decremented logger to $curr")
44+
}
45+
46+
// TODO: Add annotation to only call from tests
47+
fun resetCounter() {
48+
counter.set(0)
3949
}
50+
51+
// TODO: Add annotation to only call from tests
52+
fun count(): Int = counter.get()
4053
}

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,12 +25,20 @@
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;
33+
import org.robolectric.shadows.ShadowLog;
3134
import org.robolectric.shadows.ShadowPackageManager;
3235

3336
public class FirebasePerformanceTestBase {
37+
@BeforeClass
38+
public static void setUpBeforeClass() {
39+
ShadowLog.stream = System.out;
40+
GaugeCounter.INSTANCE.resetCounter();
41+
}
3442

3543
/**
3644
* The following values are needed by Firebase to identify the project and application that all

firebase-perf/src/test/java/com/google/firebase/perf/session/gauges/GaugeManagerTest.java

Lines changed: 6 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -331,7 +331,7 @@ public void stopCollectingGauges_invalidGaugeCollectionFrequency_appInForeground
331331
}
332332

333333
@Test
334-
public void testGaugeCounterStartsAJobToConsumeTheGeneratedMetrics() {
334+
public void testGaugeCounterStartsAJobToConsumeTheGeneratedMetrics() throws InterruptedException {
335335
PerfSession fakeSession = createTestSession(1);
336336
testGaugeManager.setApplicationProcessState(ApplicationProcessState.FOREGROUND);
337337
testGaugeManager.startCollectingGauges(fakeSession);
@@ -504,23 +504,17 @@ public void testStopCollectingGaugesCreatesOneLastJobToConsumeAnyPendingMetrics(
504504
testGaugeManager.stopCollectingGauges();
505505
assertThat(fakeScheduledExecutorService.isEmpty()).isFalse();
506506

507-
CpuMetricReading fakeCpuMetricReading = createFakeCpuMetricReading(200, 100);
508-
fakeCpuGaugeCollector.cpuMetricReadings.add(fakeCpuMetricReading);
509-
510-
AndroidMemoryReading fakeMemoryMetricReading =
511-
createFakeAndroidMetricReading(/* currentUsedAppJavaHeapMemoryKb= */ 23454678);
512-
fakeMemoryGaugeCollector.memoryMetricReadings.add(fakeMemoryMetricReading);
507+
generateMetricsAndIncrementCounter(2);
513508

514509
assertThat(fakeScheduledExecutorService.getDelayToNextTask(TimeUnit.MILLISECONDS))
515510
.isEqualTo(TIME_TO_WAIT_BEFORE_FLUSHING_GAUGES_QUEUE_MS);
516511

517512
fakeScheduledExecutorService.simulateSleepExecutingAtMostOneTask();
518513
GaugeMetric recordedGaugeMetric =
519514
getLastRecordedGaugeMetric(ApplicationProcessState.FOREGROUND);
520-
assertThatCpuGaugeMetricWasSentToTransport(
521-
testSessionId(1), recordedGaugeMetric, fakeCpuMetricReading);
522-
assertThatMemoryGaugeMetricWasSentToTransport(
523-
testSessionId(1), recordedGaugeMetric, fakeMemoryMetricReading);
515+
assertThat(recordedGaugeMetric.getSessionId()).isEqualTo(testSessionId(1));
516+
517+
assertThat(GaugeCounter.INSTANCE.count()).isEqualTo(0);
524518
}
525519

526520
@Test
@@ -587,6 +581,7 @@ public void testLogGaugeMetadataLogsAfterApplicationContextIsSet() {
587581
GaugeMetadata recordedGaugeMetadata = recordedGaugeMetric.getGaugeMetadata();
588582

589583
assertThat(recordedGaugeMetric.getSessionId()).isEqualTo(testSessionId(1));
584+
assertThat(recordedGaugeMetadata).isNotEqualTo(GaugeMetadata.getDefaultInstance());
590585
}
591586

592587
@Test
@@ -655,20 +650,4 @@ private GaugeMetric getLastRecordedGaugeMetric(
655650
when(mockTransportManager.isInitialized()).thenReturn(true);
656651
return argMetric.getValue();
657652
}
658-
659-
private void assertThatCpuGaugeMetricWasSentToTransport(
660-
String sessionId, GaugeMetric recordedGaugeMetric, CpuMetricReading... cpuMetricReadings) {
661-
assertThat(recordedGaugeMetric.getSessionId()).isEqualTo(sessionId);
662-
assertThat(recordedGaugeMetric.getCpuMetricReadingsList())
663-
.containsAtLeastElementsIn(cpuMetricReadings);
664-
}
665-
666-
private void assertThatMemoryGaugeMetricWasSentToTransport(
667-
String sessionId,
668-
GaugeMetric recordedGaugeMetric,
669-
AndroidMemoryReading... androidMetricReadings) {
670-
assertThat(recordedGaugeMetric.getSessionId()).isEqualTo(sessionId);
671-
assertThat(recordedGaugeMetric.getAndroidMemoryReadingsList())
672-
.containsAtLeastElementsIn(androidMetricReadings);
673-
}
674653
}

0 commit comments

Comments
 (0)