summaryrefslogtreecommitdiffstats
path: root/third_party/rust/glean/tests/common
diff options
context:
space:
mode:
authorDaniel Baumann <daniel.baumann@progress-linux.org>2024-04-07 19:33:14 +0000
committerDaniel Baumann <daniel.baumann@progress-linux.org>2024-04-07 19:33:14 +0000
commit36d22d82aa202bb199967e9512281e9a53db42c9 (patch)
tree105e8c98ddea1c1e4784a60a5a6410fa416be2de /third_party/rust/glean/tests/common
parentInitial commit. (diff)
downloadfirefox-esr-36d22d82aa202bb199967e9512281e9a53db42c9.tar.xz
firefox-esr-36d22d82aa202bb199967e9512281e9a53db42c9.zip
Adding upstream version 115.7.0esr.upstream/115.7.0esr
Signed-off-by: Daniel Baumann <daniel.baumann@progress-linux.org>
Diffstat (limited to 'third_party/rust/glean/tests/common')
-rw-r--r--third_party/rust/glean/tests/common/mod.rs51
1 files changed, 51 insertions, 0 deletions
diff --git a/third_party/rust/glean/tests/common/mod.rs b/third_party/rust/glean/tests/common/mod.rs
new file mode 100644
index 0000000000..cc02946d2c
--- /dev/null
+++ b/third_party/rust/glean/tests/common/mod.rs
@@ -0,0 +1,51 @@
+// This Source Code Form is subject to the terms of the Mozilla Public
+// License, v. 2.0. If a copy of the MPL was not distributed with this
+// file, You can obtain one at https://mozilla.org/MPL/2.0/.
+
+// #[allow(dead_code)] is required on this module as a workaround for
+// https://github.com/rust-lang/rust/issues/46379
+#![allow(dead_code)]
+
+use std::{panic, process};
+
+use glean::{ClientInfoMetrics, Configuration};
+
+/// Initialize the env logger for a test environment.
+///
+/// When testing we want all logs to go to stdout/stderr by default.
+pub fn enable_test_logging() {
+ let _ = env_logger::builder().is_test(true).try_init();
+}
+
+/// Install a panic handler that exits the whole process when a panic occurs.
+///
+/// This causes the process to exit even if a thread panics.
+/// This is similar to the `panic=abort` configuration, but works in the default configuration
+/// (as used by `cargo test`).
+fn install_panic_handler() {
+ let orig_hook = panic::take_hook();
+ panic::set_hook(Box::new(move |panic_info| {
+ // invoke the default handler and exit the process
+ orig_hook(panic_info);
+ process::exit(1);
+ }));
+}
+
+/// Create a new instance of Glean.
+pub fn initialize(cfg: Configuration) {
+ // Ensure panics in threads, such as the init thread or the dispatcher, cause the process to
+ // exit.
+ //
+ // Otherwise in case of a panic in a thread the integration test will just hang.
+ // CI will terminate it after a timeout, but why stick around if we know nothing is happening?
+ install_panic_handler();
+
+ // Use some default values to make our life easier a bit.
+ let client_info = ClientInfoMetrics {
+ app_build: "1.0.0".to_string(),
+ app_display_version: "1.0.0".to_string(),
+ channel: Some("testing".to_string()),
+ };
+
+ glean::initialize(cfg, client_info);
+}