Repository: apache/kafka
File: server-common/src/test/java/org/apache/kafka/server/IntegrationTestUtils.java (Java)

Code before:
import org.apache.kafka.common.protocol.ByteBufferAccessor;
import org.apache.kafka.common.requests.AbstractRequest;
import org.apache.kafka.common.requests.AbstractResponse;
import org.apache.kafka.common.requests.RequestHeader;
import org.apache.kafka.common.requests.ResponseHeader;
import org.apache.kafka.common.utils.Utils;

import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.IOException;
import java.net.Socket;
import java.nio.ByteBuffer;

public class IntegrationTestUtils {

    private static int correlationId = 0;

    public static void sendRequest(Socket socket, byte[] request) throws IOException {
        DataOutputStream outgoing = new DataOutputStream(socket.getOutputStream());
        outgoing.writeInt(request.length);
        outgoing.write(request);
        outgoing.flush();
    }

    public static RequestHeader nextRequestHeader(ApiKeys apiKey, short apiVersion) {
        return new RequestHeader(apiKey, apiVersion, "client-id", 1);
    }

    @SuppressWarnings("unchecked")
    public static <T extends AbstractResponse> T receive(Socket socket, ApiKeys apiKey, short version) throws IOException, ClassCastException {
        var incoming = new DataInputStream(socket.getInputStream());
        int len = incoming.readInt();

        var responseBytes = new byte[len];
        incoming.readFully(responseBytes);

        var responseBuffer = ByteBuffer.wrap(responseBytes);
        ResponseHeader.parse(responseBuffer, apiKey.responseHeaderVersion(version));

        return (T) AbstractResponse.parseResponse(apiKey, new ByteBufferAccessor(responseBuffer), version);
    }

    @SuppressWarnings("unchecked")
    public static <T extends AbstractResponse> T sendAndReceive(
        AbstractRequest request,
        Socket socket
    ) throws IOException {
        correlationId += 1;
        RequestHeader header = new RequestHeader(request.apiKey(), request.version(), "client-id", correlationId);
        byte[] serializedBytes = Utils.toArray(request.serializeWithHeader(header));
        sendRequest(socket, serializedBytes);

Review comment (suggestion):
`1` -> `correlationId++`

Maybe we should use `AtomicInteger` instead to avoid potential thread issue.

Code after:
import org.apache.kafka.common.protocol.ByteBufferAccessor;
import org.apache.kafka.common.requests.AbstractRequest;
import org.apache.kafka.common.requests.AbstractResponse;
import org.apache.kafka.common.requests.RequestHeader;
import org.apache.kafka.common.requests.ResponseHeader;
import org.apache.kafka.common.utils.Utils;

import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.IOException;
import java.net.Socket;
import java.nio.ByteBuffer;
import java.util.concurrent.atomic.AtomicInteger;

public class IntegrationTestUtils {

    private static final AtomicInteger CORRELATION_ID = new AtomicInteger(0);

    public static void sendRequest(Socket socket, byte[] request) throws IOException {
        DataOutputStream outgoing = new DataOutputStream(socket.getOutputStream());
        outgoing.writeInt(request.length);
        outgoing.write(request);
        outgoing.flush();
    }

    public static RequestHeader nextRequestHeader(ApiKeys apiKey, short apiVersion) {
        return new RequestHeader(apiKey, apiVersion, "client-id", CORRELATION_ID.getAndIncrement());
    }

    @SuppressWarnings("unchecked")
    public static <T extends AbstractResponse> T receive(Socket socket, ApiKeys apiKey, short version) throws IOException, ClassCastException {
        var incoming = new DataInputStream(socket.getInputStream());
        int len = incoming.readInt();

        var responseBytes = new byte[len];
        incoming.readFully(responseBytes);

        var responseBuffer = ByteBuffer.wrap(responseBytes);
        ResponseHeader.parse(responseBuffer, apiKey.responseHeaderVersion(version));

        return (T) AbstractResponse.parseResponse(apiKey, new ByteBufferAccessor(responseBuffer), version);
    }

    public static <T extends AbstractResponse> T sendAndReceive(
        AbstractRequest request,
        Socket socket
    ) throws IOException {
        var header = nextRequestHeader(request.apiKey(), request.version());
        sendRequest(socket, Utils.toArray(request.serializeWithHeader(header)));
        return receive(socket, request.apiKey(), request.version());
    }