Repository: apache/kafka
File: group-coordinator/src/test/java/org/apache/kafka/coordinator/group/UtilsTest.java (Java)

Code before:

import org.junit.jupiter.api.Test;
import org.junit.jupiter.params.ParameterizedTest;
import org.junit.jupiter.params.provider.Arguments;
import org.junit.jupiter.params.provider.MethodSource;

import java.io.DataOutputStream;
import java.io.IOException;
import java.nio.ByteBuffer;
import java.util.LinkedHashMap;
import java.util.Map;
import java.util.stream.Stream;

import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertNotEquals;

public class UtilsTest {
    private static final Uuid FOO_TOPIC_ID = Uuid.randomUuid();
    private static final String FOO_TOPIC_NAME = "foo";
    private static final String BAR_TOPIC_NAME = "bar";
    private static final int FOO_NUM_PARTITIONS = 2;
    private static final MetadataImage FOO_METADATA_IMAGE = new MetadataImageBuilder()
        .addTopic(FOO_TOPIC_ID, FOO_TOPIC_NAME, FOO_NUM_PARTITIONS)
        .addRacks()
        .build();
    private static final XXHash64 LZ4_HASH_INSTANCE = XXHashFactory.fastestInstance().hash64();

    @Test
    void testComputeTopicHash() throws IOException {
        long result = Utils.computeTopicHash(FOO_METADATA_IMAGE.topics().getTopic(FOO_TOPIC_ID), FOO_METADATA_IMAGE.cluster());

        try (ByteBufferOutputStream bbos = new ByteBufferOutputStream(512);
             DataOutputStream dos = new DataOutputStream(bbos)) {
            dos.writeByte(0); // magic byte
            dos.writeLong(FOO_TOPIC_ID.hashCode()); // topic ID
            dos.writeUTF(FOO_TOPIC_NAME); // topic name
            dos.writeInt(FOO_NUM_PARTITIONS); // number of partitions
            dos.writeInt(0); // partition 0
            dos.writeUTF("5:rack0,5:rack1"); // rack of partition 0
            dos.writeInt(1); // partition 1
            dos.writeUTF("5:rack1,5:rack2"); // rack of partition 1
            dos.flush();
            ByteBuffer topicBytes = bbos.buffer().flip();
            assertEquals(LZ4_HASH_INSTANCE.hash(topicBytes, 0), result);
        }
    }

    @Test
    void testComputeTopicHashWithDifferentMagicByte() throws IOException {
        long result = Utils.computeTopicHash(FOO_METADATA_IMAGE.topics().getTopic(FOO_TOPIC_ID), FOO_METADATA_IMAGE.cluster());


Review comment (suggestion):
`XXH3` seems to be the fastest implementation. Did we consider using that?

https://github.com/Cyan4973/xxHash

Code after:
import org.junit.jupiter.params.ParameterizedTest;
import org.junit.jupiter.params.provider.Arguments;
import org.junit.jupiter.params.provider.MethodSource;

import java.util.LinkedHashMap;
import java.util.Map;
import java.util.stream.Stream;

import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertNotEquals;

public class UtilsTest {
    private static final Uuid FOO_TOPIC_ID = Uuid.randomUuid();
    private static final String FOO_TOPIC_NAME = "foo";
    private static final String BAR_TOPIC_NAME = "bar";
    private static final int FOO_NUM_PARTITIONS = 2;
    private static final MetadataImage FOO_METADATA_IMAGE = new MetadataImageBuilder()
        .addTopic(FOO_TOPIC_ID, FOO_TOPIC_NAME, FOO_NUM_PARTITIONS)
        .addRacks()
        .build();

    @Test
    void testNonExistingTopicName() {
        assertEquals(0, Utils.computeTopicHash("unknown", FOO_METADATA_IMAGE));
    }

    @Test
    void testComputeTopicHash() {
        long result = Utils.computeTopicHash(FOO_TOPIC_NAME, FOO_METADATA_IMAGE);

        long expected = Hashing.xxh3_64().hashStream()
            .putByte((byte) 0)
            .putLong(FOO_TOPIC_ID.getMostSignificantBits())
            .putLong(FOO_TOPIC_ID.getLeastSignificantBits())
            .putString(FOO_TOPIC_NAME)
            .putInt(FOO_NUM_PARTITIONS)
            .putInt(0) // partition 0
            .putInt(5) // length of rack0
            .putString("rack0") // The first rack in partition 0
            .putInt(5) // length of rack1
            .putString("rack1") // The second rack in partition 0
            .putInt(1) // partition 1
            .putInt(5) // length of rack0
            .putString("rack1") // The first rack in partition 1
            .putInt(5) // length of rack1
            .putString("rack2") // The second rack in partition 1
            .getAsLong();
        assertEquals(expected, result);
    }

    @Test