summaryrefslogtreecommitdiffstats
path: root/toolkit/components/glean/api/src/common_test.rs
diff options
context:
space:
mode:
authorDaniel Baumann <daniel.baumann@progress-linux.org>2024-04-07 09:22:09 +0000
committerDaniel Baumann <daniel.baumann@progress-linux.org>2024-04-07 09:22:09 +0000
commit43a97878ce14b72f0981164f87f2e35e14151312 (patch)
tree620249daf56c0258faa40cbdcf9cfba06de2a846 /toolkit/components/glean/api/src/common_test.rs
parentInitial commit. (diff)
downloadfirefox-upstream.tar.xz
firefox-upstream.zip
Adding upstream version 110.0.1.upstream/110.0.1upstream
Signed-off-by: Daniel Baumann <daniel.baumann@progress-linux.org>
Diffstat (limited to 'toolkit/components/glean/api/src/common_test.rs')
-rw-r--r--toolkit/components/glean/api/src/common_test.rs52
1 files changed, 52 insertions, 0 deletions
diff --git a/toolkit/components/glean/api/src/common_test.rs b/toolkit/components/glean/api/src/common_test.rs
new file mode 100644
index 0000000000..49b7416d72
--- /dev/null
+++ b/toolkit/components/glean/api/src/common_test.rs
@@ -0,0 +1,52 @@
+// 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/.
+
+use std::sync::{Mutex, MutexGuard};
+
+use once_cell::sync::Lazy;
+
+const GLOBAL_APPLICATION_ID: &str = "org.mozilla.firefox.test";
+
+/// UGLY HACK.
+/// We use a global lock to force synchronization of all tests, even if run multi-threaded.
+/// This allows us to run without `--test-threads 1`.`
+pub fn lock_test() -> (MutexGuard<'static, ()>, tempfile::TempDir) {
+ static GLOBAL_LOCK: Lazy<Mutex<()>> = Lazy::new(|| Mutex::new(()));
+
+ let lock = GLOBAL_LOCK.lock().unwrap();
+
+ let dir = setup_glean(None);
+ (lock, dir)
+}
+
+// Create a new instance of Glean with a temporary directory.
+// We need to keep the `TempDir` alive, so that it's not deleted before we stop using it.
+fn setup_glean(tempdir: Option<tempfile::TempDir>) -> tempfile::TempDir {
+ let dir = match tempdir {
+ Some(tempdir) => tempdir,
+ None => tempfile::tempdir().unwrap(),
+ };
+ let tmpname = dir.path().to_path_buf();
+
+ let cfg = glean::Configuration {
+ upload_enabled: true,
+ data_path: tmpname,
+ application_id: GLOBAL_APPLICATION_ID.into(),
+ max_events: None,
+ delay_ping_lifetime_io: false,
+ server_endpoint: None,
+ uploader: None,
+ use_core_mps: false,
+ };
+
+ let client_info = glean::ClientInfoMetrics {
+ app_build: "test-build".into(),
+ app_display_version: "1.2.3".into(),
+ channel: None,
+ };
+
+ glean::test_reset_glean(cfg, client_info, true);
+
+ dir
+}