Report an error if Timespans are used twice before are cleared

This allows catching multiple unintended start()/stop() call
cycles before a metric is cleared.
This commit is contained in:
Alessio Placitelli 2019-08-01 10:26:52 +02:00
Родитель 1d74efbbf9
Коммит 8f60e2ca07
2 изменённых файлов: 30 добавлений и 1 удалений

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

@ -120,16 +120,33 @@ impl TimespanMetric {
return;
}
let mut report_value_exists: bool = false;
glean.storage().record_with(&self.meta, |old_value| {
if overwrite {
Metric::Timespan(elapsed, self.time_unit)
} else {
match old_value {
Some(old @ Metric::Timespan(..)) => old,
Some(old @ Metric::Timespan(..)) => {
// If some value already exists, report an error.
// We do this out of the storage since recording an
// error accesses the storage as well.
report_value_exists = true;
old
}
_ => Metric::Timespan(elapsed, self.time_unit),
}
}
});
if report_value_exists {
record_error(
glean,
&self.meta,
ErrorType::InvalidValue,
"Timespan value already recorded. New value discarded.",
None,
);
};
}
/// **Test-only API (exported for FFI purposes).**

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

@ -114,6 +114,11 @@ fn second_timer_run_is_skipped() {
metric.set_start(&glean, 0);
metric.set_stop(&glean, duration);
// No error should be recorded here: we had no prior value stored.
assert!(
test_get_num_recorded_errors(&glean, metric.meta(), ErrorType::InvalidValue, None).is_err()
);
let first_value = metric.test_get_value(&glean, "store1").unwrap();
assert_eq!(duration, first_value);
@ -122,6 +127,13 @@ fn second_timer_run_is_skipped() {
let second_value = metric.test_get_value(&glean, "store1").unwrap();
assert_eq!(second_value, first_value);
// Make sure that the error has been recorded: we had a stored value, the
// new measurement was dropped.
assert_eq!(
Ok(1),
test_get_num_recorded_errors(&glean, metric.meta(), ErrorType::InvalidValue, None)
);
}
#[test]