Skip to content

Commit

Permalink
fix: make test config public
Browse files Browse the repository at this point in the history
  • Loading branch information
alexgallotta committed Aug 21, 2024
1 parent 911f5de commit 7dc9799
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 29 deletions.
54 changes: 28 additions & 26 deletions dogstatsd/src/aggregator.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
//! The aggregation of metrics.

use std::sync::Mutex;
use crate::constants;
use crate::datadog::{self, Metric as MetricToShip, Series};
use crate::errors;
Expand Down Expand Up @@ -357,6 +358,33 @@ fn tags_string_to_vector(tags: Option<Ustr>) -> Vec<String> {
.collect()
}

#[cfg(test)]
pub fn assert_value(aggregator_mutex: &Mutex<Aggregator>, metric_id: &str, value: f64) {
let aggregator = aggregator_mutex.lock().unwrap();
if let Some(e) = aggregator.get_entry_by_id(metric_id.into(), None) {
let metric = e.metric_value.get_value().unwrap();
assert!((metric - value).abs() < PRECISION);
} else {
panic!("{}", format!("{metric_id} not found"));
}
}

#[cfg(test)]
pub fn assert_sketch(aggregator_mutex: &Mutex<Aggregator>, metric_id: &str, value: f64) {
let aggregator = aggregator_mutex.lock().unwrap();
if let Some(e) = aggregator.get_entry_by_id(metric_id.into(), None) {
let metric = e.metric_value.get_sketch().unwrap();
assert!((metric.max().unwrap() - value).abs() < PRECISION);
assert!((metric.min().unwrap() - value).abs() < PRECISION);
assert!((metric.sum().unwrap() - value).abs() < PRECISION);
assert!((metric.avg().unwrap() - value).abs() < PRECISION);
} else {
panic!("{}", format!("{metric_id} not found"));
}
}
#[cfg(test)]
const PRECISION: f64 = 0.000_000_01;

#[cfg(test)]
#[allow(clippy::unwrap_used)]
pub mod tests {
Expand All @@ -366,9 +394,6 @@ pub mod tests {
use datadog_protos::metrics::SketchPayload;
use hashbrown::hash_table;
use protobuf::Message;
use std::sync::Mutex;

const PRECISION: f64 = 0.000_000_01;

const SINGLE_METRIC_SIZE: usize = 187;
const SINGLE_DISTRIBUTION_SIZE: u64 = 135;
Expand All @@ -378,29 +403,6 @@ pub mod tests {
"_dd.compute_stats:1",
];

pub fn assert_value(aggregator_mutex: &Mutex<Aggregator>, metric_id: &str, value: f64) {
let aggregator = aggregator_mutex.lock().unwrap();
if let Some(e) = aggregator.get_entry_by_id(metric_id.into(), None) {
let metric = e.metric_value.get_value().unwrap();
assert!((metric - value).abs() < PRECISION);
} else {
panic!("{}", format!("{metric_id} not found"));
}
}

pub fn assert_sketch(aggregator_mutex: &Mutex<Aggregator>, metric_id: &str, value: f64) {
let aggregator = aggregator_mutex.lock().unwrap();
if let Some(e) = aggregator.get_entry_by_id(metric_id.into(), None) {
let metric = e.metric_value.get_sketch().unwrap();
assert!((metric.max().unwrap() - value).abs() < PRECISION);
assert!((metric.min().unwrap() - value).abs() < PRECISION);
assert!((metric.sum().unwrap() - value).abs() < PRECISION);
assert!((metric.avg().unwrap() - value).abs() < PRECISION);
} else {
panic!("{}", format!("{metric_id} not found"));
}
}

#[test]
fn insertion() {
let mut aggregator = Aggregator::new(Vec::new(), 2).unwrap();
Expand Down
4 changes: 1 addition & 3 deletions dogstatsd/src/dogstatsd.rs
Original file line number Diff line number Diff line change
Expand Up @@ -104,9 +104,7 @@ impl DogStatsD {
#[cfg(test)]
#[allow(clippy::unwrap_used)]
mod tests {
use crate::aggregator::tests::assert_sketch;
use crate::aggregator::tests::assert_value;
use crate::aggregator::Aggregator;
use crate::aggregator::{assert_sketch, assert_value, Aggregator};
use crate::dogstatsd::{BufferReader, DogStatsD};
use std::net::{IpAddr, Ipv4Addr, SocketAddr};
use std::sync::{Arc, Mutex};
Expand Down

0 comments on commit 7dc9799

Please sign in to comment.