Debian Codegraph @

third_party/rust/glean-core/tests/collection_enabled.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 glean_core::metrics::*;
9 use glean_core::CommonMetricData;
10 use glean_core::Glean;
11 use glean_core::Lifetime;
12
13 fn nofollows_ping(glean: &mut Glean) -> PingType {
14 // When `follows_collection_enabled=false` then by default `enabled=false`
15 let ping = PingBuilder::new("nofollows")
16 .with_send_if_empty(true)
17 .with_include_info_sections(false)
18 .with_enabled(false)
19 .with_follows_collection_enabled(false)
20 .build();
21 glean.register_ping_type(&ping);
22 ping
23 }
24
25 fn manual_ping(glean: &mut Glean) -> PingType {
26 let ping = PingBuilder::new("manual").build();
27 glean.register_ping_type(&ping);
28 ping
29 }
30
31 #[test]
32 fn pings_with_follows_false_are_exempt() {
33 let (mut glean, _t) = new_glean(None);
34
35 let ping = nofollows_ping(&mut glean);
36
37 // We need to store a metric as an empty ping is not stored.
38 let counter = CounterMetric::new(CommonMetricData {
39 name: "counter".into(),
40 category: "local".into(),
41 send_in_pings: vec!["nofollows".into()],
42 ..Default::default()
43 });
44
45 counter.add_sync(&glean, 1);
46 assert!(!ping.submit_sync(&glean, None));
47
48 glean.set_ping_enabled(&ping, true);
49 counter.add_sync(&glean, 2);
50 assert!(ping.submit_sync(&glean, None));
51
52 let mut queued_pings = get_queued_pings(glean.get_data_path()).unwrap();
53 assert_eq!(1, queued_pings.len());
54
55 let json = queued_pings.pop().unwrap().1;
56 let counter_val = json["metrics"]["counter"]["local.counter"]
57 .as_i64()
58 .unwrap();
59 assert_eq!(2, counter_val);
60
61 glean.set_upload_enabled(false);
62 assert_eq!(1, get_queued_pings(glean.get_data_path()).unwrap().len());
63 // Disabling upload generates a deletion ping
64 assert_eq!(1, get_deletion_pings(glean.get_data_path()).unwrap().len());
65
66 // Regardless, the `nofollows` ping is still enabled.
67 counter.add_sync(&glean, 10);
68 assert!(ping.submit_sync(&glean, None));
69
70 let queued_pings = get_queued_pings(glean.get_data_path()).unwrap();
71 // both `nofollows` pings remain in the queue
72 assert_eq!(2, queued_pings.len());
73
74 let mut values = vec![2, 10];
75 for ping in queued_pings {
76 let json = ping.1;
77 let counter_val = json["metrics"]["counter"]["local.counter"]
78 .as_i64()
79 .unwrap();
80
81 assert!(values.contains(&counter_val));
82 values.retain(|&x| x != counter_val);
83 }
84 }
85
86 #[test]
87 fn nofollows_ping_can_ride_along() {
88 let (mut glean, _t) = new_glean(None);
89
90 let nofollows_ping = nofollows_ping(&mut glean);
91 // Basically `manual_ping` but with a ride-along
92 let manual_ping = PingBuilder::new("manual")
93 .with_schedules_pings(vec!["nofollows".to_string()])
94 .build();
95 glean.register_ping_type(&manual_ping);
96
97 // We need to store a metric as an empty ping is not stored.
98 let counter = CounterMetric::new(CommonMetricData {
99 name: "counter".into(),
100 category: "local".into(),
101 send_in_pings: vec!["manual".into(), "nofollows".into()],
102 lifetime: Lifetime::Application,
103 ..Default::default()
104 });
105
106 // Trigger a ping with data.
107 counter.add_sync(&glean, 1);
108 assert!(manual_ping.submit_sync(&glean, None));
109 assert_eq!(1, get_queued_pings(glean.get_data_path()).unwrap().len());
110
111 // Enable one more ping, trigger it with more data
112 glean.set_ping_enabled(&nofollows_ping, true);
113 counter.add_sync(&glean, 2);
114 assert!(manual_ping.submit_sync(&glean, None));
115 // The previous one, plus 2 new ones
116 let queued_pings = get_queued_pings(glean.get_data_path()).unwrap();
117 assert_eq!(3, queued_pings.len());
118 for (_url, json, info) in queued_pings {
119 let Some(obj) = info else {
120 panic!("no ping info")
121 };
122 let counter_val = json["metrics"]["counter"]["local.counter"]
123 .as_i64()
124 .unwrap();
125
126 if obj["ping_name"].as_str().unwrap() == "nofollows" {
127 assert_eq!(2, counter_val, "{json:?}");
128 } else {
129 let seq = json["ping_info"]["seq"].as_i64().unwrap();
130
131 match seq {
132 0 => assert_eq!(1, counter_val),
133 1 => assert_eq!(3, counter_val),
134 2 => assert_eq!(8, counter_val),
135 _ => panic!("unexpected sequence number: {seq}"),
136 }
137 }
138 }
139
140 // Disable it again
141 glean.set_ping_enabled(&nofollows_ping, false);
142 counter.add_sync(&glean, 5);
143 assert!(manual_ping.submit_sync(&glean, None));
144 let queued_pings = get_queued_pings(glean.get_data_path()).unwrap();
145 // The 2 previous `manual` pings, the `nofollows` was removed, plus the new `manual` one
146 assert_eq!(3, queued_pings.len());
147
148 // Check that all follows are as expected.
149 // We cannot guarantee order, so we need to look at some values
150 for (_url, json, info) in queued_pings {
151 let Some(obj) = info else {
152 panic!("no ping info")
153 };
154 let counter_val = json["metrics"]["counter"]["local.counter"]
155 .as_i64()
156 .unwrap();
157
158 assert_ne!("nofollows", obj["ping_name"].as_str().unwrap());
159 let seq = json["ping_info"]["seq"].as_i64().unwrap();
160
161 match seq {
162 0 => assert_eq!(1, counter_val),
163 1 => assert_eq!(3, counter_val),
164 2 => assert_eq!(8, counter_val),
165 _ => panic!("unexpected sequence number: {seq}"),
166 }
167 }
168 }
169
170 #[test]
171 fn queued_nofollows_pings_are_not_removed() {
172 let (mut glean, t) = new_glean(None);
173
174 let nofollows_ping = nofollows_ping(&mut glean);
175 let manual_ping = manual_ping(&mut glean);
176
177 glean.set_ping_enabled(&nofollows_ping, true);
178
179 // We need to store a metric as an empty ping is not stored.
180 let counter = CounterMetric::new(CommonMetricData {
181 name: "counter".into(),
182 category: "local".into(),
183 send_in_pings: vec!["manual".into(), "nofollows".into()],
184 lifetime: Lifetime::Application,
185 ..Default::default()
186 });
187
188 // Trigger a ping with data.
189 counter.add_sync(&glean, 1);
190 assert!(manual_ping.submit_sync(&glean, None));
191 assert!(nofollows_ping.submit_sync(&glean, None));
192 assert_eq!(2, get_queued_pings(glean.get_data_path()).unwrap().len());
193
194 glean.set_upload_enabled(false);
195 assert_eq!(1, get_queued_pings(glean.get_data_path()).unwrap().len());
196
197 // Ping is still there after a Glean restart
198 let (glean, _t) = new_glean(Some(t));
199 assert_eq!(1, get_queued_pings(glean.get_data_path()).unwrap().len());
200 }