Commit e44691d6 authored by Jan-Erik Rediger's avatar Jan-Erik Rediger
Browse files

Bug 1632131 - Simple smoke test for FOG's string metric type. r=chutten

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
parent 0542e950
Loading
Loading
Loading
Loading
+2 −0
Original line number Diff line number Diff line
@@ -1817,6 +1817,8 @@ version = "0.1.0"
dependencies = [
 "glean-core",
 "log",
 "once_cell",
 "tempfile",
]

[[package]]
+4 −0
Original line number Diff line number Diff line
@@ -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 −0
Original line number Diff line number Diff line
@@ -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.
+40 −0
Original line number Diff line number Diff line
// 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 −0
Original line number Diff line number Diff line
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()
    );
}
Loading