Debian Codegraph @

third_party/rust/glean/tests/simple.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 /// Some user metrics.
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 initialization: Lazy<TimespanMetric> = Lazy::new(|| {
24 TimespanMetric::new(
25 CommonMetricData {
26 name: "initialization".into(),
27 category: "sample".into(),
28 send_in_pings: vec!["validation".into()],
29 lifetime: Lifetime::Ping,
30 disabled: false,
31 ..Default::default()
32 },
33 TimeUnit::Nanosecond,
34 )
35 });
36 }
37
38 mod pings {
39 use super::*;
40 use glean::private::PingType;
41 use once_cell::sync::Lazy;
42
43 #[allow(non_upper_case_globals)]
44 pub static validation: Lazy<PingType> = Lazy::new(|| {
45 common::PingBuilder::new("validation")
46 .with_send_if_empty(true)
47 .build()
48 });
49 }
50
51 /// Test scenario: A clean run
52 ///
53 /// The app is initialized, in turn Glean gets initialized without problems.
54 /// Some data is recorded (before and after initialization).
55 /// And later the whole process is shutdown.
56 #[test]
57 fn simple_lifecycle() {
58 common::enable_test_logging();
59
60 metrics::initialization.start();
61
62 // Create a custom configuration to use a validating uploader.
63 let dir = tempfile::tempdir().unwrap();
64 let tmpname = dir.path().to_path_buf();
65
66 _ = &*pings::validation;
67 let cfg = ConfigurationBuilder::new(true, tmpname, "firefox-desktop")
68 .with_server_endpoint("invalid-test-host")
69 .build();
70 common::initialize(cfg);
71
72 metrics::initialization.stop();
73
74 // This would never be called outside of tests,
75 // but it's the only way we can really test it's working right now.
76 assert!(metrics::initialization.test_get_value(None).is_some());
77
78 pings::validation.submit(None);
79 assert!(metrics::initialization.test_get_value(None).is_none());
80
81 glean::shutdown();
82 }