1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348
|
// 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/.
use std::time::Duration;
mod common;
use crate::common::*;
use serde_json::json;
use glean_core::metrics::*;
use glean_core::storage::StorageManager;
use glean_core::{test_get_num_recorded_errors, ErrorType};
use glean_core::{CommonMetricData, Lifetime};
// Tests ported from glean-ac
#[test]
fn serializer_should_correctly_serialize_timespans() {
let (mut tempdir, _) = tempdir();
let duration = 60;
{
// 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;
let metric = TimespanMetric::new(
CommonMetricData {
name: "timespan_metric".into(),
category: "telemetry".into(),
send_in_pings: vec!["store1".into()],
disabled: false,
lifetime: Lifetime::Ping,
..Default::default()
},
TimeUnit::Nanosecond,
);
metric.set_start(&glean, 0);
metric.set_stop(&glean, duration);
let val = metric
.get_value(&glean, "store1")
.expect("Value should be stored");
assert_eq!(duration, val, "Recorded timespan should be positive.");
}
// Make a new Glean instance here, which should force reloading of the data from disk
// so we can ensure it persisted, because it has User lifetime
{
let (glean, _t) = new_glean(Some(tempdir));
let snapshot = StorageManager
.snapshot_as_json(glean.storage(), "store1", true)
.unwrap();
assert_eq!(
json!({"timespan": {"telemetry.timespan_metric": { "value": duration, "time_unit": "nanosecond" }}}),
snapshot
);
}
}
#[test]
fn single_elapsed_time_must_be_recorded() {
let (glean, _t) = new_glean(None);
let metric = TimespanMetric::new(
CommonMetricData {
name: "timespan_metric".into(),
category: "telemetry".into(),
send_in_pings: vec!["store1".into()],
disabled: false,
lifetime: Lifetime::Ping,
..Default::default()
},
TimeUnit::Nanosecond,
);
let duration = 60;
metric.set_start(&glean, 0);
metric.set_stop(&glean, duration);
let val = metric
.get_value(&glean, "store1")
.expect("Value should be stored");
assert_eq!(duration, val, "Recorded timespan should be positive.");
}
// SKIPPED from glean-ac: multiple elapsed times must be correctly accumulated.
// replaced by below after API change.
#[test]
fn second_timer_run_is_skipped() {
let (glean, _t) = new_glean(None);
let metric = TimespanMetric::new(
CommonMetricData {
name: "timespan_metric".into(),
category: "telemetry".into(),
send_in_pings: vec!["store1".into()],
disabled: false,
lifetime: Lifetime::Ping,
..Default::default()
},
TimeUnit::Nanosecond,
);
let duration = 60;
metric.set_start(&glean, 0);
metric.set_stop(&glean, duration);
// No error should be recorded here: we had no prior value stored.
assert!(test_get_num_recorded_errors(&glean, metric.meta(), ErrorType::InvalidState).is_err());
let first_value = metric.get_value(&glean, "store1").unwrap();
assert_eq!(duration, first_value);
metric.set_start(&glean, 0);
metric.set_stop(&glean, duration * 2);
let second_value = metric.get_value(&glean, "store1").unwrap();
assert_eq!(second_value, first_value);
// Make sure that the error has been recorded: we had a stored value, the
// new measurement was dropped.
assert_eq!(
Ok(1),
test_get_num_recorded_errors(&glean, metric.meta(), ErrorType::InvalidState)
);
}
#[test]
fn recorded_time_conforms_to_resolution() {
let (glean, _t) = new_glean(None);
let ns_metric = TimespanMetric::new(
CommonMetricData {
name: "timespan_ns".into(),
category: "telemetry".into(),
send_in_pings: vec!["store1".into()],
disabled: false,
lifetime: Lifetime::Ping,
..Default::default()
},
TimeUnit::Nanosecond,
);
let minute_metric = TimespanMetric::new(
CommonMetricData {
name: "timespan_m".into(),
category: "telemetry".into(),
send_in_pings: vec!["store1".into()],
disabled: false,
lifetime: Lifetime::Ping,
..Default::default()
},
TimeUnit::Minute,
);
let duration = 60;
ns_metric.set_start(&glean, 0);
ns_metric.set_stop(&glean, duration);
let ns_value = ns_metric.get_value(&glean, "store1").unwrap();
assert_eq!(duration, ns_value);
// 1 minute in nanoseconds
let duration_minute = 60 * 1_000_000_000;
minute_metric.set_start(&glean, 0);
minute_metric.set_stop(&glean, duration_minute);
let minute_value = minute_metric.get_value(&glean, "store1").unwrap();
assert_eq!(1, minute_value);
}
// SKIPPED from glean-ac: accumulated short-lived timespans should not be discarded
#[test]
fn cancel_does_not_store() {
let (glean, _t) = new_glean(None);
let metric = TimespanMetric::new(
CommonMetricData {
name: "timespan_metric".into(),
category: "telemetry".into(),
send_in_pings: vec!["store1".into()],
disabled: false,
lifetime: Lifetime::Ping,
..Default::default()
},
TimeUnit::Nanosecond,
);
metric.set_start(&glean, 0);
metric.cancel();
assert_eq!(None, metric.get_value(&glean, "store1"));
}
#[test]
fn nothing_stored_before_stop() {
let (glean, _t) = new_glean(None);
let metric = TimespanMetric::new(
CommonMetricData {
name: "timespan_metric".into(),
category: "telemetry".into(),
send_in_pings: vec!["store1".into()],
disabled: false,
lifetime: Lifetime::Ping,
..Default::default()
},
TimeUnit::Nanosecond,
);
let duration = 60;
metric.set_start(&glean, 0);
assert_eq!(None, metric.get_value(&glean, "store1"));
metric.set_stop(&glean, duration);
assert_eq!(duration, metric.get_value(&glean, "store1").unwrap());
}
#[test]
fn set_raw_time() {
let (glean, _t) = new_glean(None);
let metric = TimespanMetric::new(
CommonMetricData {
name: "timespan_metric".into(),
category: "telemetry".into(),
send_in_pings: vec!["store1".into()],
disabled: false,
lifetime: Lifetime::Ping,
..Default::default()
},
TimeUnit::Nanosecond,
);
let time = Duration::from_secs(1);
metric.set_raw_sync(&glean, time);
let time_in_ns = time.as_nanos() as u64;
assert_eq!(Some(time_in_ns), metric.get_value(&glean, "store1"));
}
#[test]
fn set_raw_time_does_nothing_when_timer_running() {
let (glean, _t) = new_glean(None);
let metric = TimespanMetric::new(
CommonMetricData {
name: "timespan_metric".into(),
category: "telemetry".into(),
send_in_pings: vec!["store1".into()],
disabled: false,
lifetime: Lifetime::Ping,
..Default::default()
},
TimeUnit::Nanosecond,
);
let time = Duration::from_secs(42);
metric.set_start(&glean, 0);
metric.set_raw_sync(&glean, time);
metric.set_stop(&glean, 60);
// We expect the start/stop value, not the raw value.
assert_eq!(Some(60), metric.get_value(&glean, "store1"));
// Make sure that the error has been recorded
assert_eq!(
Ok(1),
test_get_num_recorded_errors(&glean, metric.meta(), ErrorType::InvalidState)
);
}
#[test]
fn timespan_is_tracked_across_upload_toggle() {
let (mut glean, _t) = new_glean(None);
let metric = TimespanMetric::new(
CommonMetricData {
name: "timespan_metric".into(),
category: "telemetry".into(),
send_in_pings: vec!["store1".into()],
disabled: false,
lifetime: Lifetime::Ping,
..Default::default()
},
TimeUnit::Nanosecond,
);
// Timer is started.
metric.set_start(&glean, 0);
// User disables telemetry upload.
glean.set_upload_enabled(false);
// App code eventually stops the timer.
// We should clear internal state as upload is disabled.
metric.set_stop(&glean, 40);
assert_eq!(None, metric.get_value(&glean, "store1"));
// App code eventually starts the timer again.
metric.set_start(&glean, 100);
// User enables telemetry upload again.
glean.set_upload_enabled(true);
// App code eventually stops the timer.
// The full timespan is recorded.
metric.set_stop(&glean, 200);
assert_eq!(Some(100), metric.get_value(&glean, "store1"));
// No errors have been recorded.
assert!(test_get_num_recorded_errors(&glean, metric.meta(), ErrorType::InvalidState).is_err());
}
#[test]
fn time_cannot_go_backwards() {
let (glean, _t) = new_glean(None);
let metric: TimespanMetric = TimespanMetric::new(
CommonMetricData {
name: "raw_timespan".into(),
category: "test".into(),
send_in_pings: vec!["store1".into()],
..Default::default()
},
TimeUnit::Millisecond,
);
// Time cannot go backwards.
metric.set_start(&glean, 10);
metric.set_stop(&glean, 0);
assert!(metric.get_value(&glean, "store1").is_none());
assert_eq!(
Ok(1),
test_get_num_recorded_errors(&glean, metric.meta(), ErrorType::InvalidValue),
);
}
|