Search in sources :

Example 1 with KafkaSink

use of io.smallrye.reactive.messaging.kafka.impl.KafkaSink in project smallrye-reactive-messaging by smallrye.

the class KafkaSinkTest method testSinkUsingIntegerAndChannelName.

@Test
@SuppressWarnings("unchecked")
public void testSinkUsingIntegerAndChannelName() {
    ConsumerTask<String, Integer> consumed = companion.consumeIntegers().fromTopics(topic, 10, Duration.ofSeconds(10));
    MapBasedConfig config = getBaseConfig().with("channel-name", topic).with("value.serializer", IntegerSerializer.class.getName()).with("partition", 0);
    KafkaConnectorOutgoingConfiguration oc = new KafkaConnectorOutgoingConfiguration(config);
    sink = new KafkaSink(oc, CountKafkaCdiEvents.noCdiEvents, UnsatisfiedInstance.instance());
    Subscriber<? extends Message<?>> subscriber = sink.getSink().build();
    Multi.createFrom().range(0, 10).map(Message::of).subscribe((Subscriber<? super Message<Integer>>) subscriber);
    assertThat(consumed.awaitCompletion(Duration.ofMinutes(1)).count()).isEqualTo(10);
}
Also used : KafkaSink(io.smallrye.reactive.messaging.kafka.impl.KafkaSink) MapBasedConfig(io.smallrye.reactive.messaging.test.common.config.MapBasedConfig) KafkaMapBasedConfig(io.smallrye.reactive.messaging.kafka.base.KafkaMapBasedConfig) Test(org.junit.jupiter.api.Test)

Example 2 with KafkaSink

use of io.smallrye.reactive.messaging.kafka.impl.KafkaSink in project smallrye-reactive-messaging by smallrye.

the class KafkaSinkTest method testInvalidPayloadType.

@SuppressWarnings({ "rawtypes", "unchecked" })
@Test
public void testInvalidPayloadType() {
    ConsumerTask<String, Integer> consumed = companion.consumeIntegers().fromTopics(topic, 4, Duration.ofSeconds(10));
    MapBasedConfig config = getBaseConfig().with("topic", topic).with("value.serializer", IntegerSerializer.class.getName()).with("partition", 0).with("max-inflight-messages", 1L).with("channel-name", "my-channel").with("retries", // disable retry.
    0L);
    KafkaConnectorOutgoingConfiguration oc = new KafkaConnectorOutgoingConfiguration(config);
    CountKafkaCdiEvents testCdiEvents = new CountKafkaCdiEvents();
    sink = new KafkaSink(oc, testCdiEvents, UnsatisfiedInstance.instance());
    await().until(() -> {
        HealthReport.HealthReportBuilder builder = HealthReport.builder();
        sink.isReady(builder);
        return builder.build().isOk();
    });
    List<Object> acked = new CopyOnWriteArrayList<>();
    List<Object> nacked = new CopyOnWriteArrayList<>();
    Subscriber subscriber = sink.getSink().build();
    Multi.createFrom().range(0, 6).map(i -> {
        if (i == 3 || i == 5) {
            return Integer.toString(i);
        }
        return i;
    }).map(i -> Message.of(i, () -> {
        acked.add(i);
        return CompletableFuture.completedFuture(null);
    }, t -> {
        nacked.add(i);
        return CompletableFuture.completedFuture(null);
    })).subscribe(subscriber);
    assertThat(consumed.awaitCompletion(Duration.ofMinutes(1)).count()).isEqualTo(4);
    await().until(() -> nacked.size() >= 2);
    assertThat(acked).containsExactly(0, 1, 2, 4);
    assertThat(nacked).contains("3", "5");
    assertThat(testCdiEvents.firedConsumerEvents.sum()).isEqualTo(0);
    assertThat(testCdiEvents.firedProducerEvents.sum()).isEqualTo(1);
}
Also used : KafkaSink(io.smallrye.reactive.messaging.kafka.impl.KafkaSink) Outgoing(org.eclipse.microprofile.reactive.messaging.Outgoing) KafkaCompanionTestBase(io.smallrye.reactive.messaging.kafka.base.KafkaCompanionTestBase) Assertions.assertThat(org.assertj.core.api.Assertions.assertThat) HealthReport(io.smallrye.reactive.messaging.health.HealthReport) CompletableFuture(java.util.concurrent.CompletableFuture) Multi(io.smallrye.mutiny.Multi) MapBasedConfig(io.smallrye.reactive.messaging.test.common.config.MapBasedConfig) Uni(io.smallrye.mutiny.Uni) ConsumerTask(io.smallrye.reactive.messaging.kafka.companion.ConsumerTask) Assertions.assertThatThrownBy(org.assertj.core.api.Assertions.assertThatThrownBy) Duration(java.time.Duration) IntegerSerializer(org.apache.kafka.common.serialization.IntegerSerializer) StringSerializer(org.apache.kafka.common.serialization.StringSerializer) Subscriber(org.reactivestreams.Subscriber) Awaitility.await(org.awaitility.Awaitility.await) Publisher(org.reactivestreams.Publisher) KafkaMapBasedConfig(io.smallrye.reactive.messaging.kafka.base.KafkaMapBasedConfig) UnsatisfiedInstance(io.smallrye.reactive.messaging.kafka.base.UnsatisfiedInstance) Test(org.junit.jupiter.api.Test) List(java.util.List) Message(org.eclipse.microprofile.reactive.messaging.Message) AfterEach(org.junit.jupiter.api.AfterEach) OutgoingKafkaRecordMetadata(io.smallrye.reactive.messaging.kafka.api.OutgoingKafkaRecordMetadata) KafkaCompanion(io.smallrye.reactive.messaging.kafka.companion.KafkaCompanion) ConsumerRecord(org.apache.kafka.clients.consumer.ConsumerRecord) ApplicationScoped(javax.enterprise.context.ApplicationScoped) CopyOnWriteArrayList(java.util.concurrent.CopyOnWriteArrayList) Incoming(org.eclipse.microprofile.reactive.messaging.Incoming) IntegerSerializer(org.apache.kafka.common.serialization.IntegerSerializer) HealthReport(io.smallrye.reactive.messaging.health.HealthReport) Subscriber(org.reactivestreams.Subscriber) KafkaSink(io.smallrye.reactive.messaging.kafka.impl.KafkaSink) MapBasedConfig(io.smallrye.reactive.messaging.test.common.config.MapBasedConfig) KafkaMapBasedConfig(io.smallrye.reactive.messaging.kafka.base.KafkaMapBasedConfig) CopyOnWriteArrayList(java.util.concurrent.CopyOnWriteArrayList) Test(org.junit.jupiter.api.Test)

Example 3 with KafkaSink

use of io.smallrye.reactive.messaging.kafka.impl.KafkaSink in project smallrye-reactive-messaging by smallrye.

the class KafkaSinkWithLegacyMetadataTest method testInvalidTypeWithDefaultInflightMessages.

@SuppressWarnings({ "rawtypes", "unchecked" })
@Test
public void testInvalidTypeWithDefaultInflightMessages() {
    ConsumerTask<String, Integer> consumed = companion.consumeIntegers().fromTopics(topic, 10, Duration.ofSeconds(10));
    MapBasedConfig config = getBaseConfig().with("topic", topic).with("value.serializer", IntegerSerializer.class.getName()).with("partition", 0).with("retries", 0L).with("channel-name", "testInvalidTypeWithDefaultInflightMessages");
    KafkaConnectorOutgoingConfiguration oc = new KafkaConnectorOutgoingConfiguration(config);
    sink = new KafkaSink(oc, CountKafkaCdiEvents.noCdiEvents, UnsatisfiedInstance.instance());
    Subscriber subscriber = sink.getSink().build();
    Multi.createFrom().range(0, 5).map(i -> {
        if (i == 3 || i == 5) {
            return Integer.toString(i);
        }
        return i;
    }).map(Message::of).subscribe(subscriber);
    await().until(() -> consumed.count() >= 3);
    // Default inflight is 5
    // 1, 2, 3, 4, 5 are sent at the same time.
    // As 3 fails, the stream is stopped, but, 1, 2, and 4 are already sent and potentially 6
    assertThat(consumed.count()).isGreaterThanOrEqualTo(3);
}
Also used : Subscriber(org.reactivestreams.Subscriber) KafkaSink(io.smallrye.reactive.messaging.kafka.impl.KafkaSink) MapBasedConfig(io.smallrye.reactive.messaging.test.common.config.MapBasedConfig) KafkaMapBasedConfig(io.smallrye.reactive.messaging.kafka.base.KafkaMapBasedConfig) Test(org.junit.jupiter.api.Test)

Example 4 with KafkaSink

use of io.smallrye.reactive.messaging.kafka.impl.KafkaSink in project smallrye-reactive-messaging by smallrye.

the class KafkaSinkWithCloudEventsTest method testSendingStructuredCloudEventsWithConfiguredTypeAndSourceAndNoCloudEventMetadata.

@SuppressWarnings({ "unchecked", "rawtypes" })
@Test
public void testSendingStructuredCloudEventsWithConfiguredTypeAndSourceAndNoCloudEventMetadata() {
    KafkaMapBasedConfig config = newCommonConfig();
    config.put("topic", topic);
    config.put("value.serializer", StringSerializer.class.getName());
    config.put("channel-name", topic);
    config.put("cloud-events-mode", "structured");
    config.put("cloud-events-type", "my type");
    config.put("cloud-events-source", "http://acme.org");
    KafkaConnectorOutgoingConfiguration oc = new KafkaConnectorOutgoingConfiguration(config);
    sink = new KafkaSink(oc, CountKafkaCdiEvents.noCdiEvents, UnsatisfiedInstance.instance());
    ConsumerTask<String, String> records = companion.consumeStrings().fromTopics(topic);
    Message<?> message = Message.of("hello!");
    Multi.createFrom().<Message<?>>item(message).subscribe().withSubscriber((Subscriber) sink.getSink().build());
    await().until(() -> records.getRecords().size() == 1);
    ConsumerRecord<String, String> record = records.getRecords().get(0);
    assertThat(record.topic()).isEqualTo(topic);
    assertThat(record.key()).isNull();
    assertThat(record.headers()).contains(new RecordHeader("content-type", "application/cloudevents+json; charset=UTF-8".getBytes()));
    JsonObject json = new JsonObject(record.value());
    assertThat(json.getString("specversion")).isEqualTo("1.0");
    assertThat(json.getString("type")).isEqualTo("my type");
    assertThat(json.getString("source")).isEqualTo("http://acme.org");
    assertThat(json.getString("id")).isNotNull();
    assertThat(json.getString("data")).isEqualTo("hello!");
}
Also used : Message(org.eclipse.microprofile.reactive.messaging.Message) KafkaSink(io.smallrye.reactive.messaging.kafka.impl.KafkaSink) JsonObject(io.vertx.core.json.JsonObject) KafkaConnectorOutgoingConfiguration(io.smallrye.reactive.messaging.kafka.KafkaConnectorOutgoingConfiguration) StringSerializer(org.apache.kafka.common.serialization.StringSerializer) RecordHeader(org.apache.kafka.common.header.internals.RecordHeader) KafkaMapBasedConfig(io.smallrye.reactive.messaging.kafka.base.KafkaMapBasedConfig) Test(org.junit.jupiter.api.Test)

Example 5 with KafkaSink

use of io.smallrye.reactive.messaging.kafka.impl.KafkaSink in project smallrye-reactive-messaging by smallrye.

the class KafkaSinkWithCloudEventsTest method testSendingBinaryCloudEventsWithConfiguredTypeAndSource.

