Merge pull request #695 from mozilla/1609442-old-or-new

1609442 - Test that a startup ping contains the old version after an upgrade
This commit is contained in:
Jan-Erik Rediger 2020-02-10 17:35:49 +01:00 коммит произвёл GitHub
Родитель e1b45e84f7 075ae3f619
Коммит 560af7dab5
Не найден ключ, соответствующий данной подписи
Идентификатор ключа GPG: 4AEE18F83AFDEB23
2 изменённых файлов: 90 добавлений и 6 удалений

Просмотреть файл

@ -126,15 +126,16 @@ internal fun resetGlean(
}
/**
* Get a context that contains [PackageInfo.versionName] mocked to
* "glean.version.name".
* Get a context that contains [PackageInfo.versionName] mocked to the passed value
* or "glean.version.name" by default.
*
* @param versionName a [String] used as the display version (default: "glean.version.name").
* @return an application [Context] that can be used to init Glean
*/
internal fun getContextWithMockedInfo(): Context {
internal fun getContextWithMockedInfo(versionName: String = "glean.version.name"): Context {
val context = Mockito.spy<Context>(ApplicationProvider.getApplicationContext<Context>())
val packageInfo = Mockito.mock(PackageInfo::class.java)
packageInfo.versionName = "glean.version.name"
packageInfo.versionName = versionName
val packageManager = Mockito.mock(PackageManager::class.java)
Mockito.`when`(
packageManager.getPackageInfo(

Просмотреть файл

@ -30,6 +30,7 @@ import org.junit.Assert.assertFalse
import org.junit.Assert.assertNotNull
import org.junit.Assert.assertNull
import org.junit.Before
import org.junit.After
import org.junit.Test
import org.junit.runner.RunWith
import org.mockito.Mockito.anyBoolean
@ -56,6 +57,19 @@ class MetricsPingSchedulerTest {
Glean.enableTestingMode()
}
@After
fun cleanup() {
// Always reset Glean to clear all data.
// It might not have been initialized, but the reset functions handle that.
resetGlean(clearStores = true)
// Once all data is cleared, destroy the handle.
// Individual tests will start Glean if necessary.
Glean.testDestroyGleanHandle()
@Suppress("EXPERIMENTAL_API_USAGE")
Dispatchers.API.setTaskQueueing(true)
}
@Test
fun `milliseconds until the due time must be correctly computed`() {
val metricsPingScheduler = MetricsPingScheduler(context)
@ -429,6 +443,77 @@ class MetricsPingSchedulerTest {
verify(mpsSpy, times(1)).collectPingAndReschedule(any(), anyBoolean())
}
@Test
fun `startup ping sends old version when upgraded`() {
// Start the web-server that will receive the metrics ping.
val server = getMockWebServer()
val configuration = Configuration(
serverEndpoint = "http://" + server.hostName + ":" + server.port, logPings = true
)
val oldVersion = "version.0"
val oldContext = getContextWithMockedInfo(oldVersion)
// New version
val newContext = getContextWithMockedInfo("version.1")
try {
// Initialize Glean for the first time.
// This should pick up the old version ("version.0").
// No metric is stored, so no metrics ping will be sent.
Glean.initialize(
oldContext,
true,
configuration
)
// Create a metric and set its value. We expect this to be sent after the restart
val expectedStringMetric = StringMetricType(
disabled = false,
category = "telemetry",
lifetime = Lifetime.Ping,
name = "expected_metric",
sendInPings = listOf("metrics")
)
val expectedValue = "canary"
expectedStringMetric.set(expectedValue)
// Reset Glean.
Glean.testDestroyGleanHandle()
@Suppress("EXPERIMENTAL_API_USAGE")
Dispatchers.API.setTaskQueueing(true)
// Initialize Glean again with the new version.
// This should trigger a metrics ping after an upgrade (differing version).
Glean.initialize(
newContext,
true,
configuration
)
// Trigger worker task to upload the pings in the background.
triggerWorkManager(context)
// Wait for the metrics ping to be received.
val request = server.takeRequest(20L, AndroidTimeUnit.SECONDS)
val docType = request.path.split("/")[3]
assertEquals("The received ping must be a 'metrics' ping", "metrics", docType)
val metricsJsonData = request.body.readUtf8()
val pingJson = JSONObject(metricsJsonData)
assertEquals("The received ping must contain the old version",
oldVersion, pingJson.getJSONObject("client_info")["app_display_version"])
} finally {
server.shutdown()
// Reset Glean.
Glean.testDestroyGleanHandle()
@Suppress("EXPERIMENTAL_API_USAGE")
Dispatchers.API.setTaskQueueing(true)
}
}
@Test
fun `startupCheck must correctly handle fresh installs (after due time)`() {
// Set the current system time to a known datetime: after 4am local.
@ -476,8 +561,6 @@ class MetricsPingSchedulerTest {
// We expect the worker to be scheduled.
assertNotNull(Glean.metricsPingScheduler.timer)
resetGlean(clearStores = true)
}
@Test