-
Notifications
You must be signed in to change notification settings - Fork 144
[ISSUE #1342]🧪Add Unit test for BrokerMemberGroup #1343
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Conversation
WalkthroughThe changes introduce a new test module in the Changes
Assessment against linked issues
Possibly related PRs
Suggested reviewers
📜 Recent review detailsConfiguration used: .coderabbit.yaml 📒 Files selected for processing (1)
🚧 Files skipped from review as they are similar to previous changes (1)
Thank you for using CodeRabbit. We offer it for free to the OSS community and would appreciate your support in helping us grow. If you find it useful, would you consider giving us a shout-out on your favorite social media? 🪧 TipsChatThere are 3 ways to chat with CodeRabbit:
Note: Be mindful of the bot's finite context window. It's strongly recommended to break down tasks such as reading entire modules into smaller chunks. For a focused discussion, use review comments to chat about specific files and their changes, instead of using the PR comments. CodeRabbit Commands (Invoked using PR comments)
Other keywords and placeholders
Documentation and Community
|
🔊@ButterflyEffect1991 🚀Thanks for your contribution 🎉. CodeRabbit(AI) will review your code first 🔥 |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Actionable comments posted: 4
🧹 Outside diff range and nitpick comments (2)
rocketmq-remoting/src/protocol/body/broker_body/broker_member_group.rs (2)
55-64
: Consider adding edge cases to constructor testWhile the basic test case is good, consider adding tests for:
- Empty strings for cluster and broker_name
- Unicode characters in the strings
- Very long strings
#[test] fn new_handles_edge_cases() { // Empty strings let group = BrokerMemberGroup::new( CheetahString::from(""), CheetahString::from("") ); assert!(group.cluster.is_empty()); // Unicode characters let group = BrokerMemberGroup::new( CheetahString::from("集群"), CheetahString::from("代理人") ); assert_eq!(group.cluster, "集群"); }
47-129
: Good test coverage with room for improvementThe test suite provides good coverage of the basic functionality. However, consider:
- Adding property-based tests using frameworks like proptest for more thorough testing
- Adding documentation comments to describe test scenarios
- Measuring code coverage to identify any missed paths
Consider organizing tests into sub-modules for better maintainability:
#[cfg(test)] mod tests { mod serialization { // All serialization related tests } mod deserialization { // All deserialization related tests } mod constructor { // All constructor related tests } }
📜 Review details
Configuration used: .coderabbit.yaml
Review profile: CHILL
📒 Files selected for processing (1)
rocketmq-remoting/src/protocol/body/broker_body/broker_member_group.rs
(1 hunks)
🔇 Additional comments (1)
rocketmq-remoting/src/protocol/body/broker_body/broker_member_group.rs (1)
47-54
: LGTM! Test module setup is well-structured
The test module is properly configured with appropriate imports and follows Rust testing conventions.
#[test] | ||
fn broker_member_group_serializes_correctly() { | ||
let cluster = CheetahString::from("test_cluster"); | ||
let broker_name = CheetahString::from("test_broker"); | ||
let mut broker_addrs = HashMap::new(); | ||
broker_addrs.insert(1, CheetahString::from("127.0.0.1:10911")); | ||
let group = BrokerMemberGroup { | ||
cluster: cluster.clone(), | ||
broker_name: broker_name.clone(), | ||
broker_addrs: broker_addrs.clone(), | ||
}; | ||
|
||
let serialized = serde_json::to_string(&group).unwrap(); | ||
let expected = format!( | ||
r#"{{"cluster":"{}","brokerName":"{}","brokerAddrs":{{"1":"{}"}}}}"#, | ||
cluster, | ||
broker_name, | ||
broker_addrs.get(&1).unwrap() | ||
); | ||
assert_eq!(serialized, expected); | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
🛠️ Refactor suggestion
Improve serialization test robustness
The current test has some potential issues:
- Manual string comparison is fragile as JSON field order isn't guaranteed
- Limited test coverage for broker_addrs variations
Consider this improved approach:
#[test]
fn broker_member_group_serializes_correctly() {
let mut group = BrokerMemberGroup::new(
CheetahString::from("test_cluster"),
CheetahString::from("test_broker")
);
// Test empty broker_addrs
let serialized = serde_json::to_string(&group).unwrap();
let deserialized: serde_json::Value = serde_json::from_str(&serialized).unwrap();
assert_eq!(deserialized["brokerAddrs"], serde_json::json!({}));
// Test multiple broker addresses
group.broker_addrs.insert(1, CheetahString::from("127.0.0.1:10911"));
group.broker_addrs.insert(2, CheetahString::from("127.0.0.1:10912"));
let serialized = serde_json::to_string(&group).unwrap();
let deserialized: serde_json::Value = serde_json::from_str(&serialized).unwrap();
assert_eq!(deserialized["cluster"], "test_cluster");
assert_eq!(deserialized["brokerAddrs"]["1"], "127.0.0.1:10911");
assert_eq!(deserialized["brokerAddrs"]["2"], "127.0.0.1:10912");
}
#[test] | ||
fn broker_member_group_deserializes_correctly() { | ||
let data = r#"{"cluster":"test_cluster","brokerName":"test_broker","brokerAddrs":{"1":"127.0.0.1:10911"}}"#; | ||
let group: BrokerMemberGroup = serde_json::from_str(data).unwrap(); | ||
|
||
assert_eq!(group.cluster, CheetahString::from("test_cluster")); | ||
assert_eq!(group.broker_name, CheetahString::from("test_broker")); | ||
assert_eq!( | ||
group.broker_addrs.get(&1).unwrap(), | ||
&CheetahString::from("127.0.0.1:10911") | ||
); | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
🛠️ Refactor suggestion
Enhance deserialization test coverage
The current test only covers the happy path. Consider adding tests for:
- Error cases with malformed JSON
- Missing optional fields
- Additional unknown fields (should be ignored)
#[test]
fn broker_member_group_deserializes_correctly() {
// Test missing fields
let data = r#"{"cluster":"test_cluster","brokerName":"test_broker"}"#;
let group: BrokerMemberGroup = serde_json::from_str(data).unwrap();
assert!(group.broker_addrs.is_empty());
// Test unknown fields
let data = r#"{"cluster":"test_cluster","brokerName":"test_broker","unknown":"field"}"#;
let _group: BrokerMemberGroup = serde_json::from_str(data).unwrap();
// Test error cases
let data = r#"{"cluster":123,"brokerName":"test_broker"}"#;
assert!(serde_json::from_str::<BrokerMemberGroup>(data).is_err());
}
#[test] | ||
fn get_broker_member_group_response_body_serializes_correctly() { | ||
let cluster = CheetahString::from("test_cluster"); | ||
let broker_name = CheetahString::from("test_broker"); | ||
let group = BrokerMemberGroup::new(cluster.clone(), broker_name.clone()); | ||
let response_body = GetBrokerMemberGroupResponseBody { | ||
broker_member_group: Some(group.clone()), | ||
}; | ||
|
||
let serialized = serde_json::to_string(&response_body).unwrap(); | ||
let expected = format!( | ||
r#"{{"brokerMemberGroup":{{"cluster":"{}","brokerName":"{}","brokerAddrs":{{}}}}}}"#, | ||
cluster, broker_name | ||
); | ||
assert_eq!(serialized, expected); | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
🛠️ Refactor suggestion
Improve response body serialization test robustness
Similar to the earlier serialization test, this test could be improved by:
- Avoiding manual string comparison
- Testing both Some and None cases for broker_member_group
#[test]
fn get_broker_member_group_response_body_serializes_correctly() {
// Test None case
let response_body = GetBrokerMemberGroupResponseBody { broker_member_group: None };
let serialized = serde_json::to_string(&response_body).unwrap();
let deserialized: serde_json::Value = serde_json::from_str(&serialized).unwrap();
assert!(deserialized["brokerMemberGroup"].is_null());
// Test Some case
let group = BrokerMemberGroup::new(
CheetahString::from("test_cluster"),
CheetahString::from("test_broker")
);
let response_body = GetBrokerMemberGroupResponseBody {
broker_member_group: Some(group),
};
let serialized = serde_json::to_string(&response_body).unwrap();
let deserialized: serde_json::Value = serde_json::from_str(&serialized).unwrap();
assert_eq!(deserialized["brokerMemberGroup"]["cluster"], "test_cluster");
}
#[test] | ||
fn get_broker_member_group_response_body_deserializes_correctly() { | ||
let data = r#"{"brokerMemberGroup":{"cluster":"test_cluster","brokerName":"test_broker","brokerAddrs":{}}}"#; | ||
let response_body: GetBrokerMemberGroupResponseBody = serde_json::from_str(data).unwrap(); | ||
|
||
assert!(response_body.broker_member_group.is_some()); | ||
let group = response_body.broker_member_group.unwrap(); | ||
assert_eq!(group.cluster, CheetahString::from("test_cluster")); | ||
assert_eq!(group.broker_name, CheetahString::from("test_broker")); | ||
assert!(group.broker_addrs.is_empty()); | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
🛠️ Refactor suggestion
Enhance response body deserialization test coverage
The test should cover additional cases:
- Deserializing when brokerMemberGroup is null/missing
- Error cases with malformed JSON
#[test]
fn get_broker_member_group_response_body_deserializes_correctly() {
// Test null case
let data = r#"{"brokerMemberGroup":null}"#;
let response: GetBrokerMemberGroupResponseBody = serde_json::from_str(data).unwrap();
assert!(response.broker_member_group.is_none());
// Test missing field
let data = r#"{}"#;
let response: GetBrokerMemberGroupResponseBody = serde_json::from_str(data).unwrap();
assert!(response.broker_member_group.is_none());
// Test malformed data
let data = r#"{"brokerMemberGroup":{"cluster":123}}"#;
assert!(serde_json::from_str::<GetBrokerMemberGroupResponseBody>(data).is_err());
}
b90fefb
to
e3750a4
Compare
🔊@ButterflyEffect1991 🚀Thanks for your contribution 🎉. CodeRabbit(AI) will review your code first 🔥 |
Which Issue(s) This PR Fixes(Closes)
Fixes #1342
Brief Description
How Did You Test This Change?
Summary by CodeRabbit
BrokerMemberGroup
andGetBrokerMemberGroupResponseBody
structs.BrokerMemberGroup
andGetBrokerMemberGroupResponseBody
.