Fix all tests to include configuration object

This commit is contained in:
Jan-Erik Rediger 2019-07-26 15:09:46 +02:00
Родитель e53d35b776
Коммит aac2c71061
17 изменённых файлов: 152 добавлений и 64 удалений

Просмотреть файл

@ -18,7 +18,13 @@ fn main() {
root.path().display().to_string()
};
let mut glean = Glean::new(&data_path, "org.mozilla.glean_core.example", true).unwrap();
let cfg = glean_core::Configuration {
data_path,
application_id: "org.mozilla.glean_core.example".into(),
upload_enabled: true,
max_events: None,
};
let mut glean = Glean::new(cfg).unwrap();
glean.register_ping_type(&PingType::new("baseline", true));
glean.register_ping_type(&PingType::new("metrics", true));

Просмотреть файл

@ -132,7 +132,7 @@ mod test {
let dir = tempfile::tempdir().unwrap();
let tmpname = dir.path().display().to_string();
let glean = Glean::new(&tmpname, GLOBAL_APPLICATION_ID, true).unwrap();
let glean = Glean::with_options(&tmpname, GLOBAL_APPLICATION_ID, true).unwrap();
(glean, dir)
}

Просмотреть файл

@ -54,7 +54,7 @@ lazy_static! {
/// The Glean configuration.
///
/// Optional values will be filled in with default values.
#[derive(Debug)]
#[derive(Debug, Clone)]
pub struct Configuration {
/// Whether upload should be enabled.
pub upload_enabled: bool,
@ -74,8 +74,14 @@ pub struct Configuration {
/// ping.
///
/// ```rust,no_run
/// # use glean_core::{Glean, CommonMetricData, metrics::*};
/// let mut glean = Glean::new("/tmp/glean", "glean.sample.app", true).unwrap();
/// # use glean_core::{Glean, Configuration, CommonMetricData, metrics::*};
/// let cfg = Configuration {
/// data_path: "/tmp/glean".into(),
/// application_id: "glean.sample.app".into(),
/// upload_enabled: true,
/// max_events: None,
/// };
/// let mut glean = Glean::new(cfg).unwrap();
/// let ping = PingType::new("baseline", true);
/// glean.register_ping_type(&ping);
///
@ -138,6 +144,23 @@ impl Glean {
Ok(glean)
}
/// For tests make it easy to create a Glean object using only the required configuration.
#[cfg(test)]
pub(crate) fn with_options(
data_path: &str,
application_id: &str,
upload_enabled: bool,
) -> Result<Self> {
let cfg = Configuration {
data_path: data_path.into(),
application_id: application_id.into(),
upload_enabled,
max_events: None,
};
Self::new(cfg)
}
/// Initialize the core metrics managed by Glean's Rust core.
fn initialize_core_metrics(&mut self) {
let need_new_client_id = match self
@ -482,7 +505,7 @@ mod test {
pub fn new_glean() -> (Glean, tempfile::TempDir) {
let dir = tempfile::tempdir().unwrap();
let tmpname = dir.path().display().to_string();
let glean = Glean::new(&tmpname, GLOBAL_APPLICATION_ID, true).unwrap();
let glean = Glean::with_options(&tmpname, GLOBAL_APPLICATION_ID, true).unwrap();
(glean, dir)
}
@ -502,7 +525,7 @@ mod test {
fn experiment_id_and_branch_get_truncated_if_too_long() {
let t = tempfile::tempdir().unwrap();
let name = t.path().display().to_string();
let glean = Glean::new(&name, "org.mozilla.glean.tests", true).unwrap();
let glean = Glean::with_options(&name, "org.mozilla.glean.tests", true).unwrap();
// Generate long strings for the used ids.
let very_long_id = "test-experiment-id".repeat(5);
@ -538,7 +561,7 @@ mod test {
fn experiments_status_is_correctly_toggled() {
let t = tempfile::tempdir().unwrap();
let name = t.path().display().to_string();
let glean = Glean::new(&name, "org.mozilla.glean.tests", true).unwrap();
let glean = Glean::with_options(&name, "org.mozilla.glean.tests", true).unwrap();
// Define the experiment's data.
let experiment_id: String = "test-toggle-experiment".into();
@ -585,7 +608,7 @@ mod test {
let dir = tempfile::tempdir().unwrap();
let tmpname = dir.path().display().to_string();
{
let glean = Glean::new(&tmpname, GLOBAL_APPLICATION_ID, true).unwrap();
let glean = Glean::with_options(&tmpname, GLOBAL_APPLICATION_ID, true).unwrap();
glean.data_store.clear_all();
@ -602,7 +625,7 @@ mod test {
}
{
let glean = Glean::new(&tmpname, GLOBAL_APPLICATION_ID, true).unwrap();
let glean = Glean::with_options(&tmpname, GLOBAL_APPLICATION_ID, true).unwrap();
assert!(glean
.core_metrics
.client_id
@ -704,7 +727,7 @@ mod test {
fn client_id_is_set_to_known_value_when_uploading_disabled_at_start() {
let dir = tempfile::tempdir().unwrap();
let tmpname = dir.path().display().to_string();
let glean = Glean::new(&tmpname, GLOBAL_APPLICATION_ID, false).unwrap();
let glean = Glean::with_options(&tmpname, GLOBAL_APPLICATION_ID, false).unwrap();
assert_eq!(
*KNOWN_CLIENT_ID,
@ -720,7 +743,7 @@ mod test {
fn client_id_is_set_to_random_value_when_uploading_enabled_at_start() {
let dir = tempfile::tempdir().unwrap();
let tmpname = dir.path().display().to_string();
let glean = Glean::new(&tmpname, GLOBAL_APPLICATION_ID, true).unwrap();
let glean = Glean::with_options(&tmpname, GLOBAL_APPLICATION_ID, true).unwrap();
let current_client_id = glean
.core_metrics
@ -734,7 +757,7 @@ mod test {
fn enabling_when_already_enabled_is_a_noop() {
let dir = tempfile::tempdir().unwrap();
let tmpname = dir.path().display().to_string();
let mut glean = Glean::new(&tmpname, GLOBAL_APPLICATION_ID, true).unwrap();
let mut glean = Glean::with_options(&tmpname, GLOBAL_APPLICATION_ID, true).unwrap();
assert!(!glean.set_upload_enabled(true));
}
@ -743,7 +766,7 @@ mod test {
fn disabling_when_already_disabled_is_a_noop() {
let dir = tempfile::tempdir().unwrap();
let tmpname = dir.path().display().to_string();
let mut glean = Glean::new(&tmpname, GLOBAL_APPLICATION_ID, false).unwrap();
let mut glean = Glean::with_options(&tmpname, GLOBAL_APPLICATION_ID, false).unwrap();
assert!(!glean.set_upload_enabled(false));
}

Просмотреть файл

@ -272,7 +272,7 @@ mod test {
pub fn new_glean() -> (Glean, tempfile::TempDir) {
let dir = tempfile::tempdir().unwrap();
let tmpname = dir.path().display().to_string();
let glean = Glean::new(&tmpname, GLOBAL_APPLICATION_ID, true).unwrap();
let glean = Glean::with_options(&tmpname, GLOBAL_APPLICATION_ID, true).unwrap();
(glean, dir)
}

Просмотреть файл

@ -205,7 +205,7 @@ mod test {
fn test_experiments_json_serialization() {
let t = tempfile::tempdir().unwrap();
let name = t.path().display().to_string();
let glean = Glean::new(&name, "org.mozilla.glean", true).unwrap();
let glean = Glean::with_options(&name, "org.mozilla.glean", true).unwrap();
let extra: HashMap<String, String> = [("test-key".into(), "test-value".into())]
.iter()

Просмотреть файл

@ -17,8 +17,15 @@ use glean_core::{CommonMetricData, Glean, Lifetime};
#[test]
fn boolean_serializer_should_correctly_serialize_boolean() {
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(&tmpname, GLOBAL_APPLICATION_ID, true).unwrap();
let glean = Glean::new(cfg.clone()).unwrap();
let metric = BooleanMetric::new(CommonMetricData {
name: "boolean_metric".into(),
@ -42,7 +49,7 @@ fn boolean_serializer_should_correctly_serialize_boolean() {
// Make a new Glean instance here, which should force reloading of the data from disk
// so we can ensure it persisted, because it has User lifetime
{
let glean = Glean::new(&tmpname, GLOBAL_APPLICATION_ID, true).unwrap();
let glean = Glean::new(cfg.clone()).unwrap();
let snapshot = StorageManager
.snapshot_as_json(glean.storage(), "store1", true)
.unwrap();

Просмотреть файл

@ -49,7 +49,13 @@ pub fn new_glean() -> (Glean, tempfile::TempDir) {
let dir = tempfile::tempdir().unwrap();
let tmpname = dir.path().display().to_string();
let glean = Glean::new(&tmpname, GLOBAL_APPLICATION_ID, true).unwrap();
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).unwrap();
(glean, dir)
}

Просмотреть файл

@ -20,8 +20,15 @@ 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(&tmpname, GLOBAL_APPLICATION_ID, true).unwrap();
let glean = Glean::new(cfg.clone()).unwrap();
let metric = CounterMetric::new(CommonMetricData {
name: "counter_metric".into(),
@ -45,7 +52,7 @@ fn counter_serializer_should_correctly_serialize_counters() {
// Make a new Glean instance here, which should force reloading of the data from disk
// so we can ensure it persisted, because it has User lifetime
{
let glean = Glean::new(&tmpname, GLOBAL_APPLICATION_ID, true).unwrap();
let glean = Glean::new(cfg).unwrap();
let snapshot = StorageManager
.snapshot_as_json(glean.storage(), "store1", true)
.unwrap();

Просмотреть файл

@ -19,8 +19,15 @@ use glean_core::{CommonMetricData, Glean, Lifetime};
fn datetime_serializer_should_correctly_serialize_datetime() {
let expected_value = "1983-04-13T12:09+00:00";
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(&tmpname, GLOBAL_APPLICATION_ID, true).unwrap();
let glean = Glean::new(cfg.clone()).unwrap();
let metric = DatetimeMetric::new(
CommonMetricData {
@ -51,7 +58,7 @@ fn datetime_serializer_should_correctly_serialize_datetime() {
// Make a new Glean instance here, which should force reloading of the data from disk
// so we can ensure it persisted, because it has User lifetime
{
let glean = Glean::new(&tmpname, GLOBAL_APPLICATION_ID, true).unwrap();
let glean = Glean::new(cfg.clone()).unwrap();
let snapshot = StorageManager
.snapshot_as_json(glean.storage(), "store1", true)
.unwrap();

Просмотреть файл

@ -9,14 +9,13 @@ use std::collections::HashMap;
use std::fs;
use glean_core::metrics::*;
use glean_core::{CommonMetricData, Glean, Lifetime};
use glean_core::{CommonMetricData, Lifetime};
#[test]
fn record_properly_records_without_optional_arguments() {
let (_t, tmpname) = tempdir();
let store_names = vec!["store1".into(), "store2".into()];
let glean = Glean::new(&tmpname, GLOBAL_APPLICATION_ID, true).unwrap();
let (glean, _t) = new_glean();
let metric = EventMetric::new(
CommonMetricData {
@ -42,12 +41,10 @@ fn record_properly_records_without_optional_arguments() {
#[test]
fn record_properly_records_with_optional_arguments() {
let (_t, tmpname) = tempdir();
let (glean, _t) = new_glean();
let store_names = vec!["store1".into(), "store2".into()];
let glean = Glean::new(&tmpname, GLOBAL_APPLICATION_ID, true).unwrap();
let metric = EventMetric::new(
CommonMetricData {
name: "test_event_no_optional".into(),
@ -84,9 +81,7 @@ fn record_properly_records_with_optional_arguments() {
#[test]
fn snapshot_returns_none_if_nothing_is_recorded_in_the_store() {
let (_t, tmpname) = tempdir();
let glean = Glean::new(&tmpname, GLOBAL_APPLICATION_ID, true).unwrap();
let (glean, _t) = new_glean();
assert!(glean
.event_storage()
@ -96,12 +91,10 @@ fn snapshot_returns_none_if_nothing_is_recorded_in_the_store() {
#[test]
fn snapshot_correctly_clears_the_stores() {
let (_t, tmpname) = tempdir();
let (glean, _t) = new_glean();
let store_names = vec!["store1".into(), "store2".into()];
let glean = Glean::new(&tmpname, GLOBAL_APPLICATION_ID, true).unwrap();
let metric = EventMetric::new(
CommonMetricData {
name: "test_event_clear".into(),
@ -151,12 +144,10 @@ fn snapshot_correctly_clears_the_stores() {
#[test]
fn test_sending_of_event_ping_when_it_fills_up() {
let (_t, tmpname) = tempdir();
let (mut glean, _t) = new_glean();
let store_names: Vec<String> = vec!["events".into()];
let mut glean = Glean::new(&tmpname, GLOBAL_APPLICATION_ID, true).unwrap();
for store_name in &store_names {
glean.register_ping_type(&PingType::new(store_name.clone(), true));
}
@ -204,12 +195,10 @@ fn test_sending_of_event_ping_when_it_fills_up() {
#[test]
fn extra_keys_must_be_recorded_and_truncated_if_needed() {
let (_t, tmpname) = tempdir();
let (glean, _t) = new_glean();
let store_names: Vec<String> = vec!["store1".into()];
let glean = Glean::new(&tmpname, GLOBAL_APPLICATION_ID, true).unwrap();
let test_event = EventMetric::new(
CommonMetricData {
name: "testEvent".into(),

Просмотреть файл

@ -319,7 +319,16 @@ fn dynamic_labels_regex_allowed() {
#[test]
fn seen_labels_get_reloaded_from_disk() {
let (glean, dir) = new_glean();
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 mut labeled = LabeledMetric::new(
CounterMetric::new(CommonMetricData {
name: "labeled_metric".into(),
@ -357,8 +366,7 @@ fn seen_labels_get_reloaded_from_disk() {
// Force a reload
{
let tmpname = dir.path().display().to_string();
let glean = Glean::new(&tmpname, GLOBAL_APPLICATION_ID, true).unwrap();
let glean = Glean::new(cfg.clone()).unwrap();
// Try to store another label
labeled.get(&glean, "new_label").add(&glean, 40);

Просмотреть файл

@ -13,7 +13,13 @@ use glean_core::{CommonMetricData, Glean, Lifetime};
fn set_up_basic_ping() -> (Glean, PingMaker, PingType, tempfile::TempDir) {
let (t, tmpname) = tempdir();
let mut glean = Glean::new(&tmpname, GLOBAL_APPLICATION_ID, true).unwrap();
let cfg = glean_core::Configuration {
data_path: tmpname,
application_id: GLOBAL_APPLICATION_ID.into(),
upload_enabled: true,
max_events: None,
};
let mut glean = Glean::new(cfg).unwrap();
let ping_maker = PingMaker::new();
let ping_type = PingType::new("store1", true);
glean.register_ping_type(&ping_type);

Просмотреть файл

@ -18,8 +18,15 @@ use glean_core::{CommonMetricData, Glean, Lifetime};
#[test]
fn string_serializer_should_correctly_serialize_strings() {
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(&tmpname, GLOBAL_APPLICATION_ID, true).unwrap();
let glean = Glean::new(cfg.clone()).unwrap();
let metric = StringMetric::new(CommonMetricData {
name: "string_metric".into(),
@ -43,7 +50,7 @@ fn string_serializer_should_correctly_serialize_strings() {
// Make a new Glean instance here, which should force reloading of the data from disk
// so we can ensure it persisted, because it has User lifetime
{
let glean = Glean::new(&tmpname, GLOBAL_APPLICATION_ID, true).unwrap();
let glean = Glean::new(cfg.clone()).unwrap();
let snapshot = StorageManager
.snapshot_as_json(glean.storage(), "store1", true)
.unwrap();

Просмотреть файл

@ -44,8 +44,16 @@ fn list_can_store_multiple_items() {
#[test]
fn stringlist_serializer_should_correctly_serialize_stringlists() {
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(&tmpname, GLOBAL_APPLICATION_ID, true).unwrap();
let glean = Glean::new(cfg.clone()).unwrap();
let metric = StringListMetric::new(CommonMetricData {
name: "string_list_metric".into(),
category: "telemetry.test".into(),
@ -57,7 +65,7 @@ fn stringlist_serializer_should_correctly_serialize_stringlists() {
}
{
let glean = Glean::new(&tmpname, GLOBAL_APPLICATION_ID, true).unwrap();
let glean = Glean::new(cfg.clone()).unwrap();
let snapshot = StorageManager
.snapshot_as_json(glean.storage(), "store1", true)

Просмотреть файл

@ -19,11 +19,17 @@ use glean_core::{CommonMetricData, Glean, Lifetime};
#[test]
fn serializer_should_correctly_serialize_timespans() {
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 duration = 60;
{
let glean = Glean::new(&tmpname, GLOBAL_APPLICATION_ID, true).unwrap();
let glean = Glean::new(cfg.clone()).unwrap();
let mut metric = TimespanMetric::new(
CommonMetricData {
@ -48,7 +54,7 @@ fn serializer_should_correctly_serialize_timespans() {
// Make a new Glean instance here, which should force reloading of the data from disk
// so we can ensure it persisted, because it has User lifetime
{
let glean = Glean::new(&tmpname, GLOBAL_APPLICATION_ID, true).unwrap();
let glean = Glean::new(cfg.clone()).unwrap();
let snapshot = StorageManager
.snapshot_as_json(glean.storage(), "store1", true)
.unwrap();

Просмотреть файл

@ -21,8 +21,15 @@ fn serializer_should_correctly_serialize_timing_distribution() {
let duration = 60;
let time_unit = TimeUnit::Nanosecond;
let cfg = glean_core::Configuration {
data_path: tmpname,
application_id: GLOBAL_APPLICATION_ID.into(),
upload_enabled: true,
max_events: None,
};
{
let glean = Glean::new(&tmpname, GLOBAL_APPLICATION_ID, true).unwrap();
let glean = Glean::new(cfg.clone()).unwrap();
let mut metric = TimingDistributionMetric::new(
CommonMetricData {
@ -48,7 +55,7 @@ fn serializer_should_correctly_serialize_timing_distribution() {
// Make a new Glean instance here, which should force reloading of the data from disk
// so we can ensure it persisted, because it has User lifetime
{
let glean = Glean::new(&tmpname, GLOBAL_APPLICATION_ID, true).unwrap();
let glean = Glean::new(cfg.clone()).unwrap();
let snapshot = StorageManager
.snapshot_as_json(glean.storage(), "store1", true)
.unwrap();
@ -99,13 +106,11 @@ fn set_value_properly_sets_the_value_in_all_stores() {
#[test]
fn timing_distributions_must_not_accumulate_negative_values() {
let (_t, tmpname) = tempdir();
let (glean, _t) = new_glean();
let duration = 60;
let time_unit = TimeUnit::Nanosecond;
let glean = Glean::new(&tmpname, GLOBAL_APPLICATION_ID, true).unwrap();
let mut metric = TimingDistributionMetric::new(
CommonMetricData {
name: "distribution".into(),
@ -138,9 +143,7 @@ fn timing_distributions_must_not_accumulate_negative_values() {
#[test]
fn the_accumulate_samples_api_correctly_stores_timing_values() {
let (_t, tmpname) = tempdir();
let glean = Glean::new(&tmpname, GLOBAL_APPLICATION_ID, true).unwrap();
let (glean, _t) = new_glean();
let mut metric = TimingDistributionMetric::new(
CommonMetricData {
@ -182,9 +185,7 @@ fn the_accumulate_samples_api_correctly_stores_timing_values() {
#[test]
fn the_accumulate_samples_api_correctly_handles_negative_values() {
let (_t, tmpname) = tempdir();
let glean = Glean::new(&tmpname, GLOBAL_APPLICATION_ID, true).unwrap();
let (glean, _t) = new_glean();
let mut metric = TimingDistributionMetric::new(
CommonMetricData {

Просмотреть файл

@ -42,8 +42,15 @@ fn uuid_serializer_should_correctly_serialize_uuids() {
let value = uuid::Uuid::new_v4();
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(&tmpname, GLOBAL_APPLICATION_ID, true).unwrap();
let glean = Glean::new(cfg.clone()).unwrap();
let metric = UuidMetric::new(CommonMetricData {
name: "uuid_metric".into(),
@ -67,7 +74,7 @@ fn uuid_serializer_should_correctly_serialize_uuids() {
// Make a new Glean instance here, which should force reloading of the data from disk
// so we can ensure it persisted, because it has User lifetime
{
let glean = Glean::new(&tmpname, GLOBAL_APPLICATION_ID, true).unwrap();
let glean = Glean::new(cfg.clone()).unwrap();
let snapshot = StorageManager
.snapshot_as_json(glean.storage(), "store1", true)
.unwrap();