Skip to content

Commit

Permalink
chore(ci): Make tests for tracing-limit more robust (vectordotdev#6632)
Browse files Browse the repository at this point in the history
Use a fake clock since the OSX Github Actions runner seems particularly
noisy.

I should have done this originally anyway for robustness and to not
artificially bloat the test time.

Signed-off-by: Jesse Szwedko <[email protected]>
  • Loading branch information
jszwedko authored Mar 4, 2021
1 parent a557735 commit 78a9dc1
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 6 deletions.
7 changes: 7 additions & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions lib/tracing-limit/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ dashmap = "4"
[dev-dependencies]
criterion = "0.3"
tracing = "0.1.15"
mock_instant = { version = "0.2" }

[[bench]]
name = "limit"
Expand Down
20 changes: 14 additions & 6 deletions lib/tracing-limit/src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
use dashmap::DashMap;
use std::{fmt, time::Instant};
use std::fmt;
use tracing_core::{
callsite::Identifier,
field::{display, Field, Value, Visit},
Expand All @@ -13,6 +13,12 @@ use tracing_subscriber::layer::{Context, Layer};
#[macro_use]
extern crate tracing;

#[cfg(test)]
use mock_instant::Instant;

#[cfg(not(test))]
use std::time::Instant;

const RATE_LIMIT_SECS_FIELD: &str = "internal_log_rate_secs";
const MESSAGE_FIELD: &str = "message";

Expand Down Expand Up @@ -389,7 +395,11 @@ impl Visit for LimitVisitor {
#[cfg(test)]
mod test {
use super::*;
use std::sync::{Arc, Mutex};
use mock_instant::MockClock;
use std::{
sync::{Arc, Mutex},
time::Duration,
};
use tracing_subscriber::layer::SubscriberExt;

#[derive(Default)]
Expand Down Expand Up @@ -440,7 +450,7 @@ mod test {
tracing::subscriber::with_default(sub, || {
for _ in 0..21 {
info!(message = "Hello world!", internal_log_rate_secs = 1);
std::thread::sleep(std::time::Duration::from_millis(100));
MockClock::advance(Duration::from_millis(100));
}
});

Expand Down Expand Up @@ -487,14 +497,12 @@ mod test {
);
}
}
std::thread::sleep(std::time::Duration::from_millis(100));
MockClock::advance(Duration::from_millis(100));
}
});

let events = events.lock().unwrap();

dbg!(&events);

assert_eq!(
*events,
vec![
Expand Down

0 comments on commit 78a9dc1

Please sign in to comment.