| 1 |
// This Source Code Form is subject to the terms of the Mozilla Public |
| 2 |
// License, v. 2.0. If a copy of the MPL was not distributed with this |
| 3 |
// file, You can obtain one at https://mozilla.org/MPL/2.0/. |
| 4 |
|
| 5 |
mod common; |
| 6 |
use crate::common::*; |
| 7 |
|
| 8 |
use serde_json::json; |
| 9 |
|
| 10 |
use glean_core::metrics::*; |
| 11 |
use glean_core::storage::StorageManager; |
| 12 |
use glean_core::{CommonMetricData, Lifetime}; |
| 13 |
|
| 14 |
#[test] |
| 15 |
fn uuid_is_generated_and_stored() { |
| 16 |
let (mut glean, _t) = new_glean(None); |
| 17 |
|
| 18 |
let uuid: UuidMetric = UuidMetric::new(CommonMetricData { |
| 19 |
name: "uuid".into(), |
| 20 |
category: "local".into(), |
| 21 |
send_in_pings: vec!["store1".into()], |
| 22 |
..Default::default() |
| 23 |
}); |
| 24 |
|
| 25 |
uuid.generate_and_set_sync(&glean); |
| 26 |
let snapshot = glean.snapshot("store1", false); |
| 27 |
assert!( |
| 28 |
snapshot.contains(r#""local.uuid": ""#), |
| 29 |
"Snapshot 1: {snapshot}" |
| 30 |
); |
| 31 |
|
| 32 |
uuid.generate_and_set_sync(&glean); |
| 33 |
let snapshot = glean.snapshot("store1", false); |
| 34 |
assert!( |
| 35 |
snapshot.contains(r#""local.uuid": ""#), |
| 36 |
"Snapshot 2: {snapshot}" |
| 37 |
); |
| 38 |
} |
| 39 |
|
| 40 |
#[test] |
| 41 |
fn uuid_serializer_should_correctly_serialize_uuids() { |
| 42 |
let value = uuid::Uuid::new_v4(); |
| 43 |
|
| 44 |
let (mut tempdir, _) = tempdir(); |
| 45 |
|
| 46 |
{ |
| 47 |
// We give tempdir to the `new_glean` function... |
| 48 |
let (glean, dir) = new_glean(Some(tempdir)); |
| 49 |
// And then we get it back once that function returns. |
| 50 |
tempdir = dir; |
| 51 |
|
| 52 |
let metric = UuidMetric::new(CommonMetricData { |
| 53 |
name: "uuid_metric".into(), |
| 54 |
category: "telemetry".into(), |
| 55 |
send_in_pings: vec!["store1".into()], |
| 56 |
disabled: false, |
| 57 |
lifetime: Lifetime::User, |
| 58 |
..Default::default() |
| 59 |
}); |
| 60 |
|
| 61 |
metric.set_from_uuid_sync(&glean, value); |
| 62 |
|
| 63 |
let snapshot = StorageManager |
| 64 |
.snapshot_as_json(glean.storage(), "store1", true) |
| 65 |
.unwrap(); |
| 66 |
assert_eq!( |
| 67 |
json!({"uuid": {"telemetry.uuid_metric": value.to_string()}}), |
| 68 |
snapshot |
| 69 |
); |
| 70 |
} |
| 71 |
|
| 72 |
// Make a new Glean instance here, which should force reloading of the data from disk |
| 73 |
// so we can ensure it persisted, because it has User lifetime |
| 74 |
{ |
| 75 |
let (glean, _t) = new_glean(Some(tempdir)); |
| 76 |
let snapshot = StorageManager |
| 77 |
.snapshot_as_json(glean.storage(), "store1", true) |
| 78 |
.unwrap(); |
| 79 |
assert_eq!( |
| 80 |
json!({"uuid": {"telemetry.uuid_metric": value.to_string()}}), |
| 81 |
snapshot |
| 82 |
); |
| 83 |
} |
| 84 |
} |
| 85 |
|
| 86 |
#[test] |
| 87 |
fn set_properly_sets_the_value_in_all_stores() { |
| 88 |
let (glean, _t) = new_glean(None); |
| 89 |
let store_names: Vec<String> = vec!["store1".into(), "store2".into()]; |
| 90 |
let value = uuid::Uuid::new_v4(); |
| 91 |
|
| 92 |
let metric = UuidMetric::new(CommonMetricData { |
| 93 |
name: "uuid_metric".into(), |
| 94 |
category: "telemetry".into(), |
| 95 |
send_in_pings: store_names.clone(), |
| 96 |
disabled: false, |
| 97 |
lifetime: Lifetime::Ping, |
| 98 |
..Default::default() |
| 99 |
}); |
| 100 |
|
| 101 |
metric.set_from_uuid_sync(&glean, value); |
| 102 |
|
| 103 |
// Check that the data was correctly set in each store. |
| 104 |
for store_name in store_names { |
| 105 |
let snapshot = StorageManager |
| 106 |
.snapshot_as_json(glean.storage(), &store_name, true) |
| 107 |
.unwrap(); |
| 108 |
|
| 109 |
assert_eq!( |
| 110 |
json!({"uuid": {"telemetry.uuid_metric": value.to_string()}}), |
| 111 |
snapshot |
| 112 |
); |
| 113 |
} |
| 114 |
} |