use std::sync::Arc;
use crate::observability::{
MetricLabels, MetricRecorder, MetricRegistry, MetricsError, MetricsSnapshot,
};
use super::{assert_metric_value, assert_metric_value_at_least, assert_no_metric};
#[derive(Debug, Clone, Default)]
pub struct TestMetrics {
registry: Arc<MetricRegistry>,
}
impl TestMetrics {
#[must_use]
pub fn new() -> Self {
Self::default()
}
#[must_use]
pub fn recorder(&self) -> Arc<dyn MetricRecorder> {
self.registry.clone()
}
#[must_use]
pub fn registry(&self) -> &Arc<MetricRegistry> {
&self.registry
}
pub fn snapshot(&self) -> Result<MetricsSnapshot, MetricsError> {
self.registry.snapshot()
}
pub fn assert_counter(&self, name: &str, labels: MetricLabels, expected: u64) {
let snapshot = self.snapshot().expect("metrics snapshot should build");
assert_metric_value(&snapshot, name, labels, expected as f64);
}
pub fn assert_counter_at_least(&self, name: &str, labels: MetricLabels, expected: u64) {
let snapshot = self.snapshot().expect("metrics snapshot should build");
assert_metric_value_at_least(&snapshot, name, labels, expected as f64);
}
pub fn assert_no_metric(&self, name: &str, labels: MetricLabels) {
let snapshot = self.snapshot().expect("metrics snapshot should build");
assert_no_metric(&snapshot, name, labels);
}
}