| 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::{test_get_num_recorded_errors, ErrorType}; |
| 13 |
use glean_core::{CommonMetricData, Lifetime}; |
| 14 |
|
| 15 |
#[test] |
| 16 |
fn text_serializer_should_correctly_serialize_strings() { |
| 17 |
let (mut tempdir, _) = tempdir(); |
| 18 |
|
| 19 |
{ |
| 20 |
// We give tempdir to the `new_glean` function... |
| 21 |
let (glean, dir) = new_glean(Some(tempdir)); |
| 22 |
// And then we get it back once that function returns. |
| 23 |
tempdir = dir; |
| 24 |
|
| 25 |
let metric = TextMetric::new(CommonMetricData { |
| 26 |
name: "text_metric".into(), |
| 27 |
category: "telemetry".into(), |
| 28 |
send_in_pings: vec!["store1".into()], |
| 29 |
disabled: false, |
| 30 |
lifetime: Lifetime::User, |
| 31 |
..Default::default() |
| 32 |
}); |
| 33 |
|
| 34 |
metric.set_sync(&glean, "test_text_value"); |
| 35 |
|
| 36 |
let snapshot = StorageManager |
| 37 |
.snapshot_as_json(glean.storage(), "store1", true) |
| 38 |
.unwrap(); |
| 39 |
assert_eq!( |
| 40 |
json!({"text": {"telemetry.text_metric": "test_text_value"}}), |
| 41 |
snapshot |
| 42 |
); |
| 43 |
} |
| 44 |
|
| 45 |
// Make a new Glean instance here, which should force reloading of the data from disk |
| 46 |
// so we can ensure it persisted, because it has User lifetime |
| 47 |
{ |
| 48 |
let (glean, _t) = new_glean(Some(tempdir)); |
| 49 |
let snapshot = StorageManager |
| 50 |
.snapshot_as_json(glean.storage(), "store1", true) |
| 51 |
.unwrap(); |
| 52 |
assert_eq!( |
| 53 |
json!({"text": {"telemetry.text_metric": "test_text_value"}}), |
| 54 |
snapshot |
| 55 |
); |
| 56 |
} |
| 57 |
} |
| 58 |
|
| 59 |
#[test] |
| 60 |
fn set_properly_sets_the_value_in_all_stores() { |
| 61 |
let (glean, _t) = new_glean(None); |
| 62 |
let store_names: Vec<String> = vec!["store1".into(), "store2".into()]; |
| 63 |
|
| 64 |
let metric = TextMetric::new(CommonMetricData { |
| 65 |
name: "text_metric".into(), |
| 66 |
category: "telemetry".into(), |
| 67 |
send_in_pings: store_names.clone(), |
| 68 |
disabled: false, |
| 69 |
lifetime: Lifetime::Ping, |
| 70 |
..Default::default() |
| 71 |
}); |
| 72 |
|
| 73 |
metric.set_sync(&glean, "test_text_value"); |
| 74 |
|
| 75 |
// Check that the data was correctly set in each store. |
| 76 |
for store_name in store_names { |
| 77 |
let snapshot = StorageManager |
| 78 |
.snapshot_as_json(glean.storage(), &store_name, true) |
| 79 |
.unwrap(); |
| 80 |
|
| 81 |
assert_eq!( |
| 82 |
json!({"text": {"telemetry.text_metric": "test_text_value"}}), |
| 83 |
snapshot |
| 84 |
); |
| 85 |
} |
| 86 |
} |
| 87 |
|
| 88 |
#[test] |
| 89 |
fn long_text_values_are_truncated() { |
| 90 |
let (glean, _t) = new_glean(None); |
| 91 |
|
| 92 |
let metric = TextMetric::new(CommonMetricData { |
| 93 |
name: "text_metric".into(), |
| 94 |
category: "telemetry".into(), |
| 95 |
send_in_pings: vec!["store1".into()], |
| 96 |
disabled: false, |
| 97 |
lifetime: Lifetime::Ping, |
| 98 |
..Default::default() |
| 99 |
}); |
| 100 |
|
| 101 |
let test_string = "01234567890".repeat(200 * 1024); |
| 102 |
metric.set_sync(&glean, test_string.clone()); |
| 103 |
|
| 104 |
// Check that data was truncated |
| 105 |
assert_eq!( |
| 106 |
test_string[..(200 * 1024)], |
| 107 |
metric.get_value(&glean, "store1").unwrap() |
| 108 |
); |
| 109 |
|
| 110 |
// Make sure that the errors have been recorded |
| 111 |
assert_eq!( |
| 112 |
Ok(1), |
| 113 |
test_get_num_recorded_errors(&glean, metric.meta(), ErrorType::InvalidOverflow) |
| 114 |
); |
| 115 |
} |