Skip to content

Commit

Permalink
Constify the test wait period
Browse files Browse the repository at this point in the history
  • Loading branch information
bnaecker committed Nov 29, 2023
1 parent 0188270 commit 5e76841
Showing 1 changed file with 11 additions and 13 deletions.
24 changes: 11 additions & 13 deletions oximeter/collector/src/agent.rs
Original file line number Diff line number Diff line change
Expand Up @@ -668,13 +668,14 @@ mod tests {
const TICK_INTERVAL: Duration = Duration::from_millis(10);

// Total number of collection attempts.
const N_COLLECTIONS: usize = 5;
const N_COLLECTIONS: u64 = 5;

// Period these tests wait using `tokio::time::advance()` before checking
// their test conditions.
fn test_wait_period() -> Duration {
COLLECTION_INTERVAL * N_COLLECTIONS as u32 + COLLECTION_INTERVAL / 2
}
const TEST_WAIT_PERIOD: Duration = Duration::from_millis(
COLLECTION_INTERVAL.as_millis() as u64 * N_COLLECTIONS
+ COLLECTION_INTERVAL.as_millis() as u64 / 2,
);

// Test that we count successful collections from a target correctly.
#[tokio::test]
Expand Down Expand Up @@ -724,8 +725,7 @@ mod tests {
// Step time until there has been exactly `N_COLLECTIONS` collections.
tokio::time::pause();
let now = Instant::now();
let wait_for = test_wait_period();
while now.elapsed() < wait_for {
while now.elapsed() < TEST_WAIT_PERIOD {
tokio::time::advance(TICK_INTERVAL).await;
}

Expand All @@ -744,7 +744,7 @@ mod tests {
.await
.expect("failed to request statistics from task");
let stats = rx.await.expect("failed to receive statistics from task");
assert_eq!(stats.collections.datum.value(), N_COLLECTIONS as u64);
assert_eq!(stats.collections.datum.value(), N_COLLECTIONS);
assert!(stats.failed_collections.is_empty());
logctx.cleanup_successful();
}
Expand Down Expand Up @@ -786,8 +786,7 @@ mod tests {
// Step time until there has been exactly `N_COLLECTIONS` collections.
tokio::time::pause();
let now = Instant::now();
let wait_for = test_wait_period();
while now.elapsed() < wait_for {
while now.elapsed() < TEST_WAIT_PERIOD {
tokio::time::advance(TICK_INTERVAL).await;
}

Expand All @@ -814,7 +813,7 @@ mod tests {
.unwrap()
.datum
.value(),
N_COLLECTIONS as u64
N_COLLECTIONS,
);
assert_eq!(stats.failed_collections.len(), 1);
logctx.cleanup_successful();
Expand Down Expand Up @@ -868,8 +867,7 @@ mod tests {
// Step time until there has been exactly `N_COLLECTIONS` collections.
tokio::time::pause();
let now = Instant::now();
let wait_for = test_wait_period();
while now.elapsed() < wait_for {
while now.elapsed() < TEST_WAIT_PERIOD {
tokio::time::advance(TICK_INTERVAL).await;
}

Expand All @@ -896,7 +894,7 @@ mod tests {
.unwrap()
.datum
.value(),
N_COLLECTIONS as u64
N_COLLECTIONS,
);
assert_eq!(stats.failed_collections.len(), 1);
logctx.cleanup_successful();
Expand Down

0 comments on commit 5e76841

Please sign in to comment.