use of org.apache.kafka.common.requests.ResponseHeader in project kafka by apache.
the class NetworkClientTest method checkSimpleRequestResponse.
private void checkSimpleRequestResponse(NetworkClient networkClient) {
// has to be before creating any request, as it may send ApiVersionsRequest and its response is mocked with correlation id 0
awaitReady(networkClient, node);
ProduceRequest.Builder builder = new ProduceRequest.Builder((short) 1, 1000, Collections.<TopicPartition, MemoryRecords>emptyMap());
TestCallbackHandler handler = new TestCallbackHandler();
ClientRequest request = networkClient.newClientRequest(node.idString(), builder, time.milliseconds(), true, handler);
networkClient.send(request, time.milliseconds());
networkClient.poll(1, time.milliseconds());
assertEquals(1, networkClient.inFlightRequestCount());
ResponseHeader respHeader = new ResponseHeader(request.correlationId());
Struct resp = new Struct(ApiKeys.PRODUCE.responseSchema(ApiKeys.PRODUCE.latestVersion()));
resp.set("responses", new Object[0]);
Struct responseHeaderStruct = respHeader.toStruct();
int size = responseHeaderStruct.sizeOf() + resp.sizeOf();
ByteBuffer buffer = ByteBuffer.allocate(size);
responseHeaderStruct.writeTo(buffer);
resp.writeTo(buffer);
buffer.flip();
selector.completeReceive(new NetworkReceive(node.idString(), buffer));
List<ClientResponse> responses = networkClient.poll(1, time.milliseconds());
assertEquals(1, responses.size());
assertTrue("The handler should have executed.", handler.executed);
assertTrue("Should have a response body.", handler.response.hasResponse());
assertEquals("Should be correlated to the original request", request.correlationId(), handler.response.requestHeader().correlationId());
}
use of org.apache.kafka.common.requests.ResponseHeader in project kafka by apache.
the class NetworkClient method parseResponse.
public static AbstractResponse parseResponse(ByteBuffer responseBuffer, RequestHeader requestHeader) {
ResponseHeader responseHeader = ResponseHeader.parse(responseBuffer);
// Always expect the response version id to be the same as the request version id
ApiKeys apiKey = ApiKeys.forId(requestHeader.apiKey());
Struct responseBody = apiKey.responseSchema(requestHeader.apiVersion()).read(responseBuffer);
correlate(requestHeader, responseHeader);
return AbstractResponse.getResponse(apiKey, responseBody);
}
use of org.apache.kafka.common.requests.ResponseHeader in project kafka by apache.
the class NetworkClientTest method maybeSetExpectedApiVersionsResponse.
private void maybeSetExpectedApiVersionsResponse() {
ByteBuffer buffer = ApiVersionsResponse.API_VERSIONS_RESPONSE.serialize((short) 0, new ResponseHeader(0));
selector.delayedReceive(new DelayedReceive(node.idString(), new NetworkReceive(node.idString(), buffer)));
}
Aggregations