Debian Codegraph @

third_party/rust/glean/tests/common/mod.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 // #[allow(dead_code)] is required on this module as a workaround for
6 // https://github.com/rust-lang/rust/issues/46379
7 #![allow(dead_code)]
8
9 use std::{panic, process};
10
11 use glean::{private::PingType, ClientInfoMetrics, Configuration};
12
13 /// Initialize the env logger for a test environment.
14 ///
15 /// When testing we want all logs to go to stdout/stderr by default.
16 pub fn enable_test_logging() {
17 let _ = env_logger::builder().is_test(true).try_init();
18 }
19
20 /// Install a panic handler that exits the whole process when a panic occurs.
21 ///
22 /// This causes the process to exit even if a thread panics.
23 /// This is similar to the `panic=abort` configuration, but works in the default configuration
24 /// (as used by `cargo test`).
25 fn install_panic_handler() {
26 let orig_hook = panic::take_hook();
27 panic::set_hook(Box::new(move |panic_info| {
28 // invoke the default handler and exit the process
29 orig_hook(panic_info);
30 process::exit(1);
31 }));
32 }
33
34 /// Create a new instance of Glean.
35 pub fn initialize(cfg: Configuration) {
36 // Ensure panics in threads, such as the init thread or the dispatcher, cause the process to
37 // exit.
38 //
39 // Otherwise in case of a panic in a thread the integration test will just hang.
40 // CI will terminate it after a timeout, but why stick around if we know nothing is happening?
41 install_panic_handler();
42
43 // Use some default values to make our life easier a bit.
44 let client_info = ClientInfoMetrics {
45 app_build: "1.0.0".to_string(),
46 app_display_version: "1.0.0".to_string(),
47 channel: Some("testing".to_string()),
48 locale: Some("xx-XX".to_string()),
49 };
50
51 _ = PingBuilder::new("store1").with_send_if_empty(true).build();
52 glean::initialize(cfg, client_info);
53 }
54
55 pub struct PingBuilder {
56 name: String,
57 include_client_id: bool,
58 send_if_empty: bool,
59 precise_timestamps: bool,
60 include_info_sections: bool,
61 enabled: bool,
62 schedules_pings: Vec<String>,
63 reason_codes: Vec<String>,
64 follows_collection_enabled: bool,
65 uploader_capabilities: Vec<String>,
66 }
67
68 impl PingBuilder {
69 pub fn new(name: &str) -> Self {
70 Self {
71 name: name.to_string(),
72 include_client_id: true,
73 send_if_empty: false,
74 precise_timestamps: true,
75 include_info_sections: true,
76 enabled: true,
77 schedules_pings: vec![],
78 reason_codes: vec![],
79 follows_collection_enabled: true,
80 uploader_capabilities: vec![],
81 }
82 }
83
84 pub fn build(self) -> PingType {
85 PingType::new(
86 self.name,
87 self.include_client_id,
88 self.send_if_empty,
89 self.precise_timestamps,
90 self.include_info_sections,
91 self.enabled,
92 self.schedules_pings,
93 self.reason_codes,
94 self.follows_collection_enabled,
95 self.uploader_capabilities,
96 )
97 }
98
99 pub fn with_send_if_empty(mut self, value: bool) -> Self {
100 self.send_if_empty = value;
101 self
102 }
103
104 pub fn with_uploader_capabilities(mut self, value: Vec<String>) -> Self {
105 self.uploader_capabilities = value;
106 self
107 }
108
109 pub fn with_include_info_sections(mut self, value: bool) -> Self {
110 self.include_info_sections = value;
111 self
112 }
113
114 pub fn with_enabled(mut self, value: bool) -> Self {
115 self.enabled = value;
116 self
117 }
118
119 pub fn with_follows_collection_enabled(mut self, value: bool) -> Self {
120 self.follows_collection_enabled = value;
121 self
122 }
123
124 pub fn with_include_client_id(mut self, value: bool) -> Self {
125 self.include_client_id = value;
126 self
127 }
128 }