Skip to content
Open
Show file tree
Hide file tree
Changes from 5 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
37 changes: 37 additions & 0 deletions dpd-client/tests/integration_tests/common.rs
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,13 @@ pub const ADMIN_LOCAL_MULTICAST_PREFIX: u16 = 0xFF04;
// packets before notifying us that any are ready.
const TEST_PCAP_TIMEOUT_MS: i32 = 1;

// Interval and attempt count for polling a counter until it reaches an
// expected value.
//
// Note: counter updates from the model can lag the packet itself.
const COUNTER_POLL_INTERVAL: Duration = Duration::from_millis(100);
const COUNTER_POLL_ATTEMPTS: usize = 20;

// Physical port number
#[derive(Clone, Copy, PartialOrd, Ord, Hash, PartialEq, Eq)]
pub struct PhysPort(pub u16);
Expand Down Expand Up @@ -827,6 +834,35 @@ impl Switch {
}
}

pub async fn check_counter_incremented(
switch: &Switch,
counter_name: &str,
baseline: u64,
expected_increment: u64,
client_name: Option<&str>,
) -> anyhow::Result<u64> {
let expected = baseline + expected_increment;
let mut new_value = 0;

// Poll for the counter value (with timeout)
for _i in 0..COUNTER_POLL_ATTEMPTS {
thread::sleep(COUNTER_POLL_INTERVAL);
new_value =
switch.get_counter(counter_name, client_name).await.unwrap();
Comment thread
zeeshanlakhani marked this conversation as resolved.
Resolved

if new_value == expected {
return Ok(new_value);
}
}

// Counter didn't increment as expected
Err(anyhow!(
Comment thread
zeeshanlakhani marked this conversation as resolved.
Resolved
"Counter '{counter_name}' expected to increase by \
{expected_increment} (from {baseline} to {expected}), but only \
Comment thread
zeeshanlakhani marked this conversation as resolved.
Resolved
reached {new_value}"
))
}

// Construct a single TCP packet with an optional payload
pub fn gen_tcp_packet_loaded(
src: Endpoint,
Expand Down Expand Up @@ -1444,5 +1480,6 @@ pub mod prelude {
pub use super::Switch;
pub use super::TestPacket;
pub use super::TestResult;
pub use super::check_counter_incremented;
pub use super::get_switch;
}
Loading