add test: unit tests for MessageUtils utility class#10558
Conversation
- Add comprehensive tests for getShardingKeyIndex() with various key types - Add tests for getShardingKeyIndexByMsg() including null sharding key - Add tests for getShardingKeyIndexes() with empty and multiple messages - Add tests for deleteProperty() covering edge cases: * Delete first/last/middle keys * Null input handling * Non-existent key handling * Substring key matching (mykey vs key) * Value containing key name Addresses the gap in test coverage for MessageUtils, aligning with Apache RocketMQ's contribution guidelines requiring 80%+ test coverage.
RockteMQ-AI
left a comment
There was a problem hiding this comment.
Review by github-manager-bot
Summary
This PR adds unit tests for MessageUtils in the common module, covering all 4 public methods (getShardingKeyIndex, getShardingKeyIndexByMsg, getShardingKeyIndexes, deleteProperty). Good effort filling the test coverage gap — the edge case coverage (null input, empty collection, substring matching) is thoughtful.
Findings
-
[Warning]
MessageUtilsTest.java— Unused import:java.nio.charset.StandardCharsetsis imported but never referenced in the test class. Please remove it. -
[Info]
testGetShardingKeyIndex,testGetShardingKeyIndex_EmptyKey,testGetShardingKeyIndex_ChineseKey— Weak assertions: These tests only verifyisBetween(0, indexSize - 1), which is trivially true for any valid hash-mod operation. Consider adding determinism checks instead:// Verify same input produces same output int index1 = MessageUtils.getShardingKeyIndex("test-key", 10); int index2 = MessageUtils.getShardingKeyIndex("test-key", 10); assertThat(index1).isEqualTo(index2);
-
[Info]
testGetShardingKeyIndex_DifferentKeys— The test name implies different keys should map to different indices, but the assertions never verifyindex1 != index2. If the goal is to verify distribution, add:assertThat(index1).isNotEqualTo(index2);
Note: this is not guaranteed for all input pairs with modular hashing, so choose inputs known to differ, or rename the test to clarify intent (e.g.,
testGetShardingKeyIndex_Consistency). -
[Info]
testGetShardingKeyIndexByMsg— Consider adding a test case where the message has no sharding key property set, verifying the fallback to empty string:MessageExt msgNoKey = new MessageExt(); int index = MessageUtils.getShardingKeyIndexByMsg(msgNoKey, 10); assertThat(index).isBetween(0, 9);
-
[Info] Naming convention: Apache RocketMQ test methods typically use camelCase (e.g.,
testGetShardingKeyIndexWithEmptyKey) rather than underscore-separated names (testGetShardingKeyIndex_EmptyKey). Consider aligning with the project convention for consistency.
Suggestions
Overall a solid contribution that meaningfully improves test coverage. The suggestions above are mostly about strengthening assertions and consistency. The substring matching test (testDeleteProperty_SubstringKey) is particularly good — it correctly validates that "mykey" is not deleted when targeting "key".
Automated review by github-manager-bot
- Remove unused import StandardCharsets - Rename test methods to camelCase (remove underscores) per project convention - Add determinism check: same input produces same output - Add test for MessageExt with no sharding key property - Clarify test method names (WithEmptyKey, WithChineseKey, etc.)
RockteMQ-AI
left a comment
There was a problem hiding this comment.
Review by github-manager-bot (Re-review after new commit)
Summary
Re-review triggered by new commit 86265c3e addressing previous feedback. The changes properly address all prior review comments.
Changes in Latest Commit
- ✅ Removed unused
StandardCharsetsimport - ✅ Renamed test methods from underscore to camelCase convention (e.g.,
testGetShardingKeyIndex_EmptyKey→testGetShardingKeyIndexWithEmptyKey) - ✅ Improved
testGetShardingKeyIndexConsistencyto properly verify determinism (same input → same output) - ✅ Added clarifying comments for fallback behavior
Verdict
All previous feedback addressed. Test class provides good coverage of MessageUtils utility methods including edge cases (empty key, Chinese characters, null handling, batch operations).
LGTM 👍
Automated re-review by github-manager-bot
Description
Please provide a brief description of your PR.
This PR adds comprehensive unit tests for the
MessageUtilsutility class in thecommonmodule, which previously had zero test coverage.Type of change
Please delete options that are not relevant.
How Has This Been Tested?
Please describe the tests you ran to verify your changes.
MessageUtilsChecklist: