Skip to content

Commit

Permalink
Improve consumer assertion
Browse files Browse the repository at this point in the history
  • Loading branch information
rukai committed Oct 17, 2024
1 parent 899ee23 commit 4ee9e61
Showing 1 changed file with 51 additions and 19 deletions.
70 changes: 51 additions & 19 deletions test-helpers/src/connection/kafka/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -200,25 +200,57 @@ impl KafkaConsumer {
Self::Java(java) => java.consume().await,
};

let topic = &expected_response.topic_name;
assert_eq!(
expected_response.topic_name, response.topic_name,
"Unexpected topic"
);
assert_eq!(
expected_response.message, response.message,
"Unexpected message for topic {topic}"
);
assert_eq!(
expected_response.key, response.key,
"Unexpected key for topic {topic}"
);

if expected_response.offset.is_some() {
assert_eq!(
expected_response.offset, response.offset,
"Unexpected offset for topic {topic}"
);
// Construct an error message that gives as much context as possible as to what went wrong.
let mut error = false;

let expected_topic = &expected_response.topic_name;
let actual_topic = &response.topic_name;
let topic_result = if expected_topic == actual_topic {
"️and it matched".into()
} else {
error = true;
format!("but the topic was {actual_topic:?}")
};

let expected_message = &expected_response.message;
let actual_message = &response.message;
let message_result = if expected_message == actual_message {
"and it matched".into()
} else {
error = true;
format!("but the message was {actual_message:?}")
};

let expected_key = &expected_response.key;
let actual_key = &response.key;
let key_result = if expected_key == actual_key {
"and it matched".into()
} else {
error = true;
format!("but the key was {actual_key:?}")
};

let expected_offset = &expected_response.offset;
let actual_offset = &response.offset;
let offset_result = if expected_offset.is_some() {
if expected_offset == actual_offset {
format!("expected offset {expected_offset:?} and it matched")
} else {
error = true;
format!("expected offset {expected_offset:?} but the offset was {actual_offset:?}")
}
} else {
format!("No offset expected and the offset was {actual_offset:?}")
};

if error {
panic!(
r#"Consumed an unexpected record:
expected topic {expected_topic:?} {topic_result}
expected message {expected_message:?} {message_result}
expected key {expected_key:?} {key_result}
{offset_result}"#
)
}
}

Expand Down

0 comments on commit 4ee9e61

Please sign in to comment.