mimaison commented on code in PR #20359:
URL: https://github.com/apache/kafka/pull/20359#discussion_r2432765524
##########
clients/src/test/java/org/apache/kafka/common/utils/UtilsTest.java:
##########
@@ -117,6 +120,38 @@ public void testMurmur2() {
}
}
+ private static String toHexString(byte[] buf) {
+ StringBuilder bld = new StringBuilder();
+ for (byte b : buf) {
+ bld.append(String.format("%02x", b));
+ }
+ return bld.toString();
+ }
+
+ @Test
+ public void testMurmur2Checksum() throws NoSuchAlgorithmException {
+ // calculates the checksum of hashes of many different random byte
arrays of variable length
+ // this test detects any incompatible changes to the Murmur2
implementation with near certainty
+ MessageDigest md = MessageDigest.getInstance("SHA-256");
+ int numTrials = 100;
+ int maxLen = 1000;
+ SplittableRandom random = new SplittableRandom(0xbd4458b165652255L);
+
+ for (int len = 0; len <= maxLen; ++len) {
+ byte[] data = new byte[len];
+ for (int i = 0; i < numTrials; ++i) {
+ random.nextBytes(data);
+ int hash = Utils.murmur2(data);
+ md.update((byte) (hash >>> 0));
+ md.update((byte) (hash >>> 8));
+ md.update((byte) (hash >>> 16));
+ md.update((byte) (hash >>> 24));
+ }
+ }
+
+ assertEquals(toHexString(md.digest()),
"647f78f4a7942bf4d23d2f226458809dda799795513a0532a1ed999014f5f113");
Review Comment:
The arguments should be in the order order, first expected, then actual.
##########
clients/src/test/java/org/apache/kafka/common/utils/UtilsTest.java:
##########
@@ -117,6 +120,38 @@ public void testMurmur2() {
}
}
+ private static String toHexString(byte[] buf) {
+ StringBuilder bld = new StringBuilder();
+ for (byte b : buf) {
+ bld.append(String.format("%02x", b));
+ }
+ return bld.toString();
+ }
+
+ @Test
+ public void testMurmur2Checksum() throws NoSuchAlgorithmException {
+ // calculates the checksum of hashes of many different random byte
arrays of variable length
+ // this test detects any incompatible changes to the Murmur2
implementation with near certainty
+ MessageDigest md = MessageDigest.getInstance("SHA-256");
+ int numTrials = 100;
+ int maxLen = 1000;
+ SplittableRandom random = new SplittableRandom(0xbd4458b165652255L);
+
+ for (int len = 0; len <= maxLen; ++len) {
+ byte[] data = new byte[len];
+ for (int i = 0; i < numTrials; ++i) {
+ random.nextBytes(data);
+ int hash = Utils.murmur2(data);
Review Comment:
Can we just add all the `hash` values into a `long sum` variable to simplify
the logic a bit?
##########
clients/src/test/java/org/apache/kafka/common/utils/UtilsTest.java:
##########
@@ -117,6 +120,38 @@ public void testMurmur2() {
}
}
+ private static String toHexString(byte[] buf) {
+ StringBuilder bld = new StringBuilder();
+ for (byte b : buf) {
+ bld.append(String.format("%02x", b));
+ }
+ return bld.toString();
+ }
+
+ @Test
+ public void testMurmur2Checksum() throws NoSuchAlgorithmException {
+ // calculates the checksum of hashes of many different random byte
arrays of variable length
+ // this test detects any incompatible changes to the Murmur2
implementation with near certainty
+ MessageDigest md = MessageDigest.getInstance("SHA-256");
+ int numTrials = 100;
+ int maxLen = 1000;
+ SplittableRandom random = new SplittableRandom(0xbd4458b165652255L);
+
+ for (int len = 0; len <= maxLen; ++len) {
+ byte[] data = new byte[len];
+ for (int i = 0; i < numTrials; ++i) {
+ random.nextBytes(data);
+ int hash = Utils.murmur2(data);
Review Comment:
With the simplifications we may be able to merge with into the existing
`testMurmur2()` test.
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: [email protected]
For queries about this service, please contact Infrastructure at:
[email protected]