Debian Codegraph @

third_party/rust/glean-core/tests/counter.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 serde_json::json;
9
10 use glean_core::metrics::*;
11 use glean_core::storage::StorageManager;
12 use glean_core::{test_get_num_recorded_errors, ErrorType};
13 use glean_core::{CommonMetricData, Lifetime};
14
15 // Tests ported from glean-ac
16
17 // SKIPPED from glean-ac: counter deserializer should correctly parse integers
18 // This test doesn't really apply to rkv
19
20 #[test]
21 fn counter_serializer_should_correctly_serialize_counters() {
22 let (mut tempdir, _) = tempdir();
23
24 {
25 // We give tempdir to the `new_glean` function...
26 let (glean, dir) = new_glean(Some(tempdir));
27 // And then we get it back once that function returns.
28 tempdir = dir;
29
30 let metric = CounterMetric::new(CommonMetricData {
31 name: "counter_metric".into(),
32 category: "telemetry".into(),
33 send_in_pings: vec!["store1".into()],
34 disabled: false,
35 lifetime: Lifetime::User,
36 ..Default::default()
37 });
38
39 metric.add_sync(&glean, 1);
40
41 let snapshot = StorageManager
42 .snapshot_as_json(glean.storage(), "store1", true)
43 .unwrap();
44 assert_eq!(
45 json!({"counter": {"telemetry.counter_metric": 1}}),
46 snapshot
47 );
48 }
49
50 // Make a new Glean instance here, which should force reloading of the data from disk
51 // so we can ensure it persisted, because it has User lifetime
52 {
53 let (glean, _t) = new_glean(Some(tempdir));
54 let snapshot = StorageManager
55 .snapshot_as_json(glean.storage(), "store1", true)
56 .unwrap();
57 assert_eq!(
58 json!({"counter": {"telemetry.counter_metric": 1}}),
59 snapshot
60 );
61 }
62 }
63
64 #[test]
65 fn set_value_properly_sets_the_value_in_all_stores() {
66 let (glean, _t) = new_glean(None);
67 let store_names: Vec<String> = vec!["store1".into(), "store2".into()];
68
69 let metric = CounterMetric::new(CommonMetricData {
70 name: "counter_metric".into(),
71 category: "telemetry".into(),
72 send_in_pings: store_names.clone(),
73 disabled: false,
74 lifetime: Lifetime::Ping,
75 ..Default::default()
76 });
77
78 metric.add_sync(&glean, 1);
79
80 for store_name in store_names {
81 let snapshot = StorageManager
82 .snapshot_as_json(glean.storage(), &store_name, true)
83 .unwrap();
84
85 assert_eq!(
86 json!({"counter": {"telemetry.counter_metric": 1}}),
87 snapshot
88 );
89 }
90 }
91
92 // SKIPPED from glean-ac: counters are serialized in the correct JSON format
93 // Completely redundant with other tests.
94
95 #[test]
96 fn counters_must_not_increment_when_passed_zero_or_negative() {
97 let (glean, _t) = new_glean(None);
98
99 let metric = CounterMetric::new(CommonMetricData {
100 name: "counter_metric".into(),
101 category: "telemetry".into(),
102 send_in_pings: vec!["store1".into()],
103 disabled: false,
104 lifetime: Lifetime::Application,
105 ..Default::default()
106 });
107
108 // Attempt to increment the counter with zero
109 metric.add_sync(&glean, 0);
110 // Check that nothing was recorded
111 assert!(metric.get_value(&glean, Some("store1")).is_none());
112
113 // Attempt to increment the counter with negative
114 metric.add_sync(&glean, -1);
115 // Check that nothing was recorded
116 assert!(metric.get_value(&glean, Some("store1")).is_none());
117
118 // Attempt increment counter properly
119 metric.add_sync(&glean, 1);
120 // Check that nothing was recorded
121 assert_eq!(1, metric.get_value(&glean, Some("store1")).unwrap());
122
123 // Make sure that the error has been recorded
124 assert_eq!(
125 Ok(1),
126 test_get_num_recorded_errors(&glean, metric.meta(), ErrorType::InvalidValue)
127 );
128 }
129
130 // New tests for glean-core below
131
132 #[test]
133 fn transformation_works() {
134 let (glean, _t) = new_glean(None);
135
136 let counter: CounterMetric = CounterMetric::new(CommonMetricData {
137 name: "transformation".into(),
138 category: "local".into(),
139 send_in_pings: vec!["store1".into(), "store2".into()],
140 ..Default::default()
141 });
142
143 counter.add_sync(&glean, 2);
144
145 assert_eq!(2, counter.get_value(&glean, Some("store1")).unwrap());
146 assert_eq!(2, counter.get_value(&glean, Some("store2")).unwrap());
147
148 // Clearing just one store
149 let _ = StorageManager
150 .snapshot_as_json(glean.storage(), "store1", true)
151 .unwrap();
152
153 counter.add_sync(&glean, 2);
154
155 assert_eq!(2, counter.get_value(&glean, Some("store1")).unwrap());
156 assert_eq!(4, counter.get_value(&glean, Some("store2")).unwrap());
157 }
158
159 #[test]
160 fn saturates_at_boundary() {
161 let (glean, _t) = new_glean(None);
162
163 let counter: CounterMetric = CounterMetric::new(CommonMetricData {
164 name: "transformation".into(),
165 category: "local".into(),
166 send_in_pings: vec!["store1".into()],
167 ..Default::default()
168 });
169
170 counter.add_sync(&glean, 2);
171 counter.add_sync(&glean, i32::MAX);
172
173 assert_eq!(i32::MAX, counter.get_value(&glean, Some("store1")).unwrap());
174 }