Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions src/atomic64.rs
Original file line number Diff line number Diff line change
Expand Up @@ -241,8 +241,8 @@ impl AtomicU64 {

#[cfg(test)]
mod test {
use std::f64;
use std::f64::consts::PI;
use std::f64::{self, EPSILON};

use super::*;

Expand All @@ -251,7 +251,7 @@ mod test {
let table: Vec<f64> = vec![0.0, 1.0, PI, f64::MIN, f64::MAX];

for f in table {
assert!((f - AtomicF64::new(f).get()).abs() < EPSILON);
assert!((f - AtomicF64::new(f).get()).abs() < f64::EPSILON);
}
}

Expand Down
46 changes: 23 additions & 23 deletions src/counter.rs
Original file line number Diff line number Diff line change
Expand Up @@ -323,7 +323,7 @@ impl<P: Atomic> Clone for GenericLocalCounterVec<P> {
#[cfg(test)]
mod tests {
use std::collections::HashMap;
use std::f64::EPSILON;
use std::f64;

use super::*;
use crate::metrics::{Collector, Opts};
Expand All @@ -343,7 +343,7 @@ mod tests {
assert_eq!(mfs.len(), 1);

let mf = mfs.pop().unwrap();
let m = mf.get_metric().get(0).unwrap();
let m = mf.get_metric().first().unwrap();
assert_eq!(m.get_label().len(), 2);
assert_eq!(m.get_counter().get_value() as u64, 43);

Expand All @@ -363,12 +363,12 @@ mod tests {
assert_eq!(mfs.len(), 1);

let mf = mfs.pop().unwrap();
let m = mf.get_metric().get(0).unwrap();
let m = mf.get_metric().first().unwrap();
assert_eq!(m.get_label().len(), 0);
assert_eq!(m.get_counter().get_value() as u64, 12);

counter.reset();
assert_eq!(counter.get() as u64, 0);
assert_eq!(counter.get(), 0);
}

#[test]
Expand Down Expand Up @@ -414,9 +414,9 @@ mod tests {

local_counter.reset();
counter.reset();
assert_eq!(counter.get() as u64, 0);
assert_eq!(counter.get(), 0);
local_counter.flush();
assert_eq!(counter.get() as u64, 0);
assert_eq!(counter.get(), 0);
}

#[test]
Expand Down Expand Up @@ -502,48 +502,48 @@ mod tests {
assert!(local_vec_1.remove_label_values(&["v1", "v2"]).is_err());

local_vec_1.with_label_values(&["v1", "v2"]).inc_by(23.0);
assert!((local_vec_1.with_label_values(&["v1", "v2"]).get() - 23.0) <= EPSILON);
assert!((vec.with_label_values(&["v1", "v2"]).get() - 0.0) <= EPSILON);
assert!((local_vec_1.with_label_values(&["v1", "v2"]).get() - 23.0) <= f64::EPSILON);
assert!((vec.with_label_values(&["v1", "v2"]).get() - 0.0) <= f64::EPSILON);

local_vec_1.flush();
assert!((local_vec_1.with_label_values(&["v1", "v2"]).get() - 0.0) <= EPSILON);
assert!((vec.with_label_values(&["v1", "v2"]).get() - 23.0) <= EPSILON);
assert!((local_vec_1.with_label_values(&["v1", "v2"]).get() - 0.0) <= f64::EPSILON);
assert!((vec.with_label_values(&["v1", "v2"]).get() - 23.0) <= f64::EPSILON);

local_vec_1.flush();
assert!((local_vec_1.with_label_values(&["v1", "v2"]).get() - 0.0) <= EPSILON);
assert!((vec.with_label_values(&["v1", "v2"]).get() - 23.0) <= EPSILON);
assert!((local_vec_1.with_label_values(&["v1", "v2"]).get() - 0.0) <= f64::EPSILON);
assert!((vec.with_label_values(&["v1", "v2"]).get() - 23.0) <= f64::EPSILON);

local_vec_1.with_label_values(&["v1", "v2"]).inc_by(11.0);
assert!((local_vec_1.with_label_values(&["v1", "v2"]).get() - 11.0) <= EPSILON);
assert!((vec.with_label_values(&["v1", "v2"]).get() - 23.0) <= EPSILON);
assert!((local_vec_1.with_label_values(&["v1", "v2"]).get() - 11.0) <= f64::EPSILON);
assert!((vec.with_label_values(&["v1", "v2"]).get() - 23.0) <= f64::EPSILON);

local_vec_1.flush();
assert!((local_vec_1.with_label_values(&["v1", "v2"]).get() - 0.0) <= EPSILON);
assert!((vec.with_label_values(&["v1", "v2"]).get() - 34.0) <= EPSILON);
assert!((local_vec_1.with_label_values(&["v1", "v2"]).get() - 0.0) <= f64::EPSILON);
assert!((vec.with_label_values(&["v1", "v2"]).get() - 34.0) <= f64::EPSILON);

// When calling `remove_label_values`, it is "flushed" immediately.
assert!(local_vec_1.remove_label_values(&["v1", "v2"]).is_ok());
assert!((local_vec_1.with_label_values(&["v1", "v2"]).get() - 0.0) <= EPSILON);
assert!((vec.with_label_values(&["v1", "v2"]).get() - 0.0) <= EPSILON);
assert!((local_vec_1.with_label_values(&["v1", "v2"]).get() - 0.0) <= f64::EPSILON);
assert!((vec.with_label_values(&["v1", "v2"]).get() - 0.0) <= f64::EPSILON);

local_vec_1.with_label_values(&["v1", "v2"]).inc();
assert!(local_vec_1.remove_label_values(&["v1"]).is_err());
assert!(local_vec_1.remove_label_values(&["v1", "v3"]).is_err());

local_vec_1.with_label_values(&["v1", "v2"]).inc_by(13.0);
assert!((local_vec_1.with_label_values(&["v1", "v2"]).get() - 14.0) <= EPSILON);
assert!((vec.with_label_values(&["v1", "v2"]).get() - 0.0) <= EPSILON);
assert!((local_vec_1.with_label_values(&["v1", "v2"]).get() - 14.0) <= f64::EPSILON);
assert!((vec.with_label_values(&["v1", "v2"]).get() - 0.0) <= f64::EPSILON);

local_vec_2.with_label_values(&["v1", "v2"]).inc_by(7.0);
assert!((local_vec_2.with_label_values(&["v1", "v2"]).get() - 7.0) <= EPSILON);
assert!((local_vec_2.with_label_values(&["v1", "v2"]).get() - 7.0) <= f64::EPSILON);

local_vec_1.flush();
local_vec_2.flush();
assert!((vec.with_label_values(&["v1", "v2"]).get() - 21.0) <= EPSILON);
assert!((vec.with_label_values(&["v1", "v2"]).get() - 21.0) <= f64::EPSILON);

local_vec_1.flush();
local_vec_2.flush();
assert!((vec.with_label_values(&["v1", "v2"]).get() - 21.0) <= EPSILON);
assert!((vec.with_label_values(&["v1", "v2"]).get() - 21.0) <= f64::EPSILON);
}

#[test]
Expand Down
9 changes: 3 additions & 6 deletions src/desc.rs
Original file line number Diff line number Diff line change
Expand Up @@ -253,8 +253,7 @@ mod tests {
vec![name.into()],
HashMap::new(),
)
.err()
.expect(format!("expected error for {}", name).as_ref());
.expect_err(format!("expected error for {}", name).as_ref());
match res {
Error::Msg(msg) => assert_eq!(msg, format!("'{}' is not a valid label name", name)),
other => panic!("{}", other),
Expand All @@ -268,8 +267,7 @@ mod tests {
let mut labels = HashMap::new();
labels.insert(name.into(), "value".into());
let res = Desc::new("name".into(), "help".into(), vec![], labels)
.err()
.expect(format!("expected error for {}", name).as_ref());
.expect_err(format!("expected error for {}", name).as_ref());
match res {
Error::Msg(msg) => assert_eq!(msg, format!("'{}' is not a valid label name", name)),
other => panic!("{}", other),
Expand All @@ -281,8 +279,7 @@ mod tests {
fn test_invalid_metric_name() {
for &name in &["-dash", "9gag", "has space"] {
let res = Desc::new(name.into(), "help".into(), vec![], HashMap::new())
.err()
.expect(format!("expected error for {}", name).as_ref());
.expect_err(format!("expected error for {}", name).as_ref());
match res {
Error::Msg(msg) => {
assert_eq!(msg, format!("'{}' is not a valid metric name", name))
Expand Down
2 changes: 1 addition & 1 deletion src/gauge.rs
Original file line number Diff line number Diff line change
Expand Up @@ -188,7 +188,7 @@ mod tests {
assert_eq!(mfs.len(), 1);

let mf = mfs.pop().unwrap();
let m = mf.get_metric().get(0).unwrap();
let m = mf.get_metric().first().unwrap();
assert_eq!(m.get_label().len(), 2);
assert_eq!(m.get_gauge().get_value() as u64, 42);
}
Expand Down
30 changes: 17 additions & 13 deletions src/histogram.rs
Original file line number Diff line number Diff line change
Expand Up @@ -284,11 +284,11 @@ impl ShardAndCount {
/// A histogram supports two main execution paths:
///
/// 1. `observe` which increases the overall observation counter, updates the
/// observation sum and increases a single bucket counter.
/// observation sum and increases a single bucket counter.
///
/// 2. `proto` (aka. collecting the metric, from now on referred to as the
/// collect operation) which snapshots the state of the histogram and exposes it
/// as a Protobuf struct.
/// collect operation) which snapshots the state of the histogram and exposes
/// it as a Protobuf struct.
///
/// If an observe and a collect operation interleave, the latter could be
/// exposing a snapshot of the histogram that does not uphold all histogram
Expand Down Expand Up @@ -1207,7 +1207,7 @@ impl Clone for LocalHistogramVec {

#[cfg(test)]
mod tests {
use std::f64::{EPSILON, INFINITY};
use std::f64;
use std::thread;
use std::time::Duration;

Expand Down Expand Up @@ -1237,7 +1237,7 @@ mod tests {
assert_eq!(mfs.len(), 1);

let mf = mfs.pop().unwrap();
let m = mf.get_metric().get(0).unwrap();
let m = mf.get_metric().first().unwrap();
assert_eq!(m.get_label().len(), 2);
let proto_histogram = m.get_histogram();
assert_eq!(proto_histogram.get_sample_count(), 3);
Expand All @@ -1251,11 +1251,11 @@ mod tests {
assert_eq!(mfs.len(), 1);

let mf = mfs.pop().unwrap();
let m = mf.get_metric().get(0).unwrap();
let m = mf.get_metric().first().unwrap();
assert_eq!(m.get_label().len(), 0);
let proto_histogram = m.get_histogram();
assert_eq!(proto_histogram.get_sample_count(), 0);
assert!((proto_histogram.get_sample_sum() - 0.0) < EPSILON);
assert!((proto_histogram.get_sample_sum() - 0.0) < f64::EPSILON);
assert_eq!(proto_histogram.get_bucket().len(), buckets.len())
}

Expand Down Expand Up @@ -1287,7 +1287,7 @@ mod tests {
let m = mf.get_metric().get(0).unwrap();
let proto_histogram = m.get_histogram();
assert_eq!(proto_histogram.get_sample_count(), 3);
assert!((proto_histogram.get_sample_sum() - 0.0) > EPSILON);
assert!((proto_histogram.get_sample_sum() - 0.0) > f64::EPSILON);
}

#[test]
Expand All @@ -1311,7 +1311,11 @@ mod tests {
(vec![], true, DEFAULT_BUCKETS.len()),
(vec![-2.0, -1.0, -0.5, 0.0, 0.5, 1.0, 2.0], true, 7),
(vec![-2.0, -1.0, -0.5, 10.0, 0.5, 1.0, 2.0], false, 7),
(vec![-2.0, -1.0, -0.5, 0.0, 0.5, 1.0, INFINITY], true, 6),
(
vec![-2.0, -1.0, -0.5, 0.0, 0.5, 1.0, f64::INFINITY],
true,
6,
),
];

for (buckets, is_ok, length) in table {
Expand Down Expand Up @@ -1366,7 +1370,7 @@ mod tests {
for (millis, seconds) in tbls {
let d = Duration::from_millis(millis);
let v = duration_to_seconds(d);
assert!((v - seconds).abs() < EPSILON);
assert!((v - seconds).abs() < f64::EPSILON);
}
}

Expand Down Expand Up @@ -1405,7 +1409,7 @@ mod tests {

let proto_histogram = m.get_histogram();
assert_eq!(proto_histogram.get_sample_count(), 1);
assert!((proto_histogram.get_sample_sum() - 1.0) < EPSILON);
assert!((proto_histogram.get_sample_sum() - 1.0) < f64::EPSILON);
assert_eq!(proto_histogram.get_bucket().len(), buckets.len())
}

Expand All @@ -1421,7 +1425,7 @@ mod tests {
let m = histogram.metric();
let proto_histogram = m.get_histogram();
assert_eq!(proto_histogram.get_sample_count(), count);
assert!((proto_histogram.get_sample_sum() - sum) < EPSILON);
assert!((proto_histogram.get_sample_sum() - sum) < f64::EPSILON);
};

local.observe(1.0);
Expand Down Expand Up @@ -1456,7 +1460,7 @@ mod tests {
let ms = vec.collect()[0].take_metric();
let proto_histogram = ms[0].get_histogram();
assert_eq!(proto_histogram.get_sample_count(), count);
assert!((proto_histogram.get_sample_sum() - sum) < EPSILON);
assert!((proto_histogram.get_sample_sum() - sum) < f64::EPSILON);
};

{
Expand Down
9 changes: 5 additions & 4 deletions src/macros.rs
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ fn test_labels_without_trailing_comma() {
"foo" => "bar"
};
assert_eq!(labels.len(), 2);
assert!(labels.get("test").is_some());
assert!(labels.contains_key("test"));
assert_eq!(*(labels.get("test").unwrap()), "hello");
}

Expand Down Expand Up @@ -84,6 +84,7 @@ macro_rules! opts {
let opts = $crate::Opts::new($NAME, $HELP);
let lbs = HashMap::<String, String>::new();
$(
#[allow(clippy::redundant_locals)]
let mut lbs = lbs;
lbs.extend($CONST_LABELS.iter().map(|(k, v)| ((*k).into(), (*v).into())));
)*
Expand All @@ -104,7 +105,7 @@ fn test_opts_trailing_comma() {

let opts = opts!(name, help, labels! {"test" => "hello", "foo" => "bar",},);
assert_eq!(opts.const_labels.len(), 2);
assert!(opts.const_labels.get("foo").is_some());
assert!(opts.const_labels.contains_key("foo"));
assert_eq!(opts.const_labels.get("foo").unwrap(), "bar");

let opts = opts!(
Expand All @@ -114,7 +115,7 @@ fn test_opts_trailing_comma() {
labels! {"ans" => "42",},
);
assert_eq!(opts.const_labels.len(), 3);
assert!(opts.const_labels.get("ans").is_some());
assert!(opts.const_labels.contains_key("ans"));
assert_eq!(opts.const_labels.get("ans").unwrap(), "42");
}

Expand Down Expand Up @@ -191,7 +192,7 @@ fn test_histogram_opts_trailing_comma() {
assert_eq!(opts.common_opts.name, name);
assert_eq!(opts.common_opts.help, help);
assert_eq!(opts.buckets.len(), 2);
assert!(opts.common_opts.const_labels.get("key").is_some());
assert!(opts.common_opts.const_labels.contains_key("key"));
assert_eq!(opts.common_opts.const_labels.get("key").unwrap(), "value");
}

Expand Down