From 38987f82a483c4aee219d61708a5df2cb96c04ec Mon Sep 17 00:00:00 2001 From: Jan-Erik Rediger Date: Wed, 19 Aug 2020 13:17:21 +0200 Subject: [PATCH] Add utility to delay the metrics ping in tests This avoids the need to deal with an "overdue" metrics ping on start. --- .../java/mozilla/telemetry/glean/GleanTest.kt | 11 +++++++- .../java/mozilla/telemetry/glean/TestUtil.kt | 28 +++++++++++++++++++ .../telemetry/glean/pings/CustomPingTest.kt | 3 ++ .../glean/private/EventMetricTypeTest.kt | 2 ++ .../telemetry/glean/private/PingTypeTest.kt | 6 ++++ 5 files changed, 49 insertions(+), 1 deletion(-) diff --git a/glean-core/android/src/test/java/mozilla/telemetry/glean/GleanTest.kt b/glean-core/android/src/test/java/mozilla/telemetry/glean/GleanTest.kt index accb7c139c..1e6faa2a45 100644 --- a/glean-core/android/src/test/java/mozilla/telemetry/glean/GleanTest.kt +++ b/glean-core/android/src/test/java/mozilla/telemetry/glean/GleanTest.kt @@ -71,6 +71,7 @@ class GleanTest { // New from glean-core. @Test fun `send a ping`() { + delayMetricsPing(context) val server = getMockWebServer() resetGlean(context, Glean.configuration.copy( serverEndpoint = "http://" + server.hostName + ":" + server.port @@ -91,6 +92,7 @@ class GleanTest { @Test fun `X-Debug-ID header is correctly added when debug view tag is set`() { + delayMetricsPing(context) val server = getMockWebServer() resetGlean(context, Glean.configuration.copy( serverEndpoint = "http://" + server.hostName + ":" + server.port @@ -183,6 +185,7 @@ class GleanTest { ) val context = getContextWithMockedInfo() + delayMetricsPing(context) resetGlean(context, Glean.configuration.copy( serverEndpoint = "http://" + server.hostName + ":" + server.port )) @@ -260,6 +263,7 @@ class GleanTest { // Restart glean and don't clear the stores. val server = getMockWebServer() val context = getContextWithMockedInfo() + delayMetricsPing(context) resetGlean(context, Glean.configuration.copy( serverEndpoint = "http://" + server.hostName + ":" + server.port ), false) @@ -434,6 +438,7 @@ class GleanTest { val server = getMockWebServer() val context = getContextWithMockedInfo() + delayMetricsPing(context) resetGlean(context, Glean.configuration.copy( serverEndpoint = "http://" + server.hostName + ":" + server.port )) @@ -576,6 +581,7 @@ class GleanTest { @Test fun `overflowing the task queue records telemetry`() { + delayMetricsPing(context) val server = getMockWebServer() Dispatchers.API.setTestingMode(true) Dispatchers.API.setTaskQueueing(true) @@ -685,6 +691,7 @@ class GleanTest { // Restart glean and don't clear the stores. val server = getMockWebServer() val context = getContextWithMockedInfo() + delayMetricsPing(context) resetGlean(context, Glean.configuration.copy( serverEndpoint = "http://" + server.hostName + ":" + server.port ), false) @@ -838,6 +845,9 @@ class GleanTest { // This test relies on Glean not being initialized, we do that ourselves. Glean.testDestroyGleanHandle() + val context = getContextWithMockedInfo() + delayMetricsPing(context) + // This test relies on testing mode to be disabled, since we need to prove the // real-world async behaviour of this. // We don't need to care about clearing it, @@ -862,7 +872,6 @@ class GleanTest { ) val server = getMockWebServer() - val context = getContextWithMockedInfo() val config = Glean.configuration.copy( serverEndpoint = "http://" + server.hostName + ":" + server.port ) diff --git a/glean-core/android/src/test/java/mozilla/telemetry/glean/TestUtil.kt b/glean-core/android/src/test/java/mozilla/telemetry/glean/TestUtil.kt index e1c8bece79..c2574ad9f4 100644 --- a/glean-core/android/src/test/java/mozilla/telemetry/glean/TestUtil.kt +++ b/glean-core/android/src/test/java/mozilla/telemetry/glean/TestUtil.kt @@ -7,6 +7,7 @@ package mozilla.telemetry.glean import android.content.Context +import android.os.SystemClock import android.content.pm.PackageInfo import android.content.pm.PackageManager import androidx.test.core.app.ApplicationProvider @@ -22,15 +23,20 @@ import org.mockito.Mockito import mozilla.telemetry.glean.config.Configuration import mozilla.telemetry.glean.scheduler.PingUploadWorker import mozilla.telemetry.glean.private.PingTypeBase +import mozilla.telemetry.glean.private.TimeUnit import mozilla.telemetry.glean.utils.decompressGZIP +import mozilla.telemetry.glean.utils.getISOTimeString +import mozilla.telemetry.glean.scheduler.MetricsPingScheduler import okhttp3.mockwebserver.Dispatcher import okhttp3.mockwebserver.MockResponse import okhttp3.mockwebserver.MockWebServer import okhttp3.mockwebserver.RecordedRequest import org.junit.Assert import org.mockito.ArgumentCaptor +import org.mockito.Mockito.spy import org.robolectric.shadows.ShadowLog import java.io.ByteArrayInputStream +import java.util.Calendar import java.util.UUID import java.util.concurrent.ExecutionException @@ -257,6 +263,28 @@ fun RecordedRequest.getPlainBody(): String { } } +/** + * Ensure no overdue metrics ping is triggered on `Glean.initialize`. + * + * This sets a fake date and time and changes the metrics ping scheduler + * to assume that now was the last time a metrics ping was sent. + * This can be used when tests should receive other pings, + * but don't want to deal with a potential overdue metrics ping first + */ +internal fun delayMetricsPing(context: Context) { + // Set the current system time to a known datetime. + val fakeNow = Calendar.getInstance() + fakeNow.clear() + fakeNow.set(2015, 6, 11, 2, 0, 0) + SystemClock.setCurrentTimeMillis(fakeNow.timeInMillis) + + // Set the last sent date to yesterday. + val mpsSpy = + spy(MetricsPingScheduler(context)) + + mpsSpy.updateSentDate(getISOTimeString(fakeNow, truncateTo = TimeUnit.Day)) +} + // The following Mockito fixups are copied over from support-test (Matchers.kt) from // Android-Components. We copied them over since A-C uses the Glean SDK, and we don't // want a dependency on A-C. diff --git a/glean-core/android/src/test/java/mozilla/telemetry/glean/pings/CustomPingTest.kt b/glean-core/android/src/test/java/mozilla/telemetry/glean/pings/CustomPingTest.kt index ffaeddf2c2..7f58b3e220 100644 --- a/glean-core/android/src/test/java/mozilla/telemetry/glean/pings/CustomPingTest.kt +++ b/glean-core/android/src/test/java/mozilla/telemetry/glean/pings/CustomPingTest.kt @@ -15,6 +15,7 @@ import mozilla.telemetry.glean.resetGlean import mozilla.telemetry.glean.testing.GleanTestRule import mozilla.telemetry.glean.triggerWorkManager import mozilla.telemetry.glean.utils.tryGetLong +import mozilla.telemetry.glean.delayMetricsPing import org.junit.Assert.assertEquals import org.junit.Rule import org.junit.Test @@ -43,6 +44,7 @@ class CustomPingTest { val server = getMockWebServer() + delayMetricsPing(context) resetGlean(context, Glean.configuration.copy( serverEndpoint = "http://" + server.hostName + ":" + server.port ), clearStores = true, uploadEnabled = true) @@ -67,6 +69,7 @@ class CustomPingTest { fun `multiple pings in one go`() { val server = getMockWebServer() + delayMetricsPing(context) resetGlean(context, Glean.configuration.copy( serverEndpoint = "http://" + server.hostName + ":" + server.port ), clearStores = true, uploadEnabled = true) diff --git a/glean-core/android/src/test/java/mozilla/telemetry/glean/private/EventMetricTypeTest.kt b/glean-core/android/src/test/java/mozilla/telemetry/glean/private/EventMetricTypeTest.kt index 8d0cdefa2e..13f7dca8c9 100644 --- a/glean-core/android/src/test/java/mozilla/telemetry/glean/private/EventMetricTypeTest.kt +++ b/glean-core/android/src/test/java/mozilla/telemetry/glean/private/EventMetricTypeTest.kt @@ -20,6 +20,7 @@ import mozilla.telemetry.glean.Dispatchers import mozilla.telemetry.glean.getContextWithMockedInfo import mozilla.telemetry.glean.getMockWebServer import mozilla.telemetry.glean.resetGlean +import mozilla.telemetry.glean.delayMetricsPing import java.lang.NullPointerException import java.util.concurrent.TimeUnit import mozilla.telemetry.glean.testing.ErrorType @@ -292,6 +293,7 @@ class EventMetricTypeTest { val server = getMockWebServer() val context = getContextWithMockedInfo() + delayMetricsPing(context) resetGlean( context, Glean.configuration.copy( diff --git a/glean-core/android/src/test/java/mozilla/telemetry/glean/private/PingTypeTest.kt b/glean-core/android/src/test/java/mozilla/telemetry/glean/private/PingTypeTest.kt index 6ba0f04732..19a7b3243e 100644 --- a/glean-core/android/src/test/java/mozilla/telemetry/glean/private/PingTypeTest.kt +++ b/glean-core/android/src/test/java/mozilla/telemetry/glean/private/PingTypeTest.kt @@ -16,6 +16,7 @@ import mozilla.telemetry.glean.resetGlean import mozilla.telemetry.glean.scheduler.PingUploadWorker import mozilla.telemetry.glean.testing.GleanTestRule import mozilla.telemetry.glean.triggerWorkManager +import mozilla.telemetry.glean.delayMetricsPing import org.json.JSONObject import org.junit.Assert.assertEquals import org.junit.Assert.assertFalse @@ -38,6 +39,7 @@ class PingTypeTest { val server = getMockWebServer() val context = getContextWithMockedInfo() + delayMetricsPing(context) resetGlean(context, Glean.configuration.copy( serverEndpoint = "http://" + server.hostName + ":" + server.port )) @@ -78,6 +80,7 @@ class PingTypeTest { val server = getMockWebServer() val context = getContextWithMockedInfo() + delayMetricsPing(context) resetGlean(context, Glean.configuration.copy( serverEndpoint = "http://" + server.hostName + ":" + server.port )) @@ -118,6 +121,7 @@ class PingTypeTest { val server = getMockWebServer() val context = getContextWithMockedInfo() + delayMetricsPing(context) resetGlean(context, Glean.configuration.copy( serverEndpoint = "http://" + server.hostName + ":" + server.port )) @@ -158,6 +162,7 @@ class PingTypeTest { val server = getMockWebServer() val context = getContextWithMockedInfo() + delayMetricsPing(context) resetGlean(context, Glean.configuration.copy( serverEndpoint = "http://" + server.hostName + ":" + server.port )) @@ -206,6 +211,7 @@ class PingTypeTest { ) val context = getContextWithMockedInfo() + delayMetricsPing(context) resetGlean(context, Glean.configuration.copy( serverEndpoint = "http://" + server.hostName + ":" + server.port ))