use of org.apache.kafka.clients.producer.Callback in project brave by openzipkin.
the class TracingCallbackTest method onCompletion_shouldKeepContext_whenNotSampled.
@Test
public void onCompletion_shouldKeepContext_whenNotSampled() {
Span span = tracing.tracer().nextSpan(TraceContextOrSamplingFlags.NOT_SAMPLED);
Callback delegate = (metadata, exception) -> assertThat(tracing.tracer().currentSpan()).isEqualTo(span);
Callback tracingCallback = TracingCallback.create(delegate, span, tracing.currentTraceContext());
tracingCallback.onCompletion(null, null);
}
use of org.apache.kafka.clients.producer.Callback in project brave by openzipkin.
the class TracingCallbackTest method on_completion_should_finish_span.
@Test
public void on_completion_should_finish_span() {
Span span = tracing.tracer().nextSpan().start();
Callback tracingCallback = TracingCallback.create(null, span, currentTraceContext);
tracingCallback.onCompletion(createRecordMetadata(), null);
assertThat(spans.get(0).finishTimestamp()).isNotZero();
}
use of org.apache.kafka.clients.producer.Callback in project hono by eclipse.
the class KafkaBasedCommandSenderTest method sendCommandAndReceiveResponse.
private void sendCommandAndReceiveResponse(final VertxTestContext ctx, final String correlationId, final Integer responseStatus, final String responsePayload, final boolean expectSuccess, final int expectedStatusCode) {
final Context context = vertx.getOrCreateContext();
final Promise<Void> onProducerRecordSentPromise = Promise.promise();
mockProducer = new MockProducer<>(true, new StringSerializer(), new BufferSerializer()) {
@Override
public synchronized java.util.concurrent.Future<RecordMetadata> send(final ProducerRecord<String, Buffer> record, final Callback callback) {
return super.send(record, (metadata, exception) -> {
callback.onCompletion(metadata, exception);
context.runOnContext(v -> {
// decouple from current execution in order to run after the "send" result handler
onProducerRecordSentPromise.complete();
});
});
}
};
final var producerFactory = CachingKafkaProducerFactory.testFactory(vertx, (n, c) -> KafkaClientUnitTestHelper.newKafkaProducer(mockProducer));
commandSender = new KafkaBasedCommandSender(vertx, consumerConfig, producerFactory, producerConfig, NoopTracerFactory.create());
final Map<String, Object> headerProperties = new HashMap<>();
headerProperties.put("appKey", "appValue");
final String command = "setVolume";
final ConsumerRecord<String, Buffer> commandResponseRecord = commandResponseRecord(tenantId, deviceId, correlationId, responseStatus, Buffer.buffer(responsePayload));
final String responseTopic = new HonoTopic(HonoTopic.Type.COMMAND_RESPONSE, tenantId).toString();
final TopicPartition responseTopicPartition = new TopicPartition(responseTopic, 0);
mockConsumer.setRebalancePartitionAssignmentAfterSubscribe(List.of(responseTopicPartition));
mockConsumer.updatePartitions(responseTopicPartition, KafkaMockConsumer.DEFAULT_NODE);
mockConsumer.updateBeginningOffsets(Map.of(responseTopicPartition, 0L));
mockConsumer.updateEndOffsets(Map.of(responseTopicPartition, 0L));
onProducerRecordSentPromise.future().onComplete(ar -> {
LOG.debug("producer record sent, add command response record to mockConsumer");
// Send a command response with the same correlation id as that of the command
mockConsumer.addRecord(commandResponseRecord);
});
// This correlation id is used for both command and its response.
commandSender.setCorrelationIdSupplier(() -> correlationId);
commandSender.setKafkaConsumerSupplier(() -> mockConsumer);
context.runOnContext(v -> {
// Send a command to the device
commandSender.sendCommand(tenantId, deviceId, command, "text/plain", Buffer.buffer("test"), headerProperties).onComplete(ar -> {
ctx.verify(() -> {
if (expectSuccess) {
// assert that send operation succeeded
assertThat(ar.succeeded()).isTrue();
// Verify the command response that has been received
final DownstreamMessage<KafkaMessageContext> response = ar.result();
assertThat(response.getDeviceId()).isEqualTo(deviceId);
assertThat(response.getStatus()).isEqualTo(responseStatus);
assertThat(response.getPayload().toString()).isEqualTo(responsePayload);
} else {
// assert that send operation failed
assertThat(ar.succeeded()).isFalse();
assertThat(ar.cause()).isInstanceOf(ServiceInvocationException.class);
assertThat(((ServiceInvocationException) ar.cause()).getErrorCode()).isEqualTo(expectedStatusCode);
assertThat(ar.cause().getMessage()).isEqualTo(responsePayload);
}
});
ctx.completeNow();
mockConsumer.close();
commandSender.stop();
});
});
}
use of org.apache.kafka.clients.producer.Callback in project open-kilda by telstra.
the class KafkaProducerServiceTest method errorReporting.
@Test
public void errorReporting() throws Exception {
final ExecutionException error = new ExecutionException("Emulate kafka send error", new IOException());
Future promise = mock(Future.class);
expect(promise.get()).andThrow(error).anyTimes();
replay(promise);
expect(kafkaProducer.send(anyObject(), anyObject(Callback.class))).andAnswer(new IAnswer<Future<RecordMetadata>>() {
@Override
public Future<RecordMetadata> answer() {
Callback callback = (Callback) getCurrentArguments()[1];
callback.onCompletion(null, error);
return promise;
}
});
replay(kafkaProducer);
subject.sendMessageAndTrack(TOPIC, makePayload());
verify(kafkaProducer);
// This test does not do any assertions, because the only action is log message with error
// you can locate this message in test's output.
}
use of org.apache.kafka.clients.producer.Callback in project kafka by apache.
the class RecordCollectorTest method shouldRetryWhenTimeoutExceptionOccursOnSend.
@SuppressWarnings("unchecked")
@Test
public void shouldRetryWhenTimeoutExceptionOccursOnSend() throws Exception {
final AtomicInteger attempt = new AtomicInteger(0);
RecordCollectorImpl collector = new RecordCollectorImpl(new MockProducer(cluster, true, new DefaultPartitioner(), byteArraySerializer, byteArraySerializer) {
@Override
public synchronized Future<RecordMetadata> send(final ProducerRecord record, final Callback callback) {
if (attempt.getAndIncrement() == 0) {
throw new TimeoutException();
}
return super.send(record, callback);
}
}, "test");
collector.send("topic1", "3", "0", null, null, stringSerializer, stringSerializer, streamPartitioner);
final Long offset = collector.offsets().get(new TopicPartition("topic1", 0));
assertEquals(Long.valueOf(0L), offset);
}
Aggregations