Debian Codegraph @

third_party/rust/glean-core/tests/rate.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::{test_get_num_recorded_errors, ErrorType};
11
12 #[test]
13 fn rate_smoke() {
14 let (glean, _t) = new_glean(None);
15
16 let metric: RateMetric = RateMetric::new(CommonMetricData {
17 name: "rate".into(),
18 category: "test".into(),
19 send_in_pings: vec!["store1".into()],
20 ..Default::default()
21 });
22
23 // Adding 0 doesn't error.
24 metric.add_to_numerator_sync(&glean, 0);
25 metric.add_to_denominator_sync(&glean, 0);
26
27 assert!(test_get_num_recorded_errors(&glean, metric.meta(), ErrorType::InvalidValue).is_err());
28
29 // Adding a negative value errors.
30 metric.add_to_numerator_sync(&glean, -1);
31 metric.add_to_denominator_sync(&glean, -1);
32
33 assert_eq!(
34 Ok(2),
35 test_get_num_recorded_errors(&glean, metric.meta(), ErrorType::InvalidValue),
36 );
37
38 // Getting the value returns 0s if that's all we have.
39 assert_eq!(metric.get_value(&glean, None), Some((0, 0).into()));
40
41 // And normal values of course work.
42 metric.add_to_numerator_sync(&glean, 22);
43 metric.add_to_denominator_sync(&glean, 7);
44
45 assert_eq!(metric.get_value(&glean, None), Some((22, 7).into()));
46 }
47
48 #[test]
49 fn numerator_smoke() {
50 let (glean, _t) = new_glean(None);
51
52 let metric: NumeratorMetric = NumeratorMetric::new(CommonMetricData {
53 name: "rate".into(),
54 category: "test".into(),
55 send_in_pings: vec!["store1".into()],
56 ..Default::default()
57 });
58
59 // Adding 0 doesn't error.
60 metric.add_to_numerator_sync(&glean, 0);
61
62 assert!(test_get_num_recorded_errors(&glean, metric.meta(), ErrorType::InvalidValue).is_err());
63
64 // Adding a negative value errors.
65 metric.add_to_numerator_sync(&glean, -1);
66
67 assert_eq!(
68 Ok(1),
69 test_get_num_recorded_errors(&glean, metric.meta(), ErrorType::InvalidValue),
70 );
71
72 // Getting the value returns 0s if that's all we have.
73 let data = metric.get_value(&glean, None).unwrap();
74 assert_eq!(0, data.numerator);
75 assert_eq!(0, data.denominator);
76
77 // And normal values of course work.
78 metric.add_to_numerator_sync(&glean, 22);
79
80 let data = metric.get_value(&glean, None).unwrap();
81 assert_eq!(22, data.numerator);
82 assert_eq!(0, data.denominator);
83 }
84
85 #[test]
86 fn denominator_smoke() {
87 let (glean, _t) = new_glean(None);
88
89 let meta1 = CommonMetricData {
90 name: "rate1".into(),
91 category: "test".into(),
92 send_in_pings: vec!["store1".into()],
93 ..Default::default()
94 };
95
96 let meta2 = CommonMetricData {
97 name: "rate2".into(),
98 category: "test".into(),
99 send_in_pings: vec!["store1".into()],
100 ..Default::default()
101 };
102
103 // This acts like a normal counter.
104 let denom: DenominatorMetric = DenominatorMetric::new(
105 CommonMetricData {
106 name: "counter".into(),
107 category: "test".into(),
108 send_in_pings: vec!["store1".into()],
109 ..Default::default()
110 },
111 vec![meta1.clone(), meta2.clone()],
112 );
113
114 let num1 = NumeratorMetric::new(meta1);
115 let num2 = NumeratorMetric::new(meta2);
116
117 num1.add_to_numerator_sync(&glean, 3);
118 num2.add_to_numerator_sync(&glean, 5);
119
120 denom.add_sync(&glean, 7);
121
122 // no errors.
123 assert!(test_get_num_recorded_errors(&glean, num1.meta(), ErrorType::InvalidValue).is_err());
124 assert!(test_get_num_recorded_errors(&glean, num2.meta(), ErrorType::InvalidValue).is_err());
125
126 // Getting the value returns what is stored
127 let data = num1.get_value(&glean, None).unwrap();
128 assert_eq!(3, data.numerator);
129 assert_eq!(7, data.denominator);
130
131 let data = num2.get_value(&glean, None).unwrap();
132 assert_eq!(5, data.numerator);
133 assert_eq!(7, data.denominator);
134 }