Skip to content

Commit

Permalink
add ActionResult::SustainConnection
Browse files Browse the repository at this point in the history
  • Loading branch information
plebhash committed Jul 9, 2024
1 parent 9db50a3 commit 92a6fbf
Show file tree
Hide file tree
Showing 3 changed files with 89 additions and 28 deletions.
114 changes: 86 additions & 28 deletions utils/message-generator/src/executor.rs
Original file line number Diff line number Diff line change
Expand Up @@ -199,35 +199,21 @@ impl Executor {
result
);

// If the connection should drop at this point then let's just break the loop
// Can't do anything else after the connection drops.
if *result == ActionResult::CloseConnection {
info!(
"Waiting 1 sec to make sure that remote have time to close the connection"
);
tokio::time::sleep(std::time::Duration::from_millis(1000)).await;
recv.recv()
.await
.expect_err("Expecting the connection to be closed: wasn't");
success = true;
break;
}

let message = match recv.recv().await {
Ok(message) => message,
Err(_) => {
success = false;
error!("Connection closed before receiving the message");
break;
}
};

let mut message: Sv2Frame<AnyMessage<'static>, _> = message.try_into().unwrap();
debug!("RECV {:#?}", message);
let header = message.get_header().unwrap();
let payload = message.payload();
match result {
ActionResult::MatchMessageType(message_type) => {
let message = match recv.recv().await {
Ok(message) => message,
Err(_) => {
success = false;
error!("Connection closed before receiving the message");
break;
}
};

let message: Sv2Frame<AnyMessage<'static>, _> = message.try_into().unwrap();
debug!("RECV {:#?}", message);
let header = message.get_header().unwrap();

if header.msg_type() != *message_type {
error!(
"WRONG MESSAGE TYPE expected: {} received: {}",
Expand All @@ -245,6 +231,20 @@ impl Executor {
message_type,
field_data, // Vec<(String, Sv2Type)>
)) => {
let message = match recv.recv().await {
Ok(message) => message,
Err(_) => {
success = false;
error!("Connection closed before receiving the message");
break;
}
};

let mut message: Sv2Frame<AnyMessage<'static>, _> =
message.try_into().unwrap();
debug!("RECV {:#?}", message);
let header = message.get_header().unwrap();
let payload = message.payload();
if subprotocol.as_str() == "CommonMessages" {
match (header.msg_type(), payload).try_into() {
Ok(roles_logic_sv2::parsers::CommonMessages::SetupConnection(m)) => {
Expand Down Expand Up @@ -532,6 +532,20 @@ impl Executor {
message_type: _,
fields,
} => {
let message = match recv.recv().await {
Ok(message) => message,
Err(_) => {
success = false;
error!("Connection closed before receiving the message");
break;
}
};

let mut message: Sv2Frame<AnyMessage<'static>, _> =
message.try_into().unwrap();
debug!("RECV {:#?}", message);
let header = message.get_header().unwrap();
let payload = message.payload();
if subprotocol.as_str() == "CommonMessages" {
match (header.msg_type(), payload).try_into() {
Ok(parsers::CommonMessages::SetupConnection(m)) => {
Expand Down Expand Up @@ -730,6 +744,19 @@ impl Executor {
};
}
ActionResult::MatchMessageLen(message_len) => {
let message = match recv.recv().await {
Ok(message) => message,
Err(_) => {
success = false;
error!("Connection closed before receiving the message");
break;
}
};

let mut message: Sv2Frame<AnyMessage<'static>, _> =
message.try_into().unwrap();
debug!("RECV {:#?}", message);
let payload = message.payload();
if payload.len() != *message_len {
error!(
"WRONG MESSAGE len expected: {} received: {}",
Expand All @@ -741,6 +768,18 @@ impl Executor {
}
}
ActionResult::MatchExtensionType(ext_type) => {
let message = match recv.recv().await {
Ok(message) => message,
Err(_) => {
success = false;
error!("Connection closed before receiving the message");
break;
}
};

let message: Sv2Frame<AnyMessage<'static>, _> = message.try_into().unwrap();
debug!("RECV {:#?}", message);
let header = message.get_header().unwrap();
if header.ext_type() != *ext_type {
error!(
"WRONG EXTENSION TYPE expected: {} received: {}",
Expand All @@ -752,7 +791,26 @@ impl Executor {
}
}
ActionResult::CloseConnection => {
todo!()
info!(
"Waiting 1 sec to make sure that remote has time to close the connection"
);
tokio::time::sleep(std::time::Duration::from_secs(1)).await;
if !recv.is_closed() {
error!("Expected connection to close, but it didn't. Test failed.");
success = false;
break;
}
}
ActionResult::SustainConnection => {
info!(
"Waiting 1 sec to make sure that remote has time to close the connection"
);
tokio::time::sleep(std::time::Duration::from_secs(1)).await;
if recv.is_closed() {
error!("Expected connection to sustain, but it didn't. Test failed.");
success = false;
break;
}
}
ActionResult::None => todo!(),
}
Expand Down
2 changes: 2 additions & 0 deletions utils/message-generator/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -191,6 +191,7 @@ enum ActionResult {
MatchMessageLen(usize),
MatchExtensionType(u16),
CloseConnection,
SustainConnection,
None,
}

Expand Down Expand Up @@ -225,6 +226,7 @@ impl std::fmt::Display for ActionResult {
write!(f, "MatchExtensionType: {}", extension_type)
}
ActionResult::CloseConnection => write!(f, "Close connection"),
ActionResult::SustainConnection => write!(f, "Sustain connection"),
ActionResult::GetMessageField {
subprotocol,
fields,
Expand Down
1 change: 1 addition & 0 deletions utils/message-generator/src/parser/actions.rs
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,7 @@ impl Sv2ActionParser {
"close_connection" => {
action_results.push(ActionResult::CloseConnection);
}
"sustain_connection" => action_results.push(ActionResult::SustainConnection),
"none" => {
action_results.push(ActionResult::None);
}
Expand Down

0 comments on commit 92a6fbf

Please sign in to comment.