Debian Codegraph @

third_party/rust/glean-core/tests/quantity.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: quantity deserializer should correctly parse integers
18 // This test doesn't really apply to rkv
19
20 #[test]
21 fn quantity_serializer_should_correctly_serialize_quantities() {
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 = QuantityMetric::new(CommonMetricData {
31 name: "quantity_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.set_sync(&glean, 1);
40
41 let snapshot = StorageManager
42 .snapshot_as_json(glean.storage(), "store1", true)
43 .unwrap();
44 assert_eq!(
45 json!({"quantity": {"telemetry.quantity_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!({"quantity": {"telemetry.quantity_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 = QuantityMetric::new(CommonMetricData {
70 name: "quantity_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.set_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!({"quantity": {"telemetry.quantity_metric": 1}}),
87 snapshot
88 );
89 }
90 }
91
92 // SKIPPED from glean-ac: quantities are serialized in the correct JSON format
93 // Completely redundant with other tests.
94
95 #[test]
96 fn quantities_must_not_set_when_passed_negative() {
97 let (glean, _t) = new_glean(None);
98
99 let metric = QuantityMetric::new(CommonMetricData {
100 name: "quantity_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 set the quantity with negative
109 metric.set_sync(&glean, -1);
110 // Check that nothing was recorded
111 assert!(metric.get_value(&glean, "store1").is_none());
112
113 // Make sure that the errors have been recorded
114 assert_eq!(
115 Ok(1),
116 test_get_num_recorded_errors(&glean, metric.meta(), ErrorType::InvalidValue)
117 );
118 }