Commit 152dd3fb authored by Denys M's avatar Denys M Committed by Sebastian Kaspari
Browse files

For #2346. Enable kotlin warningsAsErrors for `service-glean` module.

parent 6971977e
......@@ -13,7 +13,6 @@ object KotlinCompiler {
@JvmStatic
val projectsWithWarningsAsErrorsDisabled = setOf(
"browser-domains",
"feature-prompts",
"service-glean"
"feature-prompts"
)
}
......@@ -47,7 +47,6 @@ data class EventMetricType<ExtraKeysEnum : Enum<ExtraKeysEnum>>(
* identifiers. This is used for events where additional richer context is needed.
* The maximum length for values is defined by [MAX_LENGTH_EXTRA_KEY_VALUE]
*/
@Suppress("NestedBlockDepth")
fun record(extra: Map<ExtraKeysEnum, String>? = null) {
if (!shouldRecord(logger)) {
return
......@@ -57,26 +56,7 @@ data class EventMetricType<ExtraKeysEnum : Enum<ExtraKeysEnum>>(
// might get executed.
val monotonicElapsed = SystemClock.elapsedRealtime()
// Convert the extra key enums to strings before passing to the storage engine
val extraStrings = extra?.let { extra ->
// There are two extra "keys" in play here:
// 1. The Kotlin enumeration names, in CamelCase
// 2. The keys sent in the ping, in snake_case
// Here we need to get (2) to send in the ping.
if (extra.size > 0) {
val result = mutableMapOf<String, String>()
for ((k, v) in extra) {
allowedExtraKeys.getOrNull(k.ordinal)?.let { stringKey ->
result[stringKey] = v
} ?: run {
logger.debug("No string value for enum ${k.ordinal}")
}
}
result
} else {
null
}
}
val extraStrings = extra?.convertAllowedToStrings(allowedExtraKeys)
@Suppress("EXPERIMENTAL_API_USAGE")
Dispatchers.API.launch {
......@@ -89,6 +69,22 @@ data class EventMetricType<ExtraKeysEnum : Enum<ExtraKeysEnum>>(
}
}
// Convert the extra key enums to strings before passing to the storage engine
// There are two extra "keys" in play here:
// 1. The Kotlin enumeration names, in CamelCase
// 2. The keys sent in the ping, in snake_case
// Here we need to get (2) to send in the ping.
private fun Map<ExtraKeysEnum, String>.convertAllowedToStrings(allowedKeys: List<String>): Map<String, String>? =
mapNotNull { (k, v) ->
val stringKey = allowedKeys.getOrNull(k.ordinal)
if (stringKey != null) {
stringKey to v
} else run {
logger.debug("No string value for enum ${k.ordinal}")
null
}
}.toMap()
/**
* Tests whether a value is stored for the metric for testing purposes only. This function will
* attempt to await the last task (if any) writing to the the metric's storage engine before
......
......@@ -22,22 +22,21 @@ import mozilla.components.service.glean.private.Lifetime
import mozilla.components.service.glean.private.NoExtraKeys
import mozilla.components.service.glean.private.PingType
import mozilla.components.service.glean.private.StringMetricType
import mozilla.components.service.glean.private.TimeUnit as GleanTimeUnit
import mozilla.components.service.glean.private.UuidMetricType
import mozilla.components.service.glean.storages.StringsStorageEngine
import mozilla.components.service.glean.scheduler.GleanLifecycleObserver
import mozilla.components.service.glean.scheduler.PingUploadWorker
import mozilla.components.service.glean.storages.StorageEngineManager
import mozilla.components.service.glean.storages.StringsStorageEngine
import mozilla.components.service.glean.utils.getLanguageFromLocale
import mozilla.components.service.glean.utils.getLocaleTag
import org.json.JSONObject
import org.junit.Assert.assertEquals
import org.junit.Assert.assertFalse
import org.junit.Assert.assertNotEquals
import org.junit.Assert.assertNotNull
import org.junit.Assert.assertNull
import org.junit.Assert.assertSame
import org.junit.Assert.assertTrue
import org.junit.Assert.assertFalse
import org.junit.Assert.assertNotEquals
import org.junit.Before
import org.junit.Test
import org.junit.runner.RunWith
......@@ -49,11 +48,12 @@ import org.robolectric.RobolectricTestRunner
import java.io.BufferedReader
import java.io.File
import java.io.FileReader
import java.lang.AssertionError
import java.time.Instant
import java.util.Date
import java.util.Locale
import java.util.UUID
import java.util.concurrent.TimeUnit
import mozilla.components.service.glean.private.TimeUnit as GleanTimeUnit
@ObsoleteCoroutinesApi
@ExperimentalCoroutinesApi
......@@ -373,7 +373,8 @@ class GleanTest {
sendInPings = listOf("glean_ping_info"),
timeUnit = GleanTimeUnit.Day
)
firstRunDateMetric.set(Date(2200, 1, 1))
firstRunDateMetric.set(Date.from(
Instant.parse("2200-01-01T00:00:00.00Z")))
assertTrue(GleanInternalMetrics.clientId.testHasValue())
assertTrue(GleanInternalMetrics.firstRunDate.testHasValue())
......
......@@ -4,22 +4,19 @@
package mozilla.components.service.glean.private
import android.content.Context
import android.os.SystemClock
import androidx.test.core.app.ApplicationProvider
import kotlinx.coroutines.ExperimentalCoroutinesApi
import kotlinx.coroutines.ObsoleteCoroutinesApi
import mozilla.components.service.glean.Glean
import mozilla.components.service.glean.resetGlean
import org.junit.Assert.assertEquals
import org.junit.Assert.assertTrue
import org.junit.Assert.assertFalse
import org.junit.Assert.assertTrue
import org.junit.Assert.fail
import org.junit.Test
import org.junit.Before
import org.junit.Test
import org.junit.runner.RunWith
import org.robolectric.RobolectricTestRunner
import java.lang.NullPointerException
// Declared here, since Kotlin can not declare nested enum classes.
enum class clickKeys {
......@@ -184,8 +181,6 @@ class EventMetricTypeTest {
@Test
fun `events should not record when upload is disabled`() {
val context = ApplicationProvider.getApplicationContext<Context>()
val eventMetric = EventMetricType<testNameKeys>(
disabled = false,
category = "ui",
......
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment