Search in sources :

Example 21 with MockProducer

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);
}
Also used : Assertions.assertThrows(org.junit.jupiter.api.Assertions.assertThrows) AbstractNotification(org.eclipse.hono.notification.AbstractNotification) VertxTestContext(io.vertx.junit5.VertxTestContext) BeforeEach(org.junit.jupiter.api.BeforeEach) LifecycleChange(org.eclipse.hono.notification.deviceregistry.LifecycleChange) ProducerRecord(org.apache.kafka.clients.producer.ProducerRecord) JsonObjectSerializer(io.vertx.kafka.client.serialization.JsonObjectSerializer) DeviceChangeNotification(org.eclipse.hono.notification.deviceregistry.DeviceChangeNotification) ExtendWith(org.junit.jupiter.api.extension.ExtendWith) Map(java.util.Map) StringSerializer(org.apache.kafka.common.serialization.StringSerializer) JsonObject(io.vertx.core.json.JsonObject) KafkaClientUnitTestHelper(org.eclipse.hono.kafka.test.KafkaClientUnitTestHelper) AllDevicesOfTenantDeletedNotification(org.eclipse.hono.notification.deviceregistry.AllDevicesOfTenantDeletedNotification) CredentialsChangeNotification(org.eclipse.hono.notification.deviceregistry.CredentialsChangeNotification) TenantChangeNotification(org.eclipse.hono.notification.deviceregistry.TenantChangeNotification) NotificationConstants(org.eclipse.hono.notification.NotificationConstants) CachingKafkaProducerFactory(org.eclipse.hono.client.kafka.producer.CachingKafkaProducerFactory) Vertx(io.vertx.core.Vertx) ServerErrorException(org.eclipse.hono.client.ServerErrorException) Truth.assertThat(com.google.common.truth.Truth.assertThat) Instant(java.time.Instant) VertxExtension(io.vertx.junit5.VertxExtension) Test(org.junit.jupiter.api.Test) KafkaProducerFactory(org.eclipse.hono.client.kafka.producer.KafkaProducerFactory) MockProducer(org.apache.kafka.clients.producer.MockProducer) Mockito.mock(org.mockito.Mockito.mock) MockProducer(org.apache.kafka.clients.producer.MockProducer) JsonObjectSerializer(io.vertx.kafka.client.serialization.JsonObjectSerializer) JsonObject(io.vertx.core.json.JsonObject) TenantChangeNotification(org.eclipse.hono.notification.deviceregistry.TenantChangeNotification) ServerErrorException(org.eclipse.hono.client.ServerErrorException) StringSerializer(org.apache.kafka.common.serialization.StringSerializer) Test(org.junit.jupiter.api.Test)

Example 22 with MockProducer

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);
}
Also used : VertxTestContext(io.vertx.junit5.VertxTestContext) BeforeEach(org.junit.jupiter.api.BeforeEach) Tracer(io.opentracing.Tracer) AuthorizationException(org.apache.kafka.common.errors.AuthorizationException) Headers(org.apache.kafka.common.header.Headers) NoopTracerFactory(io.opentracing.noop.NoopTracerFactory) Vertx(io.vertx.core.Vertx) ServerErrorException(org.eclipse.hono.client.ServerErrorException) HashMap(java.util.HashMap) Truth.assertThat(com.google.common.truth.Truth.assertThat) ServiceInvocationException(org.eclipse.hono.client.ServiceInvocationException) MessageHelper(org.eclipse.hono.util.MessageHelper) VertxExtension(io.vertx.junit5.VertxExtension) Test(org.junit.jupiter.api.Test) ExtendWith(org.junit.jupiter.api.extension.ExtendWith) Buffer(io.vertx.core.buffer.Buffer) Map(java.util.Map) NoopSpan(io.opentracing.noop.NoopSpan) MockProducer(org.apache.kafka.clients.producer.MockProducer) KafkaClientUnitTestHelper(org.eclipse.hono.kafka.test.KafkaClientUnitTestHelper) Mockito.mock(org.mockito.Mockito.mock) AuthorizationException(org.apache.kafka.common.errors.AuthorizationException) Test(org.junit.jupiter.api.Test)

Example 23 with MockProducer

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);
}
Also used : MockProducer(org.apache.kafka.clients.producer.MockProducer) Callback(org.apache.kafka.clients.producer.Callback) DefaultPartitioner(org.apache.kafka.clients.producer.internals.DefaultPartitioner) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) TopicPartition(org.apache.kafka.common.TopicPartition) ProducerRecord(org.apache.kafka.clients.producer.ProducerRecord) Future(java.util.concurrent.Future) TimeoutException(org.apache.kafka.common.errors.TimeoutException) Test(org.junit.Test)

Example 24 with MockProducer

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);
}
Also used : MockProducer(org.apache.kafka.clients.producer.MockProducer) Callback(org.apache.kafka.clients.producer.Callback) DefaultPartitioner(org.apache.kafka.clients.producer.internals.DefaultPartitioner) ProducerRecord(org.apache.kafka.clients.producer.ProducerRecord) Future(java.util.concurrent.Future) TimeoutException(org.apache.kafka.common.errors.TimeoutException) Test(org.junit.Test)

Example 25 with MockProducer

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();
}
Also used : MockProducer(org.apache.kafka.clients.producer.MockProducer) Callback(org.apache.kafka.clients.producer.Callback) DefaultPartitioner(org.apache.kafka.clients.producer.internals.DefaultPartitioner) ProducerRecord(org.apache.kafka.clients.producer.ProducerRecord) Future(java.util.concurrent.Future) TimeoutException(org.apache.kafka.common.errors.TimeoutException) StreamsException(org.apache.kafka.streams.errors.StreamsException) Test(org.junit.Test)

Aggregations

MockProducer (org.apache.kafka.clients.producer.MockProducer)32 Test (org.junit.Test)25 DefaultPartitioner (org.apache.kafka.clients.producer.internals.DefaultPartitioner)17 ProducerRecord (org.apache.kafka.clients.producer.ProducerRecord)14 StreamsException (org.apache.kafka.streams.errors.StreamsException)14 Callback (org.apache.kafka.clients.producer.Callback)13 Future (java.util.concurrent.Future)12 KafkaException (org.apache.kafka.common.KafkaException)10 Map (java.util.Map)8 HashMap (java.util.HashMap)7 Truth.assertThat (com.google.common.truth.Truth.assertThat)6 Vertx (io.vertx.core.Vertx)5 Buffer (io.vertx.core.buffer.Buffer)5 VertxExtension (io.vertx.junit5.VertxExtension)5 VertxTestContext (io.vertx.junit5.VertxTestContext)5 TimeoutException (org.apache.kafka.common.errors.TimeoutException)5 KafkaClientUnitTestHelper (org.eclipse.hono.kafka.test.KafkaClientUnitTestHelper)5 BeforeEach (org.junit.jupiter.api.BeforeEach)5 Test (org.junit.jupiter.api.Test)5 ExtendWith (org.junit.jupiter.api.extension.ExtendWith)5