Skip to content

Commit

Permalink
Bug 1632131 - Simple smoke test for FOG's string metric type. r=chutten
Browse files Browse the repository at this point in the history
A first Rust test, which should run without any interaction with
Firefox.
This will eventually break down when we get IPC in there.

Depends on D72128

Differential Revision: https://phabricator.services.mozilla.com/D72385
  • Loading branch information
badboy committed Apr 24, 2020
1 parent 5c1e82a commit 995835e
Show file tree
Hide file tree
Showing 6 changed files with 77 additions and 0 deletions.
2 changes: 2 additions & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 4 additions & 0 deletions toolkit/components/glean/api/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -8,3 +8,7 @@ publish = false
[dependencies]
glean-core = "25.1.0"
log = "0.4"
once_cell = "1.2.0"

[dev-dependencies]
tempfile = "3.1.0"
3 changes: 3 additions & 0 deletions toolkit/components/glean/api/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,9 @@
//! assert!(glean::is_upload_enabled())
//! ```
// Re-exporting for later use in generated code.
pub extern crate once_cell;

pub mod metrics;

/// Run a closure with a mutable reference to the locked global Glean object.
Expand Down
40 changes: 40 additions & 0 deletions toolkit/components/glean/api/tests/common/mod.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
// 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 glean::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, ()> {
static GLOBAL_LOCK: Lazy<Mutex<()>> = Lazy::new(|| Mutex::new(()));

GLOBAL_LOCK.lock().unwrap()
}

// 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.
pub fn setup_glean(tempdir: Option<tempfile::TempDir>) -> tempfile::TempDir {
let dir = match tempdir {
Some(tempdir) => tempdir,
None => tempfile::tempdir().unwrap(),
};
let tmpname = dir.path().display().to_string();

let cfg = glean_core::Configuration {
data_path: tmpname,
application_id: GLOBAL_APPLICATION_ID.into(),
upload_enabled: true,
max_events: None,
delay_ping_lifetime_io: false,
};
let glean = glean_core::Glean::new(cfg).unwrap();
glean_core::setup_glean(glean).expect("can't set up global Glean object");

dir
}
27 changes: 27 additions & 0 deletions toolkit/components/glean/api/tests/string.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
mod common;
use common::*;

use glean::metrics::{CommonMetricData, Lifetime, StringMetric};

#[test]
fn sets_string_value() {
let _lock = lock_test();
let _t = setup_glean(None);
let store_names: Vec<String> = vec!["store1".into()];

let metric = StringMetric::new(CommonMetricData {
name: "string_metric".into(),
category: "telemetry".into(),
send_in_pings: store_names.clone(),
disabled: false,
lifetime: Lifetime::Ping,
..Default::default()
});

metric.set("test_string_value");

assert_eq!(
"test_string_value",
metric.test_get_value("store1").unwrap()
);
}
1 change: 1 addition & 0 deletions toolkit/library/rust/moz.build
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ RustLibrary('gkrust', gkrust_features, is_gkrust=True)
# Target directory doesn't matter a lot here, since we can't share panic=abort
# compilation artifacts with gkrust.
RUST_TESTS = [
'glean',
'selectors',
'servo_arc',
'stylo_tests',
Expand Down

0 comments on commit 995835e

Please sign in to comment.