Skip to content

Commit e334a71

Browse files
committed
test: [torrust#1403] add more tests to metrics pkg
1 parent 780e5d9 commit e334a71

13 files changed

Lines changed: 547 additions & 73 deletions

File tree

packages/metrics/.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
./.coverage

packages/metrics/README.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,10 @@ A library with the metrics types used by the [Torrust Tracker](https://github.co
66

77
[Crate documentation](https://docs.rs/torrust-tracker-metrics).
88

9+
## Acknowledgements
10+
11+
We copied some parts like units or function names and signatures from the crate [metrics](https://crates.io/crates/metrics) because we wanted to make it compatible as much as possible with it. In the future, we may consider using the `metrics` crate directly instead of maintaining our own version.
12+
913
## License
1014

1115
The project is licensed under the terms of the [GNU AFFERO GENERAL PUBLIC LICENSE](./LICENSE).

packages/metrics/src/counter.rs

Lines changed: 31 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,9 @@
1+
use derive_more::Display;
12
use serde::{Deserialize, Serialize};
23

34
use super::prometheus::PrometheusSerializable;
45

5-
#[derive(Debug, Clone, Default, PartialEq, Serialize, Deserialize)]
6+
#[derive(Debug, Display, Clone, Default, PartialEq, Serialize, Deserialize)]
67
pub struct Counter(u64);
78

89
impl Counter {
@@ -44,14 +45,37 @@ mod tests {
4445
use super::*;
4546

4647
#[test]
47-
fn test_counter() {
48-
let mut counter = Counter::new(0);
48+
fn it_should_be_created_from_integer_values() {
49+
let counter = Counter::new(0);
4950
assert_eq!(counter.value(), 0);
51+
}
5052

51-
counter.increment(5);
52-
assert_eq!(counter.value(), 5);
53+
#[test]
54+
fn it_could_be_converted_from_u64() {
55+
let counter: Counter = 42.into();
56+
assert_eq!(counter.value(), 42);
57+
}
58+
59+
#[test]
60+
fn it_could_be_converted_into_u64() {
61+
let counter = Counter::new(42);
62+
let value: u64 = counter.into();
63+
assert_eq!(value, 42);
64+
}
5365

54-
counter.increment(3);
55-
assert_eq!(counter.value(), 8);
66+
#[test]
67+
fn it_could_be_incremented() {
68+
let mut counter = Counter::new(0);
69+
counter.increment(1);
70+
assert_eq!(counter.value(), 1);
71+
72+
counter.increment(2);
73+
assert_eq!(counter.value(), 3);
74+
}
75+
76+
#[test]
77+
fn it_serializes_to_prometheus() {
78+
let counter = Counter::new(42);
79+
assert_eq!(counter.to_prometheus(), "42");
5680
}
5781
}

packages/metrics/src/gauge.rs

Lines changed: 31 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,9 @@
1+
use derive_more::Display;
12
use serde::{Deserialize, Serialize};
23

34
use super::prometheus::PrometheusSerializable;
45

5-
#[derive(Debug, Clone, Default, PartialEq, Serialize, Deserialize)]
6+
#[derive(Debug, Display, Clone, Default, PartialEq, Serialize, Deserialize)]
67
pub struct Gauge(f64);
78

89
impl Gauge {
@@ -46,14 +47,37 @@ mod tests {
4647
use super::*;
4748

4849
#[test]
49-
fn test_gauge() {
50-
let mut gauge = Gauge::new(0.0);
50+
fn it_should_be_created_from_integer_values() {
51+
let gauge = Gauge::new(0.0);
5152
assert_relative_eq!(gauge.value(), 0.0);
53+
}
5254

53-
gauge.set(5.5);
54-
assert_relative_eq!(gauge.value(), 5.5);
55+
#[test]
56+
fn it_could_be_converted_from_u64() {
57+
let gauge: Gauge = 42.0.into();
58+
assert_relative_eq!(gauge.value(), 42.0);
59+
}
60+
61+
#[test]
62+
fn it_could_be_converted_into_i64() {
63+
let gauge = Gauge::new(42.0);
64+
let value: f64 = gauge.into();
65+
assert_relative_eq!(value, 42.0);
66+
}
67+
68+
#[test]
69+
fn it_could_be_set() {
70+
let mut gauge = Gauge::new(0.0);
71+
gauge.set(1.0);
72+
assert_relative_eq!(gauge.value(), 1.0);
73+
}
74+
75+
#[test]
76+
fn it_serializes_to_prometheus() {
77+
let counter = Gauge::new(42.0);
78+
assert_eq!(counter.to_prometheus(), "42");
5579

56-
gauge.set(3.3);
57-
assert_relative_eq!(gauge.value(), 3.3);
80+
let counter = Gauge::new(42.1);
81+
assert_eq!(counter.to_prometheus(), "42.1");
5882
}
5983
}

packages/metrics/src/label/pair.rs

Lines changed: 18 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -3,15 +3,27 @@ use crate::prometheus::PrometheusSerializable;
33

44
pub type LabelPair = (LabelName, LabelValue);
55

6-
impl PrometheusSerializable for LabelPair {
6+
// Generic implementation for any tuple (A, B) where A and B implement PrometheusSerializable
7+
impl<A: PrometheusSerializable, B: PrometheusSerializable> PrometheusSerializable for (A, B) {
78
fn to_prometheus(&self) -> String {
8-
let (name, value) = self;
9-
format!("{}=\"{}\"", name.to_prometheus(), value.to_prometheus())
9+
format!("{}=\"{}\"", self.0.to_prometheus(), self.1.to_prometheus())
1010
}
1111
}
1212

13-
impl PrometheusSerializable for (&LabelName, &LabelValue) {
14-
fn to_prometheus(&self) -> String {
15-
format!("{}=\"{}\"", self.0.to_prometheus(), self.1.to_prometheus())
13+
#[cfg(test)]
14+
mod tests {
15+
mod serialization_of_label_pair_to_prometheus {
16+
use super::super::LabelName;
17+
use crate::label::LabelValue;
18+
use crate::prometheus::PrometheusSerializable;
19+
20+
#[test]
21+
fn test_label_pair_serialization_to_prometheus() {
22+
let label_pair = (LabelName::new("label_name"), LabelValue::new("value"));
23+
assert_eq!(label_pair.to_prometheus(), r#"label_name="value""#);
24+
25+
let label_pair = (&LabelName::new("label_name"), &LabelValue::new("value"));
26+
assert_eq!(label_pair.to_prometheus(), r#"label_name="value""#);
27+
}
1628
}
1729
}

packages/metrics/src/label/set.rs

Lines changed: 24 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -12,21 +12,18 @@ pub struct LabelSet {
1212
}
1313

1414
impl LabelSet {
15+
/// Insert a new label pair or update the value of an existing label.
1516
pub fn upsert(&mut self, key: LabelName, value: LabelValue) {
1617
self.items.insert(key, value);
1718
}
18-
19-
pub fn iter(&self) -> impl Iterator<Item = (&LabelName, &LabelValue)> {
20-
self.items.iter()
21-
}
2219
}
2320

2421
impl Display for LabelSet {
2522
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
2623
let items = self
2724
.items
2825
.iter()
29-
.map(|(key, value)| format!("{key}={value}"))
26+
.map(|(key, value)| format!("{key}=\"{value}\""))
3027
.collect::<Vec<_>>()
3128
.join(",");
3229

@@ -40,6 +37,18 @@ impl From<BTreeMap<LabelName, LabelValue>> for LabelSet {
4037
}
4138
}
4239

40+
impl From<Vec<(&str, &str)>> for LabelSet {
41+
fn from(vec: Vec<(&str, &str)>) -> Self {
42+
let mut items = BTreeMap::new();
43+
44+
for (name, value) in vec {
45+
items.insert(LabelName::new(name), LabelValue::new(value));
46+
}
47+
48+
Self { items }
49+
}
50+
}
51+
4352
impl From<Vec<LabelPair>> for LabelSet {
4453
fn from(vec: Vec<LabelPair>) -> Self {
4554
let mut items = BTreeMap::new();
@@ -280,13 +289,20 @@ mod tests {
280289
#[test]
281290
fn it_should_alphabetically_order_labels_in_prometheus_format() {
282291
let label_set = LabelSet::from([
283-
(LabelName::new("label_name_b"), LabelValue::new("label value b")),
284-
(LabelName::new("label_name_a"), LabelValue::new("label value a")),
292+
(LabelName::new("b_label_name"), LabelValue::new("b label value")),
293+
(LabelName::new("a_label_name"), LabelValue::new("a label value")),
285294
]);
286295

287296
assert_eq!(
288297
label_set.to_prometheus(),
289-
r#"{label_name_a="label value a",label_name_b="label value b"}"#
298+
r#"{a_label_name="a label value",b_label_name="b label value"}"#
290299
);
291300
}
301+
302+
#[test]
303+
fn it_should_allow_displaying() {
304+
let label_set = LabelSet::from((LabelName::new("label_name"), LabelValue::new("label value")));
305+
306+
assert_eq!(label_set.to_string(), r#"{label_name="label value"}"#);
307+
}
292308
}

packages/metrics/src/label/value.rs

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,3 +18,15 @@ impl PrometheusSerializable for LabelValue {
1818
self.0.clone()
1919
}
2020
}
21+
22+
#[cfg(test)]
23+
mod tests {
24+
use crate::label::value::LabelValue;
25+
use crate::prometheus::PrometheusSerializable;
26+
27+
#[test]
28+
fn it_serializes_to_prometheus() {
29+
let label_value = LabelValue::new("value");
30+
assert_eq!(label_value.to_prometheus(), "value");
31+
}
32+
}

packages/metrics/src/metric/description.rs

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,3 +10,20 @@ impl MetricDescription {
1010
Self(name.to_owned())
1111
}
1212
}
13+
14+
#[cfg(test)]
15+
mod tests {
16+
use super::*;
17+
18+
#[test]
19+
fn it_should_be_created_from_a_string_reference() {
20+
let metric = MetricDescription::new("Metric description");
21+
assert_eq!(metric.0, "Metric description");
22+
}
23+
24+
#[test]
25+
fn it_should_be_displayed() {
26+
let metric = MetricDescription::new("Metric description");
27+
assert_eq!(metric.to_string(), "Metric description");
28+
}
29+
}

0 commit comments

Comments
 (0)