| 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, CommonMetricData, ErrorType, Lifetime}; |
| 13 |
|
| 14 |
#[test] |
| 15 |
fn list_can_store_multiple_items() { |
| 16 |
let (glean, _t) = new_glean(None); |
| 17 |
|
| 18 |
let list: StringListMetric = StringListMetric::new(CommonMetricData { |
| 19 |
name: "list".into(), |
| 20 |
category: "local".into(), |
| 21 |
send_in_pings: vec!["store1".into()], |
| 22 |
..Default::default() |
| 23 |
}); |
| 24 |
|
| 25 |
list.add_sync(&glean, "first"); |
| 26 |
assert_eq!(list.get_value(&glean, "store1").unwrap(), vec!["first"]); |
| 27 |
|
| 28 |
list.add_sync(&glean, "second"); |
| 29 |
assert_eq!( |
| 30 |
list.get_value(&glean, "store1").unwrap(), |
| 31 |
vec!["first", "second"] |
| 32 |
); |
| 33 |
|
| 34 |
list.set_sync(&glean, vec!["third".into()]); |
| 35 |
assert_eq!(list.get_value(&glean, "store1").unwrap(), vec!["third"]); |
| 36 |
|
| 37 |
list.add_sync(&glean, "fourth"); |
| 38 |
assert_eq!( |
| 39 |
list.get_value(&glean, "store1").unwrap(), |
| 40 |
vec!["third", "fourth"] |
| 41 |
); |
| 42 |
} |
| 43 |
|
| 44 |
#[test] |
| 45 |
fn stringlist_serializer_should_correctly_serialize_stringlists() { |
| 46 |
let (mut tempdir, _) = tempdir(); |
| 47 |
|
| 48 |
{ |
| 49 |
// We give tempdir to the `new_glean` function... |
| 50 |
let (glean, dir) = new_glean(Some(tempdir)); |
| 51 |
// And then we get it back once that function returns. |
| 52 |
tempdir = dir; |
| 53 |
|
| 54 |
let metric = StringListMetric::new(CommonMetricData { |
| 55 |
name: "string_list_metric".into(), |
| 56 |
category: "telemetry.test".into(), |
| 57 |
send_in_pings: vec!["store1".into()], |
| 58 |
disabled: false, |
| 59 |
lifetime: Lifetime::User, |
| 60 |
..Default::default() |
| 61 |
}); |
| 62 |
metric.set_sync(&glean, vec!["test_string_1".into(), "test_string_2".into()]); |
| 63 |
} |
| 64 |
|
| 65 |
{ |
| 66 |
let (glean, _t) = new_glean(Some(tempdir)); |
| 67 |
|
| 68 |
let snapshot = StorageManager |
| 69 |
.snapshot_as_json(glean.storage(), "store1", true) |
| 70 |
.unwrap(); |
| 71 |
assert_eq!( |
| 72 |
json!({"string_list": {"telemetry.test.string_list_metric": ["test_string_1", "test_string_2"]}}), |
| 73 |
snapshot |
| 74 |
); |
| 75 |
} |
| 76 |
} |
| 77 |
|
| 78 |
#[test] |
| 79 |
fn set_properly_sets_the_value_in_all_stores() { |
| 80 |
let (glean, _t) = new_glean(None); |
| 81 |
let store_names: Vec<String> = vec!["store1".into(), "store2".into()]; |
| 82 |
|
| 83 |
let metric = StringListMetric::new(CommonMetricData { |
| 84 |
name: "string_list_metric".into(), |
| 85 |
category: "telemetry.test".into(), |
| 86 |
send_in_pings: store_names.clone(), |
| 87 |
disabled: false, |
| 88 |
lifetime: Lifetime::Ping, |
| 89 |
..Default::default() |
| 90 |
}); |
| 91 |
|
| 92 |
metric.set_sync(&glean, vec!["test_string_1".into(), "test_string_2".into()]); |
| 93 |
|
| 94 |
for store_name in store_names { |
| 95 |
let snapshot = StorageManager |
| 96 |
.snapshot_as_json(glean.storage(), &store_name, true) |
| 97 |
.unwrap(); |
| 98 |
|
| 99 |
assert_eq!( |
| 100 |
json!({"string_list": {"telemetry.test.string_list_metric": ["test_string_1", "test_string_2"]}}), |
| 101 |
snapshot |
| 102 |
); |
| 103 |
} |
| 104 |
} |
| 105 |
|
| 106 |
#[test] |
| 107 |
fn long_string_values_are_truncated() { |
| 108 |
let (glean, _t) = new_glean(None); |
| 109 |
|
| 110 |
let metric = StringListMetric::new(CommonMetricData { |
| 111 |
name: "string_list_metric".into(), |
| 112 |
category: "telemetry.test".into(), |
| 113 |
send_in_pings: vec!["store1".into()], |
| 114 |
disabled: false, |
| 115 |
lifetime: Lifetime::Ping, |
| 116 |
..Default::default() |
| 117 |
}); |
| 118 |
|
| 119 |
let test_string = "0123456789".repeat(20); |
| 120 |
metric.add_sync(&glean, test_string.clone()); |
| 121 |
|
| 122 |
// Ensure the string was truncated to the proper length. |
| 123 |
assert_eq!( |
| 124 |
vec![test_string[..100].to_string()], |
| 125 |
metric.get_value(&glean, "store1").unwrap() |
| 126 |
); |
| 127 |
|
| 128 |
// Ensure the error has been recorded. |
| 129 |
assert_eq!( |
| 130 |
Ok(1), |
| 131 |
test_get_num_recorded_errors(&glean, metric.meta(), ErrorType::InvalidOverflow) |
| 132 |
); |
| 133 |
|
| 134 |
metric.set_sync(&glean, vec![test_string.clone()]); |
| 135 |
|
| 136 |
// Ensure the string was truncated to the proper length. |
| 137 |
assert_eq!( |
| 138 |
vec![test_string[..100].to_string()], |
| 139 |
metric.get_value(&glean, "store1").unwrap() |
| 140 |
); |
| 141 |
|
| 142 |
// Ensure the error has been recorded. |
| 143 |
assert_eq!( |
| 144 |
Ok(2), |
| 145 |
test_get_num_recorded_errors(&glean, metric.meta(), ErrorType::InvalidOverflow) |
| 146 |
); |
| 147 |
} |
| 148 |
|
| 149 |
#[test] |
| 150 |
fn disabled_string_lists_dont_record() { |
| 151 |
let (glean, _t) = new_glean(None); |
| 152 |
|
| 153 |
let metric = StringListMetric::new(CommonMetricData { |
| 154 |
name: "string_list_metric".into(), |
| 155 |
category: "telemetry.test".into(), |
| 156 |
send_in_pings: vec!["store1".into()], |
| 157 |
disabled: true, |
| 158 |
lifetime: Lifetime::Ping, |
| 159 |
..Default::default() |
| 160 |
}); |
| 161 |
|
| 162 |
metric.add_sync(&glean, "test_string".repeat(20)); |
| 163 |
|
| 164 |
// Ensure the string was not added. |
| 165 |
assert_eq!(None, metric.get_value(&glean, "store1")); |
| 166 |
|
| 167 |
metric.set_sync(&glean, vec!["test_string_2".repeat(20)]); |
| 168 |
|
| 169 |
// Ensure the stringlist was not set. |
| 170 |
assert_eq!(None, metric.get_value(&glean, "store1")); |
| 171 |
|
| 172 |
// Ensure no error was recorded. |
| 173 |
assert!(test_get_num_recorded_errors(&glean, metric.meta(), ErrorType::InvalidValue).is_err()); |
| 174 |
} |
| 175 |
|
| 176 |
#[test] |
| 177 |
fn string_lists_dont_exceed_max_items() { |
| 178 |
let (glean, _t) = new_glean(None); |
| 179 |
|
| 180 |
let metric = StringListMetric::new(CommonMetricData { |
| 181 |
name: "string_list_metric".into(), |
| 182 |
category: "telemetry.test".into(), |
| 183 |
send_in_pings: vec!["store1".into()], |
| 184 |
disabled: false, |
| 185 |
lifetime: Lifetime::Ping, |
| 186 |
..Default::default() |
| 187 |
}); |
| 188 |
|
| 189 |
for _n in 1..101 { |
| 190 |
metric.add_sync(&glean, "test_string"); |
| 191 |
} |
| 192 |
|
| 193 |
let expected: Vec<String> = "test_string " |
| 194 |
.repeat(100) |
| 195 |
.split_whitespace() |
| 196 |
.map(|s| s.to_string()) |
| 197 |
.collect(); |
| 198 |
assert_eq!(expected, metric.get_value(&glean, "store1").unwrap()); |
| 199 |
|
| 200 |
// Ensure the 101st string wasn't added. |
| 201 |
metric.add_sync(&glean, "test_string"); |
| 202 |
assert_eq!(expected, metric.get_value(&glean, "store1").unwrap()); |
| 203 |
|
| 204 |
// Ensure we recorded the error. |
| 205 |
assert_eq!( |
| 206 |
Ok(1), |
| 207 |
test_get_num_recorded_errors(&glean, metric.meta(), ErrorType::InvalidValue) |
| 208 |
); |
| 209 |
|
| 210 |
// Try to set it to a list that's too long. Ensure it cuts off at 100 elements. |
| 211 |
let too_many: Vec<String> = "test_string " |
| 212 |
.repeat(101) |
| 213 |
.split_whitespace() |
| 214 |
.map(|s| s.to_string()) |
| 215 |
.collect(); |
| 216 |
metric.set_sync(&glean, too_many); |
| 217 |
assert_eq!(expected, metric.get_value(&glean, "store1").unwrap()); |
| 218 |
|
| 219 |
assert_eq!( |
| 220 |
Ok(2), |
| 221 |
test_get_num_recorded_errors(&glean, metric.meta(), ErrorType::InvalidValue) |
| 222 |
); |
| 223 |
} |
| 224 |
|
| 225 |
#[test] |
| 226 |
fn set_does_not_record_error_when_receiving_empty_list() { |
| 227 |
let (glean, _t) = new_glean(None); |
| 228 |
|
| 229 |
let metric = StringListMetric::new(CommonMetricData { |
| 230 |
name: "string_list_metric".into(), |
| 231 |
category: "telemetry.test".into(), |
| 232 |
send_in_pings: vec!["store1".into()], |
| 233 |
disabled: false, |
| 234 |
lifetime: Lifetime::Ping, |
| 235 |
..Default::default() |
| 236 |
}); |
| 237 |
|
| 238 |
metric.set_sync(&glean, vec![]); |
| 239 |
|
| 240 |
// Ensure the empty list was added |
| 241 |
assert_eq!(Some(vec![]), metric.get_value(&glean, "store1")); |
| 242 |
|
| 243 |
// Ensure we didn't record an error. |
| 244 |
assert!(test_get_num_recorded_errors(&glean, metric.meta(), ErrorType::InvalidValue).is_err()); |
| 245 |
} |