2019-05-28 12:42:46 +03:00
|
|
|
// This Source Code Form is subject to the terms of the Mozilla Public
|
|
|
|
// License, v. 2.0. If a copy of the MPL was not distributed with this
|
|
|
|
// file, You can obtain one at https://mozilla.org/MPL/2.0/.
|
|
|
|
|
|
|
|
mod common;
|
|
|
|
use crate::common::*;
|
|
|
|
|
|
|
|
use chrono::prelude::*;
|
|
|
|
use serde_json::json;
|
|
|
|
|
|
|
|
use glean_core::metrics::*;
|
|
|
|
use glean_core::storage::StorageManager;
|
2019-12-13 14:11:47 +03:00
|
|
|
use glean_core::{CommonMetricData, Lifetime};
|
2019-05-28 12:42:46 +03:00
|
|
|
|
2019-05-30 17:25:42 +03:00
|
|
|
// SKIPPED from glean-ac: datetime deserializer should correctly parse integers
|
2019-05-28 12:42:46 +03:00
|
|
|
// This test doesn't really apply to rkv
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn datetime_serializer_should_correctly_serialize_datetime() {
|
2019-05-29 11:53:04 +03:00
|
|
|
let expected_value = "1983-04-13T12:09+00:00";
|
2019-12-13 14:11:47 +03:00
|
|
|
let (mut tempdir, _) = tempdir();
|
2019-07-26 16:09:46 +03:00
|
|
|
|
2019-05-28 12:42:46 +03:00
|
|
|
{
|
2019-12-13 14:11:47 +03:00
|
|
|
// We give tempdir to the `new_glean` function...
|
|
|
|
let (glean, dir) = new_glean(Some(tempdir));
|
|
|
|
// And then we get it back once that function returns.
|
|
|
|
tempdir = dir;
|
2019-05-28 12:42:46 +03:00
|
|
|
|
2019-05-30 17:27:31 +03:00
|
|
|
let metric = DatetimeMetric::new(
|
|
|
|
CommonMetricData {
|
|
|
|
name: "datetime_metric".into(),
|
|
|
|
category: "telemetry".into(),
|
|
|
|
send_in_pings: vec!["store1".into()],
|
|
|
|
disabled: false,
|
|
|
|
lifetime: Lifetime::User,
|
2019-10-17 01:15:20 +03:00
|
|
|
..Default::default()
|
2019-05-30 17:27:31 +03:00
|
|
|
},
|
|
|
|
TimeUnit::Minute,
|
|
|
|
);
|
2019-05-28 12:42:46 +03:00
|
|
|
|
2019-05-29 11:53:04 +03:00
|
|
|
// `1983-04-13T12:09:14.274+00:00` will be truncated to Minute resolution.
|
2019-05-30 17:27:31 +03:00
|
|
|
let dt = FixedOffset::east(0)
|
|
|
|
.ymd(1983, 4, 13)
|
|
|
|
.and_hms_milli(12, 9, 14, 274);
|
2019-06-06 16:29:47 +03:00
|
|
|
metric.set(&glean, Some(dt));
|
2019-05-28 12:42:46 +03:00
|
|
|
|
|
|
|
let snapshot = StorageManager
|
|
|
|
.snapshot_as_json(glean.storage(), "store1", true)
|
|
|
|
.unwrap();
|
|
|
|
assert_eq!(
|
2019-05-29 11:53:04 +03:00
|
|
|
json!({"datetime": {"telemetry.datetime_metric": expected_value}}),
|
2019-05-28 12:42:46 +03:00
|
|
|
snapshot
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2019-06-12 19:44:05 +03:00
|
|
|
// Make a new Glean instance here, which should force reloading of the data from disk
|
2019-05-28 12:42:46 +03:00
|
|
|
// so we can ensure it persisted, because it has User lifetime
|
|
|
|
{
|
2019-12-13 14:11:47 +03:00
|
|
|
let (glean, _) = new_glean(Some(tempdir));
|
2019-05-28 12:42:46 +03:00
|
|
|
let snapshot = StorageManager
|
|
|
|
.snapshot_as_json(glean.storage(), "store1", true)
|
|
|
|
.unwrap();
|
|
|
|
assert_eq!(
|
2019-05-29 11:53:04 +03:00
|
|
|
json!({"datetime": {"telemetry.datetime_metric": expected_value}}),
|
2019-05-28 12:42:46 +03:00
|
|
|
snapshot
|
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|
2019-05-30 17:25:42 +03:00
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn set_value_properly_sets_the_value_in_all_stores() {
|
2019-12-13 14:11:47 +03:00
|
|
|
let (glean, _t) = new_glean(None);
|
2019-05-30 17:25:42 +03:00
|
|
|
let store_names: Vec<String> = vec!["store1".into(), "store2".into()];
|
|
|
|
|
2019-05-30 17:27:31 +03:00
|
|
|
let metric = DatetimeMetric::new(
|
|
|
|
CommonMetricData {
|
|
|
|
name: "datetime_metric".into(),
|
|
|
|
category: "telemetry".into(),
|
|
|
|
send_in_pings: store_names.clone(),
|
|
|
|
disabled: false,
|
|
|
|
lifetime: Lifetime::Ping,
|
2019-10-17 01:15:20 +03:00
|
|
|
..Default::default()
|
2019-05-30 17:27:31 +03:00
|
|
|
},
|
|
|
|
TimeUnit::Nanosecond,
|
|
|
|
);
|
2019-05-30 17:25:42 +03:00
|
|
|
|
|
|
|
// `1983-04-13T12:09:14.274+00:00` will be truncated to Minute resolution.
|
2019-05-30 17:27:31 +03:00
|
|
|
let dt = FixedOffset::east(0)
|
|
|
|
.ymd(1983, 4, 13)
|
2019-05-30 18:50:35 +03:00
|
|
|
.and_hms_nano(12, 9, 14, 1_560_274);
|
2019-06-06 16:29:47 +03:00
|
|
|
metric.set(&glean, Some(dt));
|
2019-05-30 17:25:42 +03:00
|
|
|
|
|
|
|
for store_name in store_names {
|
|
|
|
assert_eq!(
|
|
|
|
"1983-04-13T12:09:14.001560274+00:00",
|
2019-05-30 17:27:31 +03:00
|
|
|
metric
|
|
|
|
.test_get_value_as_string(&glean, &store_name)
|
|
|
|
.unwrap()
|
2019-05-30 17:25:42 +03:00
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// SKIPPED from glean-ac: getSnapshot() returns null if nothing is recorded in the store
|
|
|
|
// This test doesn't really apply to rkv
|
|
|
|
|
|
|
|
// SKIPPED from glean-ac: getSnapshot() correctly clears the stores
|
|
|
|
// This test doesn't really apply to rkv
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_that_truncation_works() {
|
2019-12-13 14:11:47 +03:00
|
|
|
let (glean, _t) = new_glean(None);
|
2019-05-30 17:25:42 +03:00
|
|
|
|
|
|
|
// `1985-07-03T12:09:14.000560274+01:00`
|
2019-05-30 17:27:31 +03:00
|
|
|
let high_res_datetime = FixedOffset::east(3600)
|
|
|
|
.ymd(1985, 7, 3)
|
2019-05-30 18:50:35 +03:00
|
|
|
.and_hms_nano(12, 9, 14, 1_560_274);
|
2019-05-30 17:25:42 +03:00
|
|
|
let store_name = "store1";
|
|
|
|
|
|
|
|
// Create an helper struct for defining the truncation cases.
|
|
|
|
struct TestCase {
|
2019-06-03 16:45:19 +03:00
|
|
|
case_name: &'static str,
|
2019-05-30 17:25:42 +03:00
|
|
|
desired_resolution: TimeUnit,
|
2019-06-03 16:45:19 +03:00
|
|
|
expected_result: &'static str,
|
2019-05-30 17:25:42 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
// Define the single test cases.
|
|
|
|
let test_cases = vec![
|
|
|
|
TestCase {
|
2019-06-03 16:45:19 +03:00
|
|
|
case_name: "nano",
|
2019-05-30 17:25:42 +03:00
|
|
|
desired_resolution: TimeUnit::Nanosecond,
|
2019-06-03 16:45:19 +03:00
|
|
|
expected_result: "1985-07-03T12:09:14.001560274+01:00",
|
2019-05-30 17:25:42 +03:00
|
|
|
},
|
|
|
|
TestCase {
|
2019-06-03 16:45:19 +03:00
|
|
|
case_name: "micro",
|
2019-05-30 17:25:42 +03:00
|
|
|
desired_resolution: TimeUnit::Microsecond,
|
2019-06-03 16:45:19 +03:00
|
|
|
expected_result: "1985-07-03T12:09:14.001560+01:00",
|
2019-05-30 17:25:42 +03:00
|
|
|
},
|
|
|
|
TestCase {
|
2019-06-03 16:45:19 +03:00
|
|
|
case_name: "milli",
|
2019-05-30 17:25:42 +03:00
|
|
|
desired_resolution: TimeUnit::Millisecond,
|
2019-06-03 16:45:19 +03:00
|
|
|
expected_result: "1985-07-03T12:09:14.001+01:00",
|
2019-05-30 17:25:42 +03:00
|
|
|
},
|
|
|
|
TestCase {
|
2019-06-03 16:45:19 +03:00
|
|
|
case_name: "second",
|
2019-05-30 17:25:42 +03:00
|
|
|
desired_resolution: TimeUnit::Second,
|
2019-06-03 16:45:19 +03:00
|
|
|
expected_result: "1985-07-03T12:09:14+01:00",
|
2019-05-30 17:25:42 +03:00
|
|
|
},
|
|
|
|
TestCase {
|
2019-06-03 16:45:19 +03:00
|
|
|
case_name: "minute",
|
2019-05-30 17:25:42 +03:00
|
|
|
desired_resolution: TimeUnit::Minute,
|
2019-06-03 16:45:19 +03:00
|
|
|
expected_result: "1985-07-03T12:09+01:00",
|
2019-05-30 17:25:42 +03:00
|
|
|
},
|
|
|
|
TestCase {
|
2019-06-03 16:45:19 +03:00
|
|
|
case_name: "hour",
|
2019-05-30 17:25:42 +03:00
|
|
|
desired_resolution: TimeUnit::Hour,
|
2019-06-03 16:45:19 +03:00
|
|
|
expected_result: "1985-07-03T12+01:00",
|
2019-05-30 17:25:42 +03:00
|
|
|
},
|
|
|
|
TestCase {
|
2019-06-03 16:45:19 +03:00
|
|
|
case_name: "day",
|
2019-05-30 17:25:42 +03:00
|
|
|
desired_resolution: TimeUnit::Day,
|
2019-06-03 16:45:19 +03:00
|
|
|
expected_result: "1985-07-03+01:00",
|
2019-05-30 17:25:42 +03:00
|
|
|
},
|
|
|
|
];
|
|
|
|
|
|
|
|
// Execute them all.
|
|
|
|
for t in test_cases {
|
2019-05-30 17:27:31 +03:00
|
|
|
let metric = DatetimeMetric::new(
|
|
|
|
CommonMetricData {
|
2019-05-30 18:50:35 +03:00
|
|
|
name: format!("datetime_metric_{}", t.case_name),
|
2019-05-30 17:27:31 +03:00
|
|
|
category: "telemetry".into(),
|
|
|
|
send_in_pings: vec![store_name.into()],
|
|
|
|
disabled: false,
|
|
|
|
lifetime: Lifetime::User,
|
2019-10-17 01:15:20 +03:00
|
|
|
..Default::default()
|
2019-05-30 17:27:31 +03:00
|
|
|
},
|
|
|
|
t.desired_resolution,
|
|
|
|
);
|
2019-06-06 16:29:47 +03:00
|
|
|
metric.set(&glean, Some(high_res_datetime));
|
2019-05-30 17:25:42 +03:00
|
|
|
|
|
|
|
assert_eq!(
|
|
|
|
t.expected_result,
|
2019-05-30 17:27:31 +03:00
|
|
|
metric
|
|
|
|
.test_get_value_as_string(&glean, &store_name)
|
|
|
|
.unwrap()
|
2019-05-30 17:25:42 +03:00
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|