Skip to content

Commit

Permalink
Disable the metrics ping in certain tests
Browse files Browse the repository at this point in the history
We don't expect to receive a metrics ping in these tests.
An "easy" way to prevent it is to mark a metrics ping as recently sent.
  • Loading branch information
badboy committed Aug 5, 2020
1 parent 2643d1b commit 1630741
Show file tree
Hide file tree
Showing 2 changed files with 43 additions and 16 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
package mozilla.telemetry.glean

import android.content.Context
import android.os.SystemClock
import androidx.lifecycle.Lifecycle
import androidx.lifecycle.LifecycleOwner
import androidx.lifecycle.LifecycleRegistry
Expand All @@ -27,6 +28,7 @@ import mozilla.telemetry.glean.private.NoExtraKeys
import mozilla.telemetry.glean.private.NoReasonCodes
import mozilla.telemetry.glean.private.PingType
import mozilla.telemetry.glean.private.StringMetricType
import mozilla.telemetry.glean.private.TimeUnit
import mozilla.telemetry.glean.rust.LibGleanFFI
import mozilla.telemetry.glean.rust.toBoolean
import mozilla.telemetry.glean.rust.toByte
Expand All @@ -35,6 +37,8 @@ import mozilla.telemetry.glean.scheduler.PingUploadWorker
import mozilla.telemetry.glean.testing.GleanTestRule
import mozilla.telemetry.glean.utils.getLanguageFromLocale
import mozilla.telemetry.glean.utils.getLocaleTag
import mozilla.telemetry.glean.scheduler.MetricsPingScheduler
import mozilla.telemetry.glean.utils.getISOTimeString
import org.json.JSONObject
import org.junit.After
import org.junit.Assert.assertEquals
Expand All @@ -52,7 +56,7 @@ import org.robolectric.shadows.ShadowLog
import java.io.File
import java.util.Calendar
import java.util.Locale
import java.util.concurrent.TimeUnit
import java.util.concurrent.TimeUnit as AndroidTimeUnit

@ObsoleteCoroutinesApi
@RunWith(AndroidJUnit4::class)
Expand Down Expand Up @@ -84,7 +88,7 @@ class GleanTest {
// we are only expecting one baseline ping.
assertEquals(server.requestCount, 1)

val request = server.takeRequest(20L, TimeUnit.SECONDS)
val request = server.takeRequest(20L, AndroidTimeUnit.SECONDS)
val docType = request.path.split("/")[3]
assertEquals("baseline", docType)
}
Expand All @@ -102,7 +106,7 @@ class GleanTest {
// Now trigger it to upload
triggerWorkManager(context)

val request = server.takeRequest(20L, TimeUnit.SECONDS)
val request = server.takeRequest(20L, AndroidTimeUnit.SECONDS)
assertEquals(request.getHeader("X-Debug-ID"), "this-ping-is-tagged")
}

