Skip to content

Commit

Permalink
Add utility to delay the metrics ping in tests
Browse files Browse the repository at this point in the history
This avoids the need to deal with an "overdue" metrics ping on start.
  • Loading branch information
badboy committed Aug 19, 2020
1 parent 1be5d8f commit 38987f8
Show file tree
Hide file tree
Showing 5 changed files with 49 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
Expand Down Expand Up @@ -183,6 +185,7 @@ class GleanTest {
)

val context = getContextWithMockedInfo()
delayMetricsPing(context)
resetGlean(context, Glean.configuration.copy(
serverEndpoint = "http://" + server.hostName + ":" + server.port
))
Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -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
))
Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -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,
Expand All @@ -862,7 +872,6 @@ class GleanTest {
)

val server = getMockWebServer()
val context = getContextWithMockedInfo()
val config = Glean.configuration.copy(
serverEndpoint = "http://" + server.hostName + ":" + server.port
)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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

Expand Down Expand Up @@ -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.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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)
Expand All @@ -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)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -292,6 +293,7 @@ class EventMetricTypeTest {
val server = getMockWebServer()

val context = getContextWithMockedInfo()
delayMetricsPing(context)
resetGlean(
context,
Glean.configuration.copy(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
))
Expand Down Expand Up @@ -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
))
Expand Down Expand Up @@ -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
))
Expand Down Expand Up @@ -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
))
Expand Down Expand Up @@ -206,6 +211,7 @@ class PingTypeTest {
)

val context = getContextWithMockedInfo()
delayMetricsPing(context)
resetGlean(context, Glean.configuration.copy(
serverEndpoint = "http://" + server.hostName + ":" + server.port
))
Expand Down

0 comments on commit 38987f8

Please sign in to comment.