Debian Codegraph @

third_party/rust/glean/tests/custom_distribution_buffered.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 //! This integration test should model how the RLB is used when embedded in another Rust application
6 //! (e.g. FOG/Firefox Desktop).
7 //!
8 //! We write a single test scenario per file to avoid any state keeping across runs
9 //! (different files run as different processes).
10
11 mod common;
12
13 use glean::ConfigurationBuilder;
14
15 mod metrics {
16 use glean::private::*;
17 use glean::{HistogramType, Lifetime};
18 use glean_core::CommonMetricData;
19 use once_cell::sync::Lazy;
20
21 #[allow(non_upper_case_globals)]
22 pub static measure: Lazy<CustomDistributionMetric> = Lazy::new(|| {
23 CustomDistributionMetric::new(
24 CommonMetricData {
25 name: "measure".into(),
26 category: "sample".into(),
27 send_in_pings: vec!["store1".into()],
28 lifetime: Lifetime::Ping,
29 disabled: false,
30 ..Default::default()
31 },
32 0,
33 100,
34 100,
35 HistogramType::Linear,
36 )
37 });
38 }
39
40 /// Test scenario: Ensure buffered accumulation works.
41 #[test]
42 fn buffered_memory_distribution_works() {
43 common::enable_test_logging();
44
45 let dir = tempfile::tempdir().unwrap();
46 let tmpname = dir.path().to_path_buf();
47
48 let cfg = ConfigurationBuilder::new(true, tmpname, "firefox-desktop")
49 .with_server_endpoint("invalid-test-host")
50 .build();
51 common::initialize(cfg);
52
53 let mut buffer = metrics::measure.start_buffer();
54
55 for _ in 0..3 {
56 buffer.accumulate(10);
57 }
58
59 // Nothing accumulated while buffer uncommitted.
60 assert_eq!(None, metrics::measure.test_get_value(None));
61
62 // Commit buffer by dropping it.
63 drop(buffer);
64
65 let data = metrics::measure.test_get_value(None).unwrap();
66 assert_eq!(3, data.count);
67 assert_eq!(30, data.sum);
68
69 let mut buffer = metrics::measure.start_buffer();
70 for _ in 0..3 {
71 buffer.accumulate(10);
72 }
73 // Don't record any of this data into the metric.
74 buffer.abandon();
75
76 // Metric is unchanged.
77 let data = metrics::measure.test_get_value(None).unwrap();
78 assert_eq!(3, data.count);
79 assert_eq!(30, data.sum);
80
81 glean::shutdown(); // Cleanly shut down at the end of the test.
82 }