use of org.apache.kafka.clients.producer.MockProducer in project hono by eclipse.
the class KafkaBasedNotificationSenderTest method testSendFailsWithTheExpectedError.
/**
* Verifies that the send method returns the underlying error wrapped in a {@link ServerErrorException}.
*
* @param ctx The vert.x test context.
*/
@Test
public void testSendFailsWithTheExpectedError(final VertxTestContext ctx) {
// GIVEN a sender sending a message
final RuntimeException expectedError = new RuntimeException("boom");
final MockProducer<String, JsonObject> mockProducer = new MockProducer<>(false, new StringSerializer(), new JsonObjectSerializer());
final KafkaBasedNotificationSender sender = newSender(mockProducer);
sender.publish(new TenantChangeNotification(CHANGE, TENANT_ID, CREATION_TIME, ENABLED)).onComplete(ctx.failing(t -> {
ctx.verify(() -> {
// THEN it fails with the expected error
assertThat(t).isInstanceOf(ServerErrorException.class);
assertThat(((ServerErrorException) t).getErrorCode()).isEqualTo(503);
assertThat(t.getCause()).isEqualTo(expectedError);
});
ctx.completeNow();
}));
// WHEN the send operation fails
mockProducer.errorNext(expectedError);
}
use of org.apache.kafka.clients.producer.MockProducer in project hono by eclipse.
the class AbstractKafkaBasedMessageSenderTest method testProducerIsClosedOnFatalError.
/**
* Verifies that the producer is closed when sending of a message fails with a fatal error.
*
* @param ctx The vert.x test context.
*/
@Test
public void testProducerIsClosedOnFatalError(final VertxTestContext ctx) {
final AuthorizationException expectedError = new AuthorizationException("go away");
// GIVEN a sender sending a message
final var mockProducer = KafkaClientUnitTestHelper.newMockProducer(false);
final var factory = newProducerFactory(mockProducer);
newSender(factory).sendAndWaitForOutcome("topic", "tenant", "device", null, Map.of(), NoopSpan.INSTANCE).onComplete(ctx.failing(t -> {
ctx.verify(() -> {
// THEN the producer is removed and closed
assertThat(factory.getProducer(PRODUCER_NAME).isEmpty()).isTrue();
assertThat(mockProducer.closed()).isTrue();
});
ctx.completeNow();
}));
// WHEN the send operation fails
mockProducer.errorNext(expectedError);
}
use of org.apache.kafka.clients.producer.MockProducer 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);
}
use of org.apache.kafka.clients.producer.MockProducer in project kafka by apache.
the class RecordCollectorTest method shouldThrowStreamsExceptionAfterMaxAttempts.
@SuppressWarnings("unchecked")
@Test(expected = StreamsException.class)
public void shouldThrowStreamsExceptionAfterMaxAttempts() throws Exception {
RecordCollector collector = new RecordCollectorImpl(new MockProducer(cluster, true, new DefaultPartitioner(), byteArraySerializer, byteArraySerializer) {
@Override
public synchronized Future<RecordMetadata> send(final ProducerRecord record, final Callback callback) {
throw new TimeoutException();
}
}, "test");
collector.send("topic1", "3", "0", null, null, stringSerializer, stringSerializer, streamPartitioner);
}
use of org.apache.kafka.clients.producer.MockProducer in project kafka by apache.
the class RecordCollectorTest method shouldThrowStreamsExceptionOnFlushIfASendFailed.
@SuppressWarnings("unchecked")
@Test(expected = StreamsException.class)
public void shouldThrowStreamsExceptionOnFlushIfASendFailed() throws Exception {
final RecordCollector collector = new RecordCollectorImpl(new MockProducer(cluster, true, new DefaultPartitioner(), byteArraySerializer, byteArraySerializer) {
@Override
public synchronized Future<RecordMetadata> send(final ProducerRecord record, final Callback callback) {
callback.onCompletion(null, new Exception());
return null;
}
}, "test");
collector.send("topic1", "3", "0", null, null, stringSerializer, stringSerializer, streamPartitioner);
collector.flush();
}
Aggregations