use of io.confluent.kafkarest.entities.v3.ProduceResponse in project kafka-rest by confluentinc.
the class ProduceActionIntegrationTest method produceBinaryWithTimestamp.
@Test
public void produceBinaryWithTimestamp() throws Exception {
String clusterId = testEnv.kafkaCluster().getClusterId();
Instant timestamp = Instant.ofEpochMilli(1000);
ByteString key = ByteString.copyFromUtf8("foo");
ByteString value = ByteString.copyFromUtf8("bar");
ProduceRequest request = ProduceRequest.builder().setKey(ProduceRequestData.builder().setFormat(EmbeddedFormat.BINARY).setData(BinaryNode.valueOf(key.toByteArray())).build()).setValue(ProduceRequestData.builder().setFormat(EmbeddedFormat.BINARY).setData(BinaryNode.valueOf(value.toByteArray())).build()).setTimestamp(timestamp).setOriginalSize(0L).build();
Response response = testEnv.kafkaRest().target().path("/v3/clusters/" + clusterId + "/topics/" + TOPIC_NAME + "/records").request().accept(MediaType.APPLICATION_JSON).post(Entity.entity(request, MediaType.APPLICATION_JSON));
assertEquals(Status.OK.getStatusCode(), response.getStatus());
ProduceResponse actual = readProduceResponse(response);
ConsumerRecord<byte[], byte[]> produced = testEnv.kafkaCluster().getRecord(TOPIC_NAME, actual.getPartitionId(), actual.getOffset(), new ByteArrayDeserializer(), new ByteArrayDeserializer());
assertEquals(key, ByteString.copyFrom(produced.key()));
assertEquals(value, ByteString.copyFrom(produced.value()));
assertEquals(timestamp, Instant.ofEpochMilli(produced.timestamp()));
}
use of io.confluent.kafkarest.entities.v3.ProduceResponse in project kafka-rest by confluentinc.
the class ProduceActionTest method streamingRequests.
@Test
public void streamingRequests() throws Exception {
// config
final int TOTAL_NUMBER_OF_PRODUCE_CALLS_PROD1 = 1;
final int TOTAL_NUMBER_OF_STREAMING_CALLS = 4;
Properties properties = new Properties();
properties.put(PRODUCE_MAX_REQUESTS_PER_SECOND, Integer.toString(10000));
properties.put(PRODUCE_MAX_BYTES_PER_SECOND, Integer.toString(999999999));
properties.put(PRODUCE_RATE_LIMIT_ENABLED, "true");
properties.put(PRODUCE_RATE_LIMIT_CACHE_EXPIRY_MS, Integer.toString(3600000));
// setup
ChunkedOutputFactory chunkedOutputFactory = mock(ChunkedOutputFactory.class);
ChunkedOutput<ResultOrError> mockedChunkedOutput = getChunkedOutput(chunkedOutputFactory, TOTAL_NUMBER_OF_PRODUCE_CALLS_PROD1);
Provider<RequestRateLimiter> countLimitProvider = mock(Provider.class);
Provider<RequestRateLimiter> bytesLimitProvider = mock(Provider.class);
RequestRateLimiter rateLimiterForCount = mock(RequestRateLimiter.class);
RequestRateLimiter rateLimiterForBytes = mock(RequestRateLimiter.class);
expect(countLimitProvider.get()).andReturn(rateLimiterForCount);
expect(bytesLimitProvider.get()).andReturn(rateLimiterForBytes);
rateLimiterForCount.rateLimit(anyInt());
rateLimiterForBytes.rateLimit(anyInt());
rateLimiterForCount.rateLimit(anyInt());
rateLimiterForBytes.rateLimit(anyInt());
rateLimiterForCount.rateLimit(anyInt());
rateLimiterForBytes.rateLimit(anyInt());
rateLimiterForCount.rateLimit(anyInt());
rateLimiterForBytes.rateLimit(anyInt());
replay(countLimitProvider, bytesLimitProvider, rateLimiterForCount, rateLimiterForBytes);
ProduceAction produceAction1 = getProduceAction(properties, chunkedOutputFactory, TOTAL_NUMBER_OF_STREAMING_CALLS, countLimitProvider, bytesLimitProvider);
MappingIterator<ProduceRequest> requests = getStreamingProduceRequestsMappingIterator(4);
// expected results
ProduceResponse produceResponse1 = getProduceResponse(0);
ResultOrError resultOrErrorOKProd1 = ResultOrError.result(produceResponse1);
expect(mockedChunkedOutput.isClosed()).andReturn(false);
mockedChunkedOutput.write(resultOrErrorOKProd1);
ProduceResponse produceResponse2 = getProduceResponse(1);
ResultOrError resultOrErrorOKProd2 = ResultOrError.result(produceResponse2);
expect(mockedChunkedOutput.isClosed()).andReturn(false);
mockedChunkedOutput.write(resultOrErrorOKProd2);
ProduceResponse produceResponse3 = getProduceResponse(2);
ResultOrError resultOrErrorOKProd3 = ResultOrError.result(produceResponse3);
expect(mockedChunkedOutput.isClosed()).andReturn(false);
mockedChunkedOutput.write(resultOrErrorOKProd3);
ProduceResponse produceResponse4 = getProduceResponse(3);
ResultOrError resultOrErrorOKProd4 = ResultOrError.result(produceResponse4);
expect(mockedChunkedOutput.isClosed()).andReturn(false);
mockedChunkedOutput.write(resultOrErrorOKProd4);
mockedChunkedOutput.close();
replay(mockedChunkedOutput, chunkedOutputFactory);
// run test
FakeAsyncResponse fakeAsyncResponse1 = new FakeAsyncResponse();
produceAction1.produce(fakeAsyncResponse1, "clusterId", "topicName", requests);
// check results
EasyMock.verify(requests);
EasyMock.verify(mockedChunkedOutput);
}
use of io.confluent.kafkarest.entities.v3.ProduceResponse in project kafka-rest by confluentinc.
the class ProduceActionTest method produceWithByteLimit.
@Test
public void produceWithByteLimit() throws Exception {
// config
final int TOTAL_NUMBER_OF_PRODUCE_CALLS = 2;
Properties properties = new Properties();
properties.put(PRODUCE_MAX_REQUESTS_PER_SECOND, "100");
properties.put(PRODUCE_MAX_BYTES_PER_SECOND, // first record is 25 bytes long
Integer.toString(30));
properties.put(PRODUCE_RATE_LIMIT_CACHE_EXPIRY_MS, "3600000");
properties.put(PRODUCE_RATE_LIMIT_ENABLED, "true");
// setup
ChunkedOutputFactory chunkedOutputFactory = mock(ChunkedOutputFactory.class);
ChunkedOutput<ResultOrError> mockedChunkedOutput = getChunkedOutput(chunkedOutputFactory, TOTAL_NUMBER_OF_PRODUCE_CALLS);
Provider<RequestRateLimiter> countLimitProvider = mock(Provider.class);
Provider<RequestRateLimiter> bytesLimitProvider = mock(Provider.class);
RequestRateLimiter rateLimiterForCount = mock(RequestRateLimiter.class);
RequestRateLimiter rateLimiterForBytes = mock(RequestRateLimiter.class);
expect(countLimitProvider.get()).andReturn(rateLimiterForCount);
expect(bytesLimitProvider.get()).andReturn(rateLimiterForBytes);
rateLimiterForCount.rateLimit(anyInt());
rateLimiterForBytes.rateLimit(anyInt());
rateLimiterForCount.rateLimit(anyInt());
rateLimiterForBytes.rateLimit(anyInt());
EasyMock.expectLastCall().andThrow(new RateLimitExceededException());
replay(countLimitProvider, bytesLimitProvider, rateLimiterForCount, rateLimiterForBytes);
ProduceAction produceAction = getProduceAction(properties, chunkedOutputFactory, 1, countLimitProvider, bytesLimitProvider);
MappingIterator<ProduceRequest> requests = getProduceRequestsMappingIterator(TOTAL_NUMBER_OF_PRODUCE_CALLS);
// expected results
ProduceResponse produceResponse = getProduceResponse(0);
ResultOrError resultOrErrorOK = ResultOrError.result(produceResponse);
expect(mockedChunkedOutput.isClosed()).andReturn(false);
// successful first produce
mockedChunkedOutput.write(resultOrErrorOK);
mockedChunkedOutput.close();
ErrorResponse err = ErrorResponse.create(429, "Request rate limit exceeded: The rate limit of requests per second has been exceeded.");
ResultOrError resultOrErrorFail = ResultOrError.error(err);
expect(mockedChunkedOutput.isClosed()).andReturn(false);
// failing second produce
mockedChunkedOutput.write(resultOrErrorFail);
// error close
mockedChunkedOutput.close();
replay(mockedChunkedOutput, chunkedOutputFactory);
// run test
FakeAsyncResponse fakeAsyncResponse = new FakeAsyncResponse();
produceAction.produce(fakeAsyncResponse, "clusterId", "topicName", requests);
FakeAsyncResponse fakeAsyncResponse2 = new FakeAsyncResponse();
produceAction.produce(fakeAsyncResponse2, "clusterId", "topicName", requests);
// check results
// check results
verify(requests, mockedChunkedOutput, countLimitProvider, bytesLimitProvider, rateLimiterForCount, rateLimiterForBytes);
}
Aggregations