Debian Codegraph @

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