Debian Codegraph @

third_party/rust/glean-core/tests/timespan.rs

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