Debian Codegraph @

third_party/rust/glean-core/tests/string.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 // SKIPPED from glean-ac: string deserializer should correctly parse integers
16 // This test doesn't really apply to rkv
17
18 #[test]
19 fn string_serializer_should_correctly_serialize_strings() {
20 let (mut tempdir, _) = tempdir();
21
22 {
23 // We give tempdir to the `new_glean` function...
24 let (glean, dir) = new_glean(Some(tempdir));
25 // And then we get it back once that function returns.
26 tempdir = dir;
27
28 let metric = StringMetric::new(CommonMetricData {
29 name: "string_metric".into(),
30 category: "telemetry".into(),
31 send_in_pings: vec!["store1".into()],
32 disabled: false,
33 lifetime: Lifetime::User,
34 ..Default::default()
35 });
36
37 metric.set_sync(&glean, "test_string_value");
38
39 let snapshot = StorageManager
40 .snapshot_as_json(glean.storage(), "store1", true)
41 .unwrap();
42 assert_eq!(
43 json!({"string": {"telemetry.string_metric": "test_string_value"}}),
44 snapshot
45 );
46 }
47
48 // Make a new Glean instance here, which should force reloading of the data from disk
49 // so we can ensure it persisted, because it has User lifetime
50 {
51 let (glean, _t) = new_glean(Some(tempdir));
52 let snapshot = StorageManager
53 .snapshot_as_json(glean.storage(), "store1", true)
54 .unwrap();
55 assert_eq!(
56 json!({"string": {"telemetry.string_metric": "test_string_value"}}),
57 snapshot
58 );
59 }
60 }
61
62 #[test]
63 fn set_properly_sets_the_value_in_all_stores() {
64 let (glean, _t) = new_glean(None);
65 let store_names: Vec<String> = vec!["store1".into(), "store2".into()];
66
67 let metric = StringMetric::new(CommonMetricData {
68 name: "string_metric".into(),
69 category: "telemetry".into(),
70 send_in_pings: store_names.clone(),
71 disabled: false,
72 lifetime: Lifetime::Ping,
73 ..Default::default()
74 });
75
76 metric.set_sync(&glean, "test_string_value");
77
78 // Check that the data was correctly set in each store.
79 for store_name in store_names {
80 let snapshot = StorageManager
81 .snapshot_as_json(glean.storage(), &store_name, true)
82 .unwrap();
83
84 assert_eq!(
85 json!({"string": {"telemetry.string_metric": "test_string_value"}}),
86 snapshot
87 );
88 }
89 }
90
91 // SKIPPED from glean-ac: strings are serialized in the correct JSON format
92 // Completely redundant with other tests.
93
94 #[test]
95 fn long_string_values_are_truncated() {
96 let (glean, _t) = new_glean(None);
97
98 let metric = StringMetric::new(CommonMetricData {
99 name: "string_metric".into(),
100 category: "telemetry".into(),
101 send_in_pings: vec!["store1".into()],
102 disabled: false,
103 lifetime: Lifetime::Ping,
104 ..Default::default()
105 });
106
107 let test_string = "01234567890".repeat(26);
108 metric.set_sync(&glean, test_string.clone());
109
110 // Check that data was truncated
111 assert_eq!(
112 test_string[..255],
113 metric.get_value(&glean, "store1").unwrap()
114 );
115
116 // Make sure that the errors have been recorded
117 assert_eq!(
118 Ok(1),
119 test_get_num_recorded_errors(&glean, metric.meta(), ErrorType::InvalidOverflow)
120 );
121 }