Bug 1552820 - Expose boolean test API and re-enable metric tests (#78)

Bug 1552820 - Expose boolean test API and re-enable metric tests
This commit is contained in:
Jan-Erik Rediger 2019-05-20 15:48:24 +02:00 коммит произвёл GitHub
Родитель bb9ead2256 00a9020f4e
Коммит a84711698c
Не найден ключ, соответствующий данной подписи
Идентификатор ключа GPG: 4AEE18F83AFDEB23
6 изменённых файлов: 78 добавлений и 13 удалений

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

@ -11,6 +11,9 @@ import mozilla.telemetry.glean.rust.LibGleanFFI
import mozilla.telemetry.glean.rust.toByte
import mozilla.telemetry.glean.rust.RustError
import mozilla.telemetry.glean.Dispatchers
import mozilla.telemetry.glean.rust.toBoolean
class BooleanMetricType(
disabled: Boolean,
category: String,
@ -61,8 +64,11 @@ class BooleanMetricType(
*/
@VisibleForTesting(otherwise = VisibleForTesting.NONE)
fun testHasValue(pingName: String = sendInPings.first()): Boolean {
assert(false, { "Testing API not implementated for BooleanMetricType" })
return false
@Suppress("EXPERIMENTAL_API_USAGE")
Dispatchers.API.assertInTestingMode()
val res = LibGleanFFI.INSTANCE.glean_boolean_test_has_value(Glean.handle, this.handle, pingName)
return res.toBoolean()
}
/**
@ -77,7 +83,12 @@ class BooleanMetricType(
*/
@VisibleForTesting(otherwise = VisibleForTesting.NONE)
fun testGetValue(pingName: String = sendInPings.first()): Boolean {
assert(false, { "Testing API not implementated for BooleanMetricType" })
return false
@Suppress("EXPERIMENTAL_API_USAGE")
Dispatchers.API.assertInTestingMode()
if (!testHasValue(pingName)) {
throw NullPointerException()
}
return LibGleanFFI.INSTANCE.glean_boolean_test_get_value(Glean.handle, this.handle, pingName).toBoolean()
}
}

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

@ -63,6 +63,10 @@ internal interface LibGleanFFI : Library {
fun glean_boolean_set(glean_handle: Long, metric_id: Long, value: Byte)
fun glean_boolean_test_get_value(glean_handle: Long, metric_id: Long, storage_name: String): Byte
fun glean_boolean_test_has_value(glean_handle: Long, metric_id: Long, storage_name: String): Byte
fun glean_counter_add(glean_handle: Long, metric_id: Long, amount: Int)
fun glean_counter_test_get_value(glean_handle: Long, metric_id: Long, storage_name: String): Int

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

@ -16,7 +16,6 @@ import mozilla.telemetry.glean.resetGlean
import org.junit.Assert.assertTrue
import org.junit.Assert.assertFalse
import org.junit.Before
import org.junit.Ignore
import org.junit.Test
import org.junit.runner.RunWith
import org.robolectric.RobolectricTestRunner
@ -32,7 +31,6 @@ class BooleanMetricTypeTest {
resetGlean()
}
@Ignore("Ignoring the test since testing API is not implemented")
@Test
fun `The API saves to its storage engine`() {
// Define a 'booleanMetric' boolean metric, which will be stored in "store1"
@ -56,7 +54,6 @@ class BooleanMetricTypeTest {
assertFalse(booleanMetric.testGetValue())
}
@Ignore("Ignoring the test since testing API is not implemented")
@Test
fun `disabled booleans must not record data`() {
// Define a 'booleanMetric' boolean metric, which will be stored in "store1". It's disabled
@ -75,7 +72,6 @@ class BooleanMetricTypeTest {
assertFalse(booleanMetric.testHasValue())
}
@Ignore("Ignoring the test since testing API is not implemented")
@Test(expected = NullPointerException::class)
fun `testGetValue() throws NullPointerException if nothing is stored`() {
// Define a 'booleanMetric' boolean metric to have an instance to call
@ -90,7 +86,6 @@ class BooleanMetricTypeTest {
booleanMetric.testGetValue()
}
@Ignore("Ignoring the test since testing API is not implemented")
@Test
fun `The API saves to secondary pings`() {
// Define a 'booleanMetric' boolean metric, which will be stored in "store1" and "store2"

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

@ -58,11 +58,19 @@ typedef const char *const *RawStringArray;
void glean_boolean_set(uint64_t glean_handle, uint64_t metric_id, uint8_t value);
void glean_counter_add(uint64_t glean_handle, uint64_t metric_id, uint64_t amount);
int32_t glean_boolean_test_get_value(uint64_t glean_handle,
uint64_t metric_id,
FfiStr storage_name);
uint64_t glean_counter_test_get_value(uint64_t glean_handle,
uint64_t metric_id,
FfiStr storage_name);
uint8_t glean_boolean_test_has_value(uint64_t glean_handle,
uint64_t metric_id,
FfiStr storage_name);
void glean_counter_add(uint64_t glean_handle, uint64_t metric_id, int32_t amount);
int32_t glean_counter_test_get_value(uint64_t glean_handle,
uint64_t metric_id,
FfiStr storage_name);
uint8_t glean_counter_test_has_value(uint64_t glean_handle,
uint64_t metric_id,

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

@ -199,6 +199,34 @@ pub extern "C" fn glean_boolean_set(glean_handle: u64, metric_id: u64, value: u8
})
}
#[no_mangle]
pub extern "C" fn glean_boolean_test_has_value(
glean_handle: u64,
metric_id: u64,
storage_name: FfiStr,
) -> u8 {
GLEAN.call_infallible(glean_handle, |glean| {
BOOLEAN_METRICS.call_infallible(metric_id, |metric| {
metric
.test_get_value(glean, storage_name.as_str())
.is_some()
})
})
}
#[no_mangle]
pub extern "C" fn glean_boolean_test_get_value(
glean_handle: u64,
metric_id: u64,
storage_name: FfiStr,
) -> u8 {
GLEAN.call_infallible(glean_handle, |glean| {
BOOLEAN_METRICS.call_infallible(metric_id, |metric| {
metric.test_get_value(glean, storage_name.as_str()).unwrap()
})
})
}
#[no_mangle]
pub extern "C" fn glean_string_set(glean_handle: u64, metric_id: u64, value: FfiStr) {
GLEAN.call_infallible(glean_handle, |glean| {

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

@ -3,6 +3,7 @@
// file, You can obtain one at https://mozilla.org/MPL/2.0/.
use crate::metrics::Metric;
use crate::storage::StorageManager;
use crate::CommonMetricData;
use crate::Glean;
@ -24,4 +25,22 @@ impl BooleanMetric {
let value = Metric::Boolean(value);
glean.storage().record(&self.meta, &value)
}
/// **Test-only API (exported for FFI purposes).**
///
/// Get the currently stored value as a boolean.
///
/// This doesn't clear the stored value.
pub fn test_get_value(&self, glean: &Glean, storage_name: &str) -> Option<bool> {
let snapshot = match StorageManager.snapshot_as_json(glean.storage(), storage_name, false) {
Some(snapshot) => snapshot,
None => return None,
};
snapshot
.as_object()
.and_then(|o| o.get("boolean"))
.and_then(|o| o.as_object())
.and_then(|o| o.get(&self.meta.identifier()))
.and_then(|o| o.as_bool())
}
}