Debian Codegraph @

third_party/rust/glean-core/tests/ping.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 mod common;
6 use crate::common::*;
7
8 use std::collections::HashMap;
9
10 use glean_core::metrics::*;
11 use glean_core::CommonMetricData;
12 use glean_core::LabeledMetricData;
13 use glean_core::Lifetime;
14
15 #[test]
16 fn write_ping_to_disk() {
17 let (mut glean, _temp) = new_glean(None);
18
19 let ping = new_test_ping(&mut glean, "store1");
20
21 // We need to store a metric as an empty ping is not stored.
22 let counter = CounterMetric::new(CommonMetricData {
23 name: "counter".into(),
24 category: "local".into(),
25 send_in_pings: vec!["store1".into()],
26 ..Default::default()
27 });
28 counter.add_sync(&glean, 1);
29
30 assert!(ping.submit_sync(&glean, None));
31
32 assert_eq!(1, get_queued_pings(glean.get_data_path()).unwrap().len());
33 }
34
35 #[test]
36 fn disabling_upload_clears_pending_pings() {
37 let (mut glean, _t) = new_glean(None);
38
39 let ping = new_test_ping(&mut glean, "store1");
40
41 // We need to store a metric as an empty ping is not stored.
42 let counter = CounterMetric::new(CommonMetricData {
43 name: "counter".into(),
44 category: "local".into(),
45 send_in_pings: vec!["store1".into()],
46 ..Default::default()
47 });
48
49 counter.add_sync(&glean, 1);
50 assert!(ping.submit_sync(&glean, None));
51 assert_eq!(1, get_queued_pings(glean.get_data_path()).unwrap().len());
52 // At this point no deletion_request ping should exist
53 // (that is: it's directory should not exist at all)
54 assert!(get_deletion_pings(glean.get_data_path()).is_err());
55
56 glean.set_upload_enabled(false);
57 assert_eq!(0, get_queued_pings(glean.get_data_path()).unwrap().len());
58 // Disabling upload generates a deletion ping
59 let dpings = get_deletion_pings(glean.get_data_path()).unwrap();
60 assert_eq!(1, dpings.len());
61 let payload = &dpings[0].1;
62 assert_eq!(
63 "set_upload_enabled",
64 payload["ping_info"].as_object().unwrap()["reason"]
65 .as_str()
66 .unwrap()
67 );
68
69 glean.set_upload_enabled(true);
70 assert_eq!(0, get_queued_pings(glean.get_data_path()).unwrap().len());
71
72 counter.add_sync(&glean, 1);
73 assert!(ping.submit_sync(&glean, None));
74 assert_eq!(1, get_queued_pings(glean.get_data_path()).unwrap().len());
75 }
76
77 #[test]
78 fn deletion_request_only_when_toggled_from_on_to_off() {
79 let (mut glean, _t) = new_glean(None);
80
81 // Disabling upload generates a deletion ping
82 glean.set_upload_enabled(false);
83 let dpings = get_deletion_pings(glean.get_data_path()).unwrap();
84 assert_eq!(1, dpings.len());
85 let payload = &dpings[0].1;
86 assert_eq!(
87 "set_upload_enabled",
88 payload["ping_info"].as_object().unwrap()["reason"]
89 .as_str()
90 .unwrap()
91 );
92
93 // Re-setting it to `false` should not generate an additional ping.
94 // As we didn't clear the pending ping, that's the only one that sticks around.
95 glean.set_upload_enabled(false);
96 assert_eq!(1, get_deletion_pings(glean.get_data_path()).unwrap().len());
97
98 // Toggling back to true won't generate a ping either.
99 glean.set_upload_enabled(true);
100 assert_eq!(1, get_deletion_pings(glean.get_data_path()).unwrap().len());
101 }
102
103 #[test]
104 fn empty_pings_with_flag_are_sent() {
105 let (mut glean, _t) = new_glean(None);
106
107 let ping1 = PingBuilder::new("custom-ping1")
108 .with_send_if_empty(true)
109 .build();
110 glean.register_ping_type(&ping1);
111 let ping2 = PingBuilder::new("custom-ping2").build();
112 glean.register_ping_type(&ping2);
113
114 // No data is stored in either of the custom pings
115
116 // Sending this should succeed.
117 assert!(ping1.submit_sync(&glean, None));
118 assert_eq!(1, get_queued_pings(glean.get_data_path()).unwrap().len());
119
120 // Sending this should fail.
121 assert!(!ping2.submit_sync(&glean, None));
122 assert_eq!(1, get_queued_pings(glean.get_data_path()).unwrap().len());
123 }
124
125 #[test]
126 fn test_pings_submitted_metric() {
127 let (mut glean, _temp) = new_glean(None);
128
129 // Reconstructed here so we can test it without reaching into the library
130 // internals.
131 let pings_submitted = LabeledCounter::new(
132 LabeledMetricData::Common {
133 cmd: CommonMetricData {
134 name: "pings_submitted".into(),
135 category: "glean.validation".into(),
136 send_in_pings: vec!["metrics".into(), "baseline".into()],
137 lifetime: Lifetime::Ping,
138 disabled: false,
139 dynamic_label: None,
140 },
141 },
142 None,
143 );
144
145 let metrics_ping = new_test_ping(&mut glean, "metrics");
146 let baseline_ping = new_test_ping(&mut glean, "baseline");
147
148 let custom_ping = PingBuilder::new("custom").with_send_if_empty(true).build();
149 glean.register_ping_type(&custom_ping);
150
151 // We need to store a metric as an empty ping is not stored.
152 let counter = CounterMetric::new(CommonMetricData {
153 name: "counter".into(),
154 category: "local".into(),
155 send_in_pings: vec!["metrics".into()],
156 ..Default::default()
157 });
158 counter.add_sync(&glean, 1);
159
160 assert!(metrics_ping.submit_sync(&glean, None));
161 // A custom ping is just never recorded.
162 assert!(custom_ping.submit_sync(&glean, None));
163
164 // Check recording in the metrics ping
165 assert_eq!(
166 Some(1),
167 pings_submitted.get("metrics").get_value(&glean, "metrics")
168 );
169 assert_eq!(
170 None,
171 pings_submitted.get("baseline").get_value(&glean, "metrics")
172 );
173 assert_eq!(
174 None,
175 pings_submitted.get("custom").get_value(&glean, "metrics")
176 );
177
178 // Check recording in the baseline ping
179 assert_eq!(
180 Some(1),
181 pings_submitted.get("metrics").get_value(&glean, "baseline")
182 );
183 assert_eq!(
184 None,
185 pings_submitted
186 .get("baseline")
187 .get_value(&glean, "baseline")
188 );
189 assert_eq!(
190 None,
191 pings_submitted.get("custom").get_value(&glean, "baseline")
192 );
193
194 // Trigger 2 baseline pings.
195 // This should record a count of 2 baseline pings in the metrics ping, but
196 // it resets each time on the baseline ping, so we should only ever get 1
197 // baseline ping recorded in the baseline ping itsef.
198 assert!(baseline_ping.submit_sync(&glean, None));
199 assert!(baseline_ping.submit_sync(&glean, None));
200 // A custom ping is just never recorded.
201 assert!(custom_ping.submit_sync(&glean, None));
202
203 // Check recording in the metrics ping
204 assert_eq!(
205 Some(1),
206 pings_submitted
207 .get("metrics")
208 .get_value(&glean, Some("metrics"))
209 );
210 assert_eq!(
211 Some(2),
212 pings_submitted
213 .get("baseline")
214 .get_value(&glean, Some("metrics"))
215 );
216 assert_eq!(
217 None,
218 pings_submitted
219 .get("custom")
220 .get_value(&glean, Some("metrics"))
221 );
222
223 // Check recording in the baseline ping
224 assert_eq!(
225 None,
226 pings_submitted
227 .get("metrics")
228 .get_value(&glean, Some("baseline"))
229 );
230 assert_eq!(
231 Some(1),
232 pings_submitted
233 .get("baseline")
234 .get_value(&glean, Some("baseline"))
235 );
236 assert_eq!(
237 None,
238 pings_submitted
239 .get("custom")
240 .get_value(&glean, Some("baseline"))
241 );
242 }
243
244 #[test]
245 fn events_ping_with_metric_but_no_events_is_not_sent() {
246 let (mut glean, _t) = new_glean(None);
247
248 let events_ping = new_test_ping(&mut glean, "events");
249 let counter = CounterMetric::new(CommonMetricData {
250 name: "counter".into(),
251 category: "local".into(),
252 send_in_pings: vec!["events".into()],
253 ..Default::default()
254 });
255 counter.add_sync(&glean, 1);
256
257 // Sending this should fail.
258 assert!(!events_ping.submit_sync(&glean, None));
259 assert!(get_queued_pings(glean.get_data_path()).is_err());
260
261 let event = EventMetric::new(
262 CommonMetricData {
263 name: "name".into(),
264 category: "category".into(),
265 send_in_pings: vec!["events".into()],
266 ..Default::default()
267 },
268 vec![],
269 );
270 event.record_sync(&glean, 0, HashMap::new(), 0);
271
272 // Sending this should now succeed.
273 assert!(events_ping.submit_sync(&glean, None));
274 assert_eq!(1, get_queued_pings(glean.get_data_path()).unwrap().len());
275 }
276
277 #[test]
278 fn test_scheduled_pings_are_sent() {
279 let (mut glean, _t) = new_glean(None);
280
281 let piggyback_ping = PingBuilder::new("piggyback")
282 .with_send_if_empty(true)
283 .build();
284 glean.register_ping_type(&piggyback_ping);
285
286 let trigger_ping = PingBuilder::new("trigger")
287 .with_send_if_empty(true)
288 .with_schedules_pings(vec!["piggyback".into()])
289 .build();
290 glean.register_ping_type(&trigger_ping);
291
292 assert!(trigger_ping.submit_sync(&glean, None));
293 assert_eq!(2, get_queued_pings(glean.get_data_path()).unwrap().len());
294 }
295
296 #[test]
297 #[ignore] // This metric is disabled by default now, so we can skip this test. See Bug 1928161
298 fn database_write_timings_get_recorded() {
299 let (mut glean, _t) = new_glean(None);
300
301 let metrics_ping = new_test_ping(&mut glean, "metrics");
302 glean.register_ping_type(&metrics_ping);
303
304 // We need to store a metric to record something.
305 let counter = CounterMetric::new(CommonMetricData {
306 name: "counter".into(),
307 category: "local".into(),
308 send_in_pings: vec!["metrics".into()],
309 ..Default::default()
310 });
311 counter.add_sync(&glean, 1);
312
313 assert!(metrics_ping.submit_sync(&glean, None));
314
315 let mut queued_pings = get_queued_pings(glean.get_data_path()).unwrap();
316 assert_eq!(1, queued_pings.len(), "missing metrics ping");
317
318 let json = queued_pings.pop().unwrap().1;
319 let write_time = &json["metrics"]["timing_distribution"]["glean.database.write_time"];
320 assert!(
321 0 < write_time["sum"].as_i64().unwrap(),
322 "writing should take some time"
323 );
324 }