Expand Down Expand Up @@ -208,7 +212,7 @@ class GleanTest {
triggerWorkManager(context)

for (i in 0..3) {
val request = server.takeRequest(20L, TimeUnit.SECONDS)
val request = server.takeRequest(20L, AndroidTimeUnit.SECONDS)
val docType = request.path.split("/")[3]
val json = JSONObject(request.getPlainBody())
checkPingSchema(json)
Expand Down Expand Up @@ -268,7 +272,7 @@ class GleanTest {
// Trigger worker task to upload the pings in the background
triggerWorkManager(context)

val request = server.takeRequest(20L, TimeUnit.SECONDS)
val request = server.takeRequest(20L, AndroidTimeUnit.SECONDS)
val docType = request.path.split("/")[3]
assertEquals("The received ping must be a 'baseline' ping", "baseline", docType)

Expand Down Expand Up @@ -472,7 +476,7 @@ class GleanTest {
triggerWorkManager(context)

// Validate the received data.
val request = server.takeRequest(20L, TimeUnit.SECONDS)
val request = server.takeRequest(20L, AndroidTimeUnit.SECONDS)
val docType = request.path.split("/")[3]
assertEquals(pingName, docType)

Expand Down Expand Up @@ -604,7 +608,7 @@ class GleanTest {
// Now trigger it to upload
triggerWorkManager(context)

val request = server.takeRequest(20L, TimeUnit.SECONDS)
val request = server.takeRequest(20L, AndroidTimeUnit.SECONDS)
val jsonContent = JSONObject(request.getPlainBody())
assertEquals(
110,
Expand Down Expand Up @@ -640,7 +644,7 @@ class GleanTest {
// Now trigger it to upload
triggerWorkManager(context)

val request = server.takeRequest(20L, TimeUnit.SECONDS)
val request = server.takeRequest(20L, AndroidTimeUnit.SECONDS)
val docType = request.path.split("/")[3]
assertEquals("deletion-request", docType)
}
Expand Down Expand Up @@ -670,6 +674,15 @@ class GleanTest {

@Test
fun `test sending of startup baseline ping with application lifetime metric`() {
// Let's tell the MPS that we just sent a metrics ping.
// This way we won't trigger it again here.
val fakeNowDoNotSend = Calendar.getInstance()
fakeNowDoNotSend.clear()
fakeNowDoNotSend.set(2015, 6, 11, 4, 0, 0)
SystemClock.setCurrentTimeMillis(fakeNowDoNotSend.timeInMillis)
val fakeMpsSetter = spy(MetricsPingScheduler(context))
fakeMpsSetter.updateSentDate(getISOTimeString(fakeNowDoNotSend, truncateTo = TimeUnit.Day))

// Set the dirty flag.
LibGleanFFI.INSTANCE.glean_set_dirty_flag(true.toByte())

Expand All @@ -693,7 +706,7 @@ class GleanTest {
// Trigger worker task to upload the pings in the background
triggerWorkManager(context)

val request = server.takeRequest(20L, TimeUnit.SECONDS)
val request = server.takeRequest(20L, AndroidTimeUnit.SECONDS)
val docType = request.path.split("/")[3]
assertEquals("The received ping must be a 'baseline' ping", "baseline", docType)

Expand Down Expand Up @@ -815,7 +828,7 @@ class GleanTest {
// Trigger it to upload
triggerWorkManager(context)

val request = server.takeRequest(20L, TimeUnit.SECONDS)
val request = server.takeRequest(20L, AndroidTimeUnit.SECONDS)
assertEquals(request.getHeader("X-Debug-ID"), "valid-tag")
}

Expand Down Expand Up @@ -883,7 +896,7 @@ class GleanTest {
triggerWorkManager(context)

// Validate the received data.
val request = server.takeRequest(20L, TimeUnit.SECONDS)
val request = server.takeRequest(20L, AndroidTimeUnit.SECONDS)
val docType = request.path.split("/")[3]
assertEquals("deletion-request", docType)
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,10 +21,11 @@ import mozilla.telemetry.glean.getContextWithMockedInfo
import mozilla.telemetry.glean.getMockWebServer
import mozilla.telemetry.glean.resetGlean
import java.lang.NullPointerException
import java.util.concurrent.TimeUnit
import java.util.concurrent.TimeUnit as AndroidTimeUnit
import mozilla.telemetry.glean.testing.ErrorType
import mozilla.telemetry.glean.testing.GleanTestRule
import mozilla.telemetry.glean.triggerWorkManager
import mozilla.telemetry.glean.scheduler.MetricsPingScheduler
import org.json.JSONObject
import org.junit.Assert.assertEquals
import org.junit.Assert.assertFalse
Expand All @@ -34,6 +35,9 @@ import org.junit.Assert.fail
import org.junit.Rule
import org.junit.Test
import org.junit.runner.RunWith
import java.util.Calendar
import org.mockito.Mockito.spy
import mozilla.telemetry.glean.utils.getISOTimeString

// Declared here, since Kotlin can not declare nested enum classes.
enum class clickKeys {
Expand Down Expand Up @@ -264,7 +268,7 @@ class EventMetricTypeTest {

triggerWorkManager(context)

val request = server.takeRequest(1L, TimeUnit.SECONDS)
val request = server.takeRequest(1L, AndroidTimeUnit.SECONDS)
assertEquals("POST", request.method)
val applicationId = "mozilla-telemetry-glean-test"
assert(
Expand All @@ -289,9 +293,19 @@ class EventMetricTypeTest {
@Suppress("LongMethod")
@Test
fun `flush queued events on startup and correctly handle pre-init events`() {
val context = getContextWithMockedInfo()

// Let's tell the MPS that we just sent a metrics ping.
// This way we won't trigger it again here.
val fakeNowDoNotSend = Calendar.getInstance()
fakeNowDoNotSend.clear()
fakeNowDoNotSend.set(2015, 6, 11, 4, 0, 0)
SystemClock.setCurrentTimeMillis(fakeNowDoNotSend.timeInMillis)
val fakeMpsSetter = spy(MetricsPingScheduler(context))
fakeMpsSetter.updateSentDate(getISOTimeString(fakeNowDoNotSend, truncateTo = TimeUnit.Day))

val server = getMockWebServer()

val context = getContextWithMockedInfo()
resetGlean(
context,
Glean.configuration.copy(
Expand Down Expand Up @@ -328,7 +342,7 @@ class EventMetricTypeTest {
// Trigger worker task to upload the pings in the background
triggerWorkManager(context)

var request = server.takeRequest(20L, TimeUnit.SECONDS)
var request = server.takeRequest(20L, AndroidTimeUnit.SECONDS)
var pingJsonData = request.getPlainBody()
var pingJson = JSONObject(pingJsonData)
checkPingSchema(pingJson)
Expand All @@ -353,7 +367,7 @@ class EventMetricTypeTest {
// Trigger worker task to upload the pings in the background
triggerWorkManager(context)

request = server.takeRequest(20L, TimeUnit.SECONDS)
request = server.takeRequest(20L, AndroidTimeUnit.SECONDS)
pingJsonData = request.getPlainBody()
pingJson = JSONObject(pingJsonData)
checkPingSchema(pingJson)
Expand Down

0 comments on commit 1630741

Please sign in to comment.