Skip to content

Commit 6c88c9c

Browse files
committed
test: add test for consumer loop
1 parent db7fdf3 commit 6c88c9c

File tree

3 files changed

+136
-0
lines changed

3 files changed

+136
-0
lines changed

Cargo.lock

Lines changed: 70 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Cargo.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,7 @@ tokio = { version = "1.49.0", features = [
4343
] }
4444
toml = "1.0.1"
4545
rustls = { version = "0.23" }
46+
mockito = "1.7.2"
4647

4748
[build-dependencies]
4849
prost-build = "0.14.3"

src/metrics.rs

Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -273,3 +273,68 @@ pub async fn metrics_loop(config: &'static IngestorConfig) {
273273
handle.abort(); // abort each task
274274
}
275275
}
276+
277+
#[cfg(test)]
278+
mod tests {
279+
use std::collections::HashMap;
280+
281+
use tokio::spawn;
282+
283+
use crate::{
284+
config::{BasicAuth, MetricInterval, MetricsConfig, UrlPort},
285+
metrics::consumer_loop,
286+
metrics_core::prometheus::{Label, Sample, TimeSeries},
287+
};
288+
289+
fn test_config(url: String) -> MetricsConfig {
290+
MetricsConfig {
291+
user_config_path: None,
292+
auth: BasicAuth {
293+
username: "user".into(),
294+
password: "pass".into(),
295+
},
296+
url: url,
297+
intervals: HashMap::new(),
298+
dynamic: HashMap::new(),
299+
watchdog_interval: MetricInterval::Daily(1),
300+
publish_interval: MetricInterval::Millis(1),
301+
}
302+
}
303+
304+
#[tokio::test(flavor = "multi_thread")]
305+
async fn test_consumer_loop() {
306+
// Request a new server from the pool
307+
let mut server = mockito::Server::new_async().await;
308+
309+
// Use one of these addresses to configure your client
310+
let host = server.host_with_port();
311+
let url = server.url();
312+
let config = test_config(url);
313+
// Create a mock
314+
let mock = server
315+
.mock("POST", "/")
316+
.with_status(201)
317+
.with_header("Content-Type", "application/x-protobuf")
318+
.with_header("Content-Encoding", "snappy")
319+
.match_request(|req| req.body().unwrap().len() == 33)
320+
.create();
321+
322+
let (tx, rx) = tokio::sync::mpsc::unbounded_channel::<TimeSeries>();
323+
let handle = spawn(consumer_loop(Box::leak(Box::new(rx)), config));
324+
325+
let _ = tx.send(TimeSeries {
326+
labels: Vec::from([Label {
327+
name: "__name__".into(),
328+
value: "test".into(),
329+
}]),
330+
samples: Vec::from([Sample {
331+
value: 1.0,
332+
timestamp: 0,
333+
}]),
334+
});
335+
drop(tx);
336+
let _ = handle.await;
337+
338+
mock.assert();
339+
}
340+
}

0 commit comments

Comments
 (0)