Add some tests to verify behavior

This commit is contained in:
Jan-Erik Rediger 2019-04-24 17:22:03 +02:00
Родитель a2bd27cfc1
Коммит 26ccd4552a
1 изменённых файлов: 107 добавлений и 0 удалений

107
glean-core/tests/simple.rs Normal file
Просмотреть файл

@ -0,0 +1,107 @@
use glean_core::metrics::{BooleanMetric, StringMetric, CounterMetric};
use glean_core::{storage, CommonMetricData, Lifetime, Glean};
use lazy_static::lazy_static;
lazy_static! {
pub static ref GLOBAL_METRIC: BooleanMetric = BooleanMetric::new(CommonMetricData {
name: "global_metric".into(),
category: "global".into(),
send_in_pings: vec!["core".into(), "metrics".into()],
lifetime: Lifetime::Ping,
disabled: false,
});
}
#[test]
fn it_works() {
Glean::singleton().initialize();
assert!(Glean::singleton().is_initialized());
}
#[test]
fn can_set_metrics() {
Glean::singleton().initialize();
let local_metric: StringMetric = StringMetric::new(CommonMetricData {
name: "local_metric".into(),
category: "local".into(),
send_in_pings: vec!["core".into()],
.. Default::default()
});
let call_counter: CounterMetric = CounterMetric::new(CommonMetricData {
name: "calls".into(),
category: "local".into(),
send_in_pings: vec!["core".into(), "metrics".into()],
.. Default::default()
});
GLOBAL_METRIC.set(true);
local_metric.set("I can set this");
call_counter.add(1);
}
#[test]
fn can_snapshot() {
Glean::singleton().initialize();
let local_metric: StringMetric = StringMetric::new(CommonMetricData {
name: "can_snapshot_local_metric".into(),
category: "local".into(),
send_in_pings: vec!["core".into()],
.. Default::default()
});
local_metric.set("snapshot 42");
let snapshot = storage::StorageManager.snapshot("core", false);
assert!(snapshot.contains(r#""local.can_snapshot_local_metric": "snapshot 42""#));
}
#[test]
fn snapshot_can_clear_ping_store() {
Glean::singleton().initialize();
let local_metric: StringMetric = StringMetric::new(CommonMetricData {
name: "clear_snapshot_local_metric".into(),
category: "local".into(),
send_in_pings: vec!["core".into()],
.. Default::default()
});
local_metric.set("snapshot 43");
let snapshot = storage::StorageManager.snapshot("core", true);
assert!(snapshot.contains(r#""local.clear_snapshot_local_metric": "snapshot 43""#));
let snapshot = storage::StorageManager.snapshot("core", true);
assert!(!snapshot.contains(r#""local.clear_snapshot_local_metric": "snapshot 43""#));
}
#[test]
fn clear_is_store_specific() {
Glean::singleton().initialize();
let local_metric: StringMetric = StringMetric::new(CommonMetricData {
name: "store_specific".into(),
category: "local".into(),
send_in_pings: vec!["core".into(), "baseline".into()],
.. Default::default()
});
local_metric.set("snapshot 44");
// Snapshot 1: Clear core.
let core_snapshot = storage::StorageManager.snapshot("core", true);
let baseline_snapshot = storage::StorageManager.snapshot("baseline", false);
assert!(core_snapshot.contains(r#""local.store_specific": "snapshot 44""#));
assert!(baseline_snapshot.contains(r#""local.store_specific": "snapshot 44""#));
// Snapshot 2: Only baseline should contain the data.
let core_snapshot = storage::StorageManager.snapshot("core", true);
let baseline_snapshot = storage::StorageManager.snapshot("baseline", false);
assert!(!core_snapshot.contains(r#""local.store_specific": "snapshot 44""#));
assert!(baseline_snapshot.contains(r#""local.store_specific": "snapshot 44""#));
}