mod common;
use crate::common::*;
use serde_json::json;
use glean_core::metrics::*;
use glean_core::storage::StorageManager;
use glean_core::{test_get_num_recorded_errors, ErrorType};
use glean_core::{CommonMetricData, Glean, Lifetime};
#[test]
fn counter_serializer_should_correctly_serialize_counters() {
let (_t, tmpname) = tempdir();
let cfg = glean_core::Configuration {
data_path: tmpname,
application_id: GLOBAL_APPLICATION_ID.into(),
upload_enabled: true,
max_events: None,
};
{
let glean = Glean::new(cfg.clone()).unwrap();
let metric = CounterMetric::new(CommonMetricData {
name: "counter_metric".into(),
category: "telemetry".into(),
send_in_pings: vec!["store1".into()],
disabled: false,
lifetime: Lifetime::User,
..Default::default()
});
metric.add(&glean, 1);
let snapshot = StorageManager
.snapshot_as_json(glean.storage(), "store1", true)
.unwrap();
assert_eq!(
json!({"counter": {"telemetry.counter_metric": 1}}),
snapshot
);
}
{
let glean = Glean::new(cfg).unwrap();
let snapshot = StorageManager
.snapshot_as_json(glean.storage(), "store1", true)
.unwrap();
assert_eq!(
json!({"counter": {"telemetry.counter_metric": 1}}),
snapshot
);
}
}
#[test]
fn set_value_properly_sets_the_value_in_all_stores() {
let (glean, _t) = new_glean();
let store_names: Vec<String> = vec!["store1".into(), "store2".into()];
let metric = CounterMetric::new(CommonMetricData {
name: "counter_metric".into(),
category: "telemetry".into(),
send_in_pings: store_names.clone(),
disabled: false,
lifetime: Lifetime::Ping,
..Default::default()
});
metric.add(&glean, 1);
for store_name in store_names {
let snapshot = StorageManager
.snapshot_as_json(glean.storage(), &store_name, true)
.unwrap();
assert_eq!(
json!({"counter": {"telemetry.counter_metric": 1}}),
snapshot
);
}
}
#[test]
fn counters_must_not_increment_when_passed_zero_or_negative() {
let (glean, _t) = new_glean();
let metric = CounterMetric::new(CommonMetricData {
name: "counter_metric".into(),
category: "telemetry".into(),
send_in_pings: vec!["store1".into()],
disabled: false,
lifetime: Lifetime::Application,
..Default::default()
});
metric.add(&glean, 0);
assert!(metric.test_get_value(&glean, "store1").is_none());
metric.add(&glean, -1);
assert!(metric.test_get_value(&glean, "store1").is_none());
metric.add(&glean, 1);
assert_eq!(1, metric.test_get_value(&glean, "store1").unwrap());
assert_eq!(
Ok(2),
test_get_num_recorded_errors(&glean, metric.meta(), ErrorType::InvalidValue, None)
);
}
#[test]
fn transformation_works() {
let (glean, _t) = new_glean();
let counter: CounterMetric = CounterMetric::new(CommonMetricData {
name: "transformation".into(),
category: "local".into(),
send_in_pings: vec!["store1".into(), "store2".into()],
..Default::default()
});
counter.add(&glean, 2);
assert_eq!(2, counter.test_get_value(&glean, "store1").unwrap());
assert_eq!(2, counter.test_get_value(&glean, "store2").unwrap());
let _ = StorageManager
.snapshot_as_json(glean.storage(), "store1", true)
.unwrap();
counter.add(&glean, 2);
assert_eq!(2, counter.test_get_value(&glean, "store1").unwrap());
assert_eq!(4, counter.test_get_value(&glean, "store2").unwrap());
}
#[test]
fn saturates_at_boundary() {
let (glean, _t) = new_glean();
let counter: CounterMetric = CounterMetric::new(CommonMetricData {
name: "transformation".into(),
category: "local".into(),
send_in_pings: vec!["store1".into()],
..Default::default()
});
counter.add(&glean, 2);
counter.add(&glean, i32::max_value());
assert_eq!(
i32::max_value(),
counter.test_get_value(&glean, "store1").unwrap()
);
}