Skip to content

Commit

Permalink
test(core/message): msgpack miri tests
Browse files Browse the repository at this point in the history
  • Loading branch information
loyd committed Jun 30, 2024
1 parent 7da3f42 commit ec6eac2
Showing 1 changed file with 47 additions and 10 deletions.
57 changes: 47 additions & 10 deletions elfo-core/src/message/any.rs
Original file line number Diff line number Diff line change
Expand Up @@ -575,18 +575,19 @@ mod tests_miri {

#[test]
fn json_serialize() {
let any_msg = AnyMessage::new(MyCoolMessage::example());
let any_message = AnyMessage::new(MyCoolMessage::example());
for mode in [SerdeMode::Normal, SerdeMode::Network] {
let dump =
crate::scope::with_serde_mode(mode, || serde_json::to_string(&any_msg).unwrap());
let dump = crate::scope::with_serde_mode(mode, || {
serde_json::to_string(&any_message).unwrap()
});
assert_eq!(
dump,
r#"["elfo-core","MyCoolMessage",{"field_a":123,"field_b":"Hello world","field_c":0.5}]"#
);
}

let dump = crate::scope::with_serde_mode(SerdeMode::Dumping, || {
serde_json::to_string(&any_msg).unwrap()
serde_json::to_string(&any_message).unwrap()
});
assert_eq!(
dump,
Expand All @@ -596,13 +597,49 @@ mod tests_miri {

#[test]
fn json_roundtrip() {
let msg = MyCoolMessage::example();
let any_msg = AnyMessage::new(msg.clone());
let serialized = serde_json::to_string(&any_msg).unwrap();
let message = MyCoolMessage::example();
let any_message = AnyMessage::new(message.clone());
let serialized = serde_json::to_string(&any_message).unwrap();

let deserialized_any_msg: AnyMessage = serde_json::from_str(&serialized).unwrap();
let deserialized_msg: MyCoolMessage = deserialized_any_msg.downcast().unwrap();
let deserialized_any_message: AnyMessage = serde_json::from_str(&serialized).unwrap();
let deserialized_message: MyCoolMessage = deserialized_any_message.downcast().unwrap();

assert_eq!(msg, deserialized_msg);
assert_eq!(deserialized_message, message);
}

#[test]
fn msgpack_roundtrip() {
let message = MyCoolMessage::example();
let any_message = AnyMessage::new(message.clone());

let mut buffer = Vec::new();
any_message.write_msgpack(&mut buffer, 1024).unwrap();

let deserialized_any_message =
AnyMessage::read_msgpack(&buffer, "elfo-core", "MyCoolMessage")
.unwrap()
.unwrap();
let deserialized_message: MyCoolMessage = deserialized_any_message.downcast().unwrap();

assert_eq!(deserialized_message, message);
}

#[test]
fn msgpack_nonexist() {
let maybe = AnyMessage::read_msgpack(&[], "nonexist", "NonExist").unwrap();
assert!(maybe.is_none());
}

#[test]
fn msgpack_limited() {
let message = MyCoolMessage::example();
let any_message = AnyMessage::new(message.clone());

let mut buffer = Vec::new();

for limit in 0..=20 {
let err = any_message.write_msgpack(&mut buffer, limit).unwrap_err();
assert!(format!("{:?}", err).contains("failed to write whole buffer"));
}
}
}

0 comments on commit ec6eac2

Please sign in to comment.