Skip to content

Commit

Permalink
Add check_on_drop property to Sniffer
Browse files Browse the repository at this point in the history
..If this flag is set to true, the `Sniffer` **will**
validate that all the downstrea and upstream messages were handled
before dropping, otherwise they are ignored.
  • Loading branch information
jbesraa committed Nov 20, 2024
1 parent 9c94856 commit 27eec57
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 19 deletions.
3 changes: 2 additions & 1 deletion roles/tests-integration/tests/common/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -198,8 +198,9 @@ pub async fn start_sniffer(
identifier: String,
listening_address: SocketAddr,
upstream: SocketAddr,
check_on_drop: bool,
) -> Sniffer {
let sniffer = Sniffer::new(identifier, listening_address, upstream).await;
let sniffer = Sniffer::new(identifier, listening_address, upstream, check_on_drop).await;
let sniffer_clone = sniffer.clone();
tokio::spawn(async move {
sniffer_clone.start().await;
Expand Down
39 changes: 22 additions & 17 deletions roles/tests-integration/tests/common/sniffer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,7 @@ pub struct Sniffer {
upstream_address: SocketAddr,
messages_from_downstream: MessagesAggregator,
messages_from_upstream: MessagesAggregator,
check_on_drop: bool,
}

impl Sniffer {
Expand All @@ -60,13 +61,15 @@ impl Sniffer {
identifier: String,
listening_address: SocketAddr,
upstream_address: SocketAddr,
check_on_drop: bool,
) -> Self {
Self {
identifier,
listening_address,
upstream_address,
messages_from_downstream: MessagesAggregator::new(),
messages_from_upstream: MessagesAggregator::new(),
check_on_drop,
}
}

Expand Down Expand Up @@ -436,23 +439,25 @@ macro_rules! assert_jd_message {
// This is useful to ensure that the test has checked all exchanged messages between the roles.
impl Drop for Sniffer {
fn drop(&mut self) {
// Don't print backtrace on panic
std::panic::set_hook(Box::new(|_| {
println!();
}));
if !self.messages_from_downstream.is_empty() {
println!(
"Sniffer {}: You didn't handle all downstream messages: {:?}",
self.identifier, self.messages_from_downstream
);
panic!();
}
if !self.messages_from_upstream.is_empty() {
println!(
"Sniffer{}: You didn't handle all upstream messages: {:?}",
self.identifier, self.messages_from_upstream
);
panic!();
if self.check_on_drop {
// Don't print backtrace on panic
std::panic::set_hook(Box::new(|_| {
println!();
}));
if !self.messages_from_downstream.is_empty() {
println!(
"Sniffer {}: You didn't handle all downstream messages: {:?}",
self.identifier, self.messages_from_downstream
);
panic!();
}
if !self.messages_from_upstream.is_empty() {
println!(
"Sniffer{}: You didn't handle all upstream messages: {:?}",
self.identifier, self.messages_from_upstream
);
panic!();
}
}
}
}
Expand Down
9 changes: 8 additions & 1 deletion roles/tests-integration/tests/pool_integration.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,14 @@ async fn success_pool_template_provider_connection() {
let _tp = common::start_template_provider(tp_addr.port()).await;
let sniffer_identifier =
"success_pool_template_provider_connection tp_pool sniffer".to_string();
let sniffer = common::start_sniffer(sniffer_identifier, sniffer_addr, tp_addr).await;
let sniffer_check_on_drop = true;
let sniffer = common::start_sniffer(
sniffer_identifier,
sniffer_addr,
tp_addr,
sniffer_check_on_drop,
)
.await;
let _ = common::start_pool(Some(pool_addr), Some(sniffer_addr)).await;
// here we assert that the downstream(pool in this case) have sent `SetupConnection` message
// with the correct parameters, protocol, flags, min_version and max_version. Note that the
Expand Down

0 comments on commit 27eec57

Please sign in to comment.