Debian Codegraph @

third_party/rust/glean/tests/memory_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, ErrorType};
14
15 mod metrics {
16 use glean::private::*;
17 use glean::{Lifetime, MemoryUnit};
18 use glean_core::CommonMetricData;
19 use once_cell::sync::Lazy;
20
21 #[allow(non_upper_case_globals)]
22 pub static measure: Lazy<MemoryDistributionMetric> = Lazy::new(|| {
23 MemoryDistributionMetric::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 MemoryUnit::Byte,
33 )
34 });
35 }
36
37 /// Test scenario: Ensure buffered accumulation works.
38 #[test]
39 fn buffered_memory_distribution_works() {
40 common::enable_test_logging();
41
42 let dir = tempfile::tempdir().unwrap();
43 let tmpname = dir.path().to_path_buf();
44
45 let cfg = ConfigurationBuilder::new(true, tmpname, "firefox-desktop")
46 .with_server_endpoint("invalid-test-host")
47 .build();
48 common::initialize(cfg);
49
50 let mut buffer = metrics::measure.start_buffer();
51
52 for _ in 0..3 {
53 buffer.accumulate(10);
54 }
55
56 // Nothing accumulated while buffer uncommitted.
57 assert_eq!(None, metrics::measure.test_get_value(None));
58
59 // Commit buffer by dropping it.
60 drop(buffer);
61
62 let data = metrics::measure.test_get_value(None).unwrap();
63 assert_eq!(3, data.count);
64 assert_eq!(30, data.sum);
65
66 let mut buffer = metrics::measure.start_buffer();
67 for _ in 0..3 {
68 buffer.accumulate(10);
69 }
70 // Don't record any of this data into the metric.
71 buffer.abandon();
72
73 // Metric is unchanged.
74 let data = metrics::measure.test_get_value(None).unwrap();
75 assert_eq!(3, data.count);
76 assert_eq!(30, data.sum);
77
78 // Check error recording.
79 let mut buffer = metrics::measure.start_buffer();
80 let large_sample = (1 << 40) + 1;
81 for _ in 0..10 {
82 buffer.accumulate(large_sample);
83 }
84 drop(buffer);
85
86 let data = metrics::measure.test_get_value(None).unwrap();
87 assert_eq!(13, data.count);
88
89 assert_eq!(
90 10,
91 metrics::measure.test_get_num_recorded_errors(ErrorType::InvalidValue)
92 );
93
94 glean::shutdown(); // Cleanly shut down at the end of the test.
95 }