Skip to content

Commit

Permalink
feat: test Disconnect (de)serialization and edge cases
Browse files Browse the repository at this point in the history
  • Loading branch information
vvp committed Jul 27, 2023
1 parent c545ce6 commit 610f850
Showing 1 changed file with 41 additions and 0 deletions.
41 changes: 41 additions & 0 deletions node/narwhal/src/event/disconnect.rs
Original file line number Diff line number Diff line change
Expand Up @@ -63,3 +63,44 @@ impl EventTrait for Disconnect {
}
}
}

#[cfg(test)]
mod tests {
use crate::{event::EventTrait, Disconnect, DisconnectReason};
use bytes::{BufMut, BytesMut};

#[test]
fn serialize_deserialize() {
// TODO switch to an iteration method that doesn't require manually updating this vec if enums are added
let all_reasons = vec![
DisconnectReason::ProtocolViolation,
DisconnectReason::NoReasonGiven,
DisconnectReason::InvalidChallengeResponse,
DisconnectReason::OutdatedClientVersion,
];

for reason in all_reasons.iter() {
let disconnect = Disconnect::from(*reason);
let mut buf = BytesMut::with_capacity(64).writer();
Disconnect::serialize(&disconnect, &mut buf).unwrap();

let disconnect = Disconnect::deserialize(buf.get_ref().clone()).unwrap();
assert_eq!(reason, &disconnect.reason);
}
}

#[test]
fn deserializing_empty_defaults_no_reason() {
let buf = BytesMut::with_capacity(64).writer();
let disconnect = Disconnect::deserialize(buf.get_ref().clone()).unwrap();
assert_eq!(disconnect.reason, DisconnectReason::NoReasonGiven);
}

#[test]
#[should_panic(expected = "Invalid 'Disconnect' event")]
fn deserializing_invalid_data_panics() {
let mut buf = BytesMut::with_capacity(64).writer();
bincode::serialize_into(&mut buf, "not a DisconnectReason-value").unwrap();
let _disconnect = Disconnect::deserialize(buf.get_ref().clone()).unwrap();
}
}

0 comments on commit 610f850

Please sign in to comment.