Debian Codegraph @

third_party/rust/glean/src/common_test.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 use crate::private::PingType;
6 use crate::ClientInfoMetrics;
7 use crate::{Configuration, ConfigurationBuilder};
8 use std::sync::{Mutex, MutexGuard};
9
10 use once_cell::sync::Lazy;
11
12 pub(crate) const GLOBAL_APPLICATION_ID: &str = "org.mozilla.rlb.test";
13
14 // Because Glean uses a global-singleton, we need to run the tests one-by-one to
15 // avoid different tests stomping over each other.
16 // This is only an issue because we're resetting Glean, this cannot happen in normal
17 // use of the RLB.
18 //
19 // We use a global lock to force synchronization of all tests, even if run multi-threaded.
20 // This allows us to run without `--test-threads 1`.`
21 pub(crate) fn lock_test() -> MutexGuard<'static, ()> {
22 static GLOBAL_LOCK: Lazy<Mutex<()>> = Lazy::new(|| Mutex::new(()));
23
24 // This is going to be called from all the tests: make sure
25 // to enable logging.
26 env_logger::try_init().ok();
27
28 let lock = GLOBAL_LOCK.lock().unwrap();
29
30 lock
31 }
32
33 // Create a new instance of Glean with a temporary directory.
34 // We need to keep the `TempDir` alive, so that it's not deleted before we stop using it.
35 pub(crate) fn new_glean(
36 configuration: Option<Configuration>,
37 clear_stores: bool,
38 ) -> tempfile::TempDir {
39 let dir = tempfile::tempdir().unwrap();
40 let tmpname = dir.path().to_path_buf();
41
42 let cfg = match configuration {
43 Some(c) => c,
44 None => ConfigurationBuilder::new(true, tmpname, GLOBAL_APPLICATION_ID)
45 .with_server_endpoint("invalid-test-host")
46 .build(),
47 };
48
49 _ = PingType::new(
50 "store1",
51 true,
52 true,
53 true,
54 true,
55 true,
56 vec![],
57 vec![],
58 true,
59 vec![],
60 );
61
62 crate::test_reset_glean(cfg, ClientInfoMetrics::unknown(), clear_stores);
63 dir
64 }