Debian Codegraph @

third_party/rust/glean-core/tests/storage.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::{CommonMetricData, Lifetime};
13
14 #[test]
15 fn snapshot_returns_none_if_nothing_is_recorded_in_the_store() {
16 let (glean, _t) = new_glean(None);
17 assert!(StorageManager
18 .snapshot(glean.storage(), "unknown_store", true)
19 .is_none())
20 }
21
22 #[test]
23 fn can_snapshot() {
24 let (glean, _t) = new_glean(None);
25
26 let local_metric = StringMetric::new(CommonMetricData {
27 name: "can_snapshot_local_metric".into(),
28 category: "local".into(),
29 send_in_pings: vec!["store1".into()],
30 ..Default::default()
31 });
32
33 local_metric.set_sync(&glean, "snapshot 42");
34
35 assert!(StorageManager
36 .snapshot(glean.storage(), "store1", true)
37 .is_some())
38 }
39
40 #[test]
41 fn snapshot_correctly_clears_the_stores() {
42 let (glean, _t) = new_glean(None);
43 let store_names: Vec<String> = vec!["store1".into(), "store2".into()];
44
45 let metric = CounterMetric::new(CommonMetricData {
46 name: "metric".into(),
47 category: "telemetry".into(),
48 send_in_pings: store_names,
49 disabled: false,
50 lifetime: Lifetime::Ping,
51 ..Default::default()
52 });
53
54 metric.add_sync(&glean, 1);
55
56 // Get the snapshot from "store1" and clear it.
57 let snapshot = StorageManager.snapshot(glean.storage(), "store1", true);
58 assert!(snapshot.is_some());
59 // Check that getting a new snapshot for "store1" returns an empty store.
60 assert!(StorageManager
61 .snapshot(glean.storage(), "store1", false)
62 .is_none());
63 // Check that we get the right data from both the stores. Clearing "store1" must
64 // not clear "store2" as well.
65 let snapshot2 = StorageManager.snapshot(glean.storage(), "store2", true);
66 assert!(snapshot2.is_some());
67 }
68
69 #[test]
70 fn storage_is_thread_safe() {
71 use std::sync::{Arc, Barrier, Mutex};
72 use std::thread;
73
74 let (glean, _t) = new_glean(None);
75 let glean = Arc::new(Mutex::new(glean));
76
77 let threadsafe_metric = CounterMetric::new(CommonMetricData {
78 name: "threadsafe".into(),
79 category: "global".into(),
80 send_in_pings: vec!["store1".into(), "metrics".into()],
81 ..Default::default()
82 });
83 let threadsafe_metric = Arc::new(threadsafe_metric);
84
85 let barrier = Arc::new(Barrier::new(2));
86 let c = barrier.clone();
87 let threadsafe_metric_clone = threadsafe_metric.clone();
88 let glean_clone = glean.clone();
89 let child = thread::spawn(move || {
90 threadsafe_metric_clone.add_sync(&glean_clone.lock().unwrap(), 1);
91 c.wait();
92 threadsafe_metric_clone.add_sync(&glean_clone.lock().unwrap(), 1);
93 });
94
95 threadsafe_metric.add_sync(&glean.lock().unwrap(), 1);
96 barrier.wait();
97 threadsafe_metric.add_sync(&glean.lock().unwrap(), 1);
98
99 child.join().unwrap();
100
101 let snapshot = StorageManager
102 .snapshot_as_json(glean.lock().unwrap().storage(), "store1", true)
103 .unwrap();
104 assert_eq!(json!({"counter": { "global.threadsafe": 4 }}), snapshot);
105 }