@SuppressWarnings({ "unchecked", "rawtypes" })
@Test
public void testSendingBinaryCloudEventsWithConfiguredTypeAndSource() {
    KafkaMapBasedConfig config = newCommonConfig();
    config.put("topic", topic);
    config.put("value.serializer", StringSerializer.class.getName());
    config.put("channel-name", topic);
    config.put("key", "my-key");
    config.put("cloud-events-type", "my type");
    config.put("cloud-events-source", "http://acme.org");
    KafkaConnectorOutgoingConfiguration oc = new KafkaConnectorOutgoingConfiguration(config);
    sink = new KafkaSink(oc, CountKafkaCdiEvents.noCdiEvents, UnsatisfiedInstance.instance());
    ConsumerTask<String, String> records = companion.consumeStrings().fromTopics(topic);
    Message<?> message = Message.of("hello!").addMetadata(OutgoingCloudEventMetadata.builder().withId("some id").build());
    Multi.createFrom().<Message<?>>item(message).subscribe().withSubscriber((Subscriber) sink.getSink().build());
    await().until(() -> records.getRecords().size() == 1);
    ConsumerRecord<String, String> record = records.getRecords().get(0);
    assertThat(record.topic()).isEqualTo(topic);
    assertThat(record.key()).isEqualTo("my-key");
    assertThat(record.headers()).contains(new RecordHeader("ce_specversion", "1.0".getBytes()), new RecordHeader("ce_type", "my type".getBytes()), new RecordHeader("ce_source", "http://acme.org".getBytes()), new RecordHeader("ce_id", "some id".getBytes()));
    assertThat(record.value()).isEqualTo("hello!");
}
Also used : Message(org.eclipse.microprofile.reactive.messaging.Message) KafkaSink(io.smallrye.reactive.messaging.kafka.impl.KafkaSink) KafkaConnectorOutgoingConfiguration(io.smallrye.reactive.messaging.kafka.KafkaConnectorOutgoingConfiguration) StringSerializer(org.apache.kafka.common.serialization.StringSerializer) RecordHeader(org.apache.kafka.common.header.internals.RecordHeader) KafkaMapBasedConfig(io.smallrye.reactive.messaging.kafka.base.KafkaMapBasedConfig) Test(org.junit.jupiter.api.Test)

Aggregations

KafkaSink (io.smallrye.reactive.messaging.kafka.impl.KafkaSink)34 Test (org.junit.jupiter.api.Test)32 KafkaMapBasedConfig (io.smallrye.reactive.messaging.kafka.base.KafkaMapBasedConfig)27 StringSerializer (org.apache.kafka.common.serialization.StringSerializer)20 KafkaConnectorOutgoingConfiguration (io.smallrye.reactive.messaging.kafka.KafkaConnectorOutgoingConfiguration)18 Message (org.eclipse.microprofile.reactive.messaging.Message)18 MapBasedConfig (io.smallrye.reactive.messaging.test.common.config.MapBasedConfig)16 RecordHeader (org.apache.kafka.common.header.internals.RecordHeader)13 JsonObject (io.vertx.core.json.JsonObject)9 HealthReport (io.smallrye.reactive.messaging.health.HealthReport)4 IntegerSerializer (org.apache.kafka.common.serialization.IntegerSerializer)4 Subscriber (org.reactivestreams.Subscriber)4 Multi (io.smallrye.mutiny.Multi)2 Uni (io.smallrye.mutiny.Uni)2 KafkaCompanionTestBase (io.smallrye.reactive.messaging.kafka.base.KafkaCompanionTestBase)2 UnsatisfiedInstance (io.smallrye.reactive.messaging.kafka.base.UnsatisfiedInstance)2 ConsumerTask (io.smallrye.reactive.messaging.kafka.companion.ConsumerTask)2 KafkaCompanion (io.smallrye.reactive.messaging.kafka.companion.KafkaCompanion)2 JsonObjectSerializer (io.vertx.kafka.client.serialization.JsonObjectSerializer)2 Duration (java.time.Duration)2