Search in sources :

Example 1 with MockProducer

use of org.apache.kafka.clients.producer.MockProducer in project kafka by apache.

the class RecordCollectorTest method shouldThrowStreamsExceptionOnSubsequentCallIfASendFails.

@SuppressWarnings("unchecked")
@Test(expected = StreamsException.class)
public void shouldThrowStreamsExceptionOnSubsequentCallIfASendFails() 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.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) StreamsException(org.apache.kafka.streams.errors.StreamsException) Test(org.junit.Test)

Example 2 with MockProducer

use of org.apache.kafka.clients.producer.MockProducer in project kafka by apache.

the class RecordCollectorTest method shouldThrowStreamsExceptionOnCloseIfASendFailed.

@SuppressWarnings("unchecked")
@Test(expected = StreamsException.class)
public void shouldThrowStreamsExceptionOnCloseIfASendFailed() 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.close();
}
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)

Example 3 with MockProducer

use of org.apache.kafka.clients.producer.MockProducer in project kafka by apache.

the class SinkNodeTest method shouldThrowStreamsExceptionOnKeyValueTypeSerializerMismatch.

@Test
@SuppressWarnings("unchecked")
public void shouldThrowStreamsExceptionOnKeyValueTypeSerializerMismatch() {
    // Given
    final Serializer anySerializer = Serdes.Bytes().serializer();
    final StateSerdes anyStateSerde = StateSerdes.withBuiltinTypes("anyName", Bytes.class, Bytes.class);
    final MockProcessorContext context = new MockProcessorContext(anyStateSerde, new RecordCollectorImpl(new MockProducer<byte[], byte[]>(true, anySerializer, anySerializer), null));
    context.setTime(0);
    final SinkNode sink = new SinkNode<>("anyNodeName", "any-output-topic", anySerializer, anySerializer, null);
    sink.init(context);
    final String keyOfDifferentTypeThanSerializer = "key with different type";
    final String valueOfDifferentTypeThanSerializer = "value with different type";
    // When/Then
    try {
        sink.process(keyOfDifferentTypeThanSerializer, valueOfDifferentTypeThanSerializer);
        fail("Should have thrown StreamsException");
    } catch (final StreamsException e) {
        assertThat(e.getCause(), instanceOf(ClassCastException.class));
    }
}
Also used : MockProducer(org.apache.kafka.clients.producer.MockProducer) StreamsException(org.apache.kafka.streams.errors.StreamsException) CoreMatchers.containsString(org.hamcrest.CoreMatchers.containsString) StateSerdes(org.apache.kafka.streams.state.StateSerdes) MockProcessorContext(org.apache.kafka.test.MockProcessorContext) Serializer(org.apache.kafka.common.serialization.Serializer) Test(org.junit.Test)

Example 4 with MockProducer

use of org.apache.kafka.clients.producer.MockProducer in project kafka by apache.

the class SinkNodeTest method shouldThrowStreamsExceptionOnInputRecordWithInvalidTimestamp.

@Test
@SuppressWarnings("unchecked")
public void shouldThrowStreamsExceptionOnInputRecordWithInvalidTimestamp() {
    // Given
    final Serializer anySerializer = Serdes.Bytes().serializer();
    final StateSerdes anyStateSerde = StateSerdes.withBuiltinTypes("anyName", Bytes.class, Bytes.class);
    final MockProcessorContext context = new MockProcessorContext(anyStateSerde, new RecordCollectorImpl(new MockProducer<byte[], byte[]>(true, anySerializer, anySerializer), null));
    final SinkNode sink = new SinkNode<>("anyNodeName", "any-output-topic", anySerializer, anySerializer, null);
    sink.init(context);
    final Bytes anyKey = new Bytes("any key".getBytes());
    final Bytes anyValue = new Bytes("any value".getBytes());
    // When/Then
    // ensures a negative timestamp is set for the record we send next
    context.setTime(-1);
    try {
        sink.process(anyKey, anyValue);
        fail("Should have thrown StreamsException");
    } catch (final StreamsException ignored) {
    }
}
Also used : Bytes(org.apache.kafka.common.utils.Bytes) MockProducer(org.apache.kafka.clients.producer.MockProducer) StreamsException(org.apache.kafka.streams.errors.StreamsException) StateSerdes(org.apache.kafka.streams.state.StateSerdes) MockProcessorContext(org.apache.kafka.test.MockProcessorContext) Serializer(org.apache.kafka.common.serialization.Serializer) Test(org.junit.Test)

Example 5 with MockProducer

use of org.apache.kafka.clients.producer.MockProducer in project kafka by apache.

the class SinkNodeTest method shouldHandleNullValuesWhenThrowingStreamsExceptionOnKeyValueTypeSerializerMismatch.

@Test
@SuppressWarnings("unchecked")
public void shouldHandleNullValuesWhenThrowingStreamsExceptionOnKeyValueTypeSerializerMismatch() {
    // Given
    final Serializer anySerializer = Serdes.Bytes().serializer();
    final StateSerdes anyStateSerde = StateSerdes.withBuiltinTypes("anyName", Bytes.class, Bytes.class);
    final MockProcessorContext context = new MockProcessorContext(anyStateSerde, new RecordCollectorImpl(new MockProducer<byte[], byte[]>(true, anySerializer, anySerializer), null));
    context.setTime(1);
    final SinkNode sink = new SinkNode<>("anyNodeName", "any-output-topic", anySerializer, anySerializer, null);
    sink.init(context);
    final String invalidKeyToTriggerSerializerMismatch = "";
    // When/Then
    try {
        sink.process(invalidKeyToTriggerSerializerMismatch, null);
        fail("Should have thrown StreamsException");
    } catch (final StreamsException e) {
        assertThat(e.getCause(), instanceOf(ClassCastException.class));
        assertThat(e.getMessage(), containsString("unknown because value is null"));
    }
}
Also used : MockProducer(org.apache.kafka.clients.producer.MockProducer) StreamsException(org.apache.kafka.streams.errors.StreamsException) CoreMatchers.containsString(org.hamcrest.CoreMatchers.containsString) StateSerdes(org.apache.kafka.streams.state.StateSerdes) MockProcessorContext(org.apache.kafka.test.MockProcessorContext) Serializer(org.apache.kafka.common.serialization.Serializer) 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