Search in sources :

Example 1 with ConsumerTask

use of io.smallrye.reactive.messaging.kafka.companion.ConsumerTask 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 2 with ConsumerTask

use of io.smallrye.reactive.messaging.kafka.companion.ConsumerTask in project smallrye-reactive-messaging by smallrye.

the class BatchTracingPropagationTest method testFromAppToKafka.

@SuppressWarnings("ConstantConditions")
@Test
public void testFromAppToKafka() {
    List<Context> contexts = new CopyOnWriteArrayList<>();
    ConsumerTask<String, Integer> consumed = companion.consumeIntegers().fromTopics(topic, m -> m.plug(until(10L, Duration.ofMinutes(1), null)).onItem().invoke(record -> {
        contexts.add(GlobalOpenTelemetry.getPropagators().getTextMapPropagator().extract(Context.current(), record.headers(), new HeaderExtractAdapter()));
    }));
    runApplication(getKafkaSinkConfigForMyAppGeneratingData(), MyAppGeneratingData.class);
    await().until(() -> consumed.getRecords().size() >= 10);
    List<Integer> values = new ArrayList<>();
    assertThat(consumed.getRecords()).allSatisfy(record -> {
        assertThat(record.value()).isNotNull();
        values.add(record.value());
    });
    assertThat(values).containsExactly(0, 1, 2, 3, 4, 5, 6, 7, 8, 9);
    assertThat(contexts).hasSize(10);
    assertThat(contexts).doesNotContainNull().doesNotHaveDuplicates();
    List<String> spanIds = contexts.stream().map(context -> Span.fromContextOrNull(context).getSpanContext().getSpanId()).collect(Collectors.toList());
    assertThat(spanIds).doesNotContainNull().doesNotHaveDuplicates().hasSize(10);
    List<String> traceIds = contexts.stream().map(context -> Span.fromContextOrNull(context).getSpanContext().getTraceId()).collect(Collectors.toList());
    assertThat(traceIds).doesNotContainNull().doesNotHaveDuplicates().hasSize(10);
    for (SpanData data : testExporter.getFinishedSpanItems()) {
        assertThat(data.getSpanId()).isIn(spanIds);
        assertThat(data.getSpanId()).isNotEqualTo(data.getParentSpanId());
        assertThat(data.getTraceId()).isIn(traceIds);
        assertThat(data.getKind()).isEqualByComparingTo(SpanKind.PRODUCER);
    }
}
Also used : Context(io.opentelemetry.context.Context) SpanContext(io.opentelemetry.api.trace.SpanContext) Sampler(io.opentelemetry.sdk.trace.samplers.Sampler) BeforeEach(org.junit.jupiter.api.BeforeEach) ProducerRecord(org.apache.kafka.clients.producer.ProducerRecord) Outgoing(org.eclipse.microprofile.reactive.messaging.Outgoing) KafkaCompanionTestBase(io.smallrye.reactive.messaging.kafka.base.KafkaCompanionTestBase) W3CTraceContextPropagator(io.opentelemetry.api.trace.propagation.W3CTraceContextPropagator) OpenTelemetrySdk(io.opentelemetry.sdk.OpenTelemetrySdk) Assertions.assertThat(org.assertj.core.api.Assertions.assertThat) Multi(io.smallrye.mutiny.Multi) ArrayList(java.util.ArrayList) AfterAll(org.junit.jupiter.api.AfterAll) RecordHeaders(org.apache.kafka.common.header.internals.RecordHeaders) ConsumerTask(io.smallrye.reactive.messaging.kafka.companion.ConsumerTask) StringDeserializer(org.apache.kafka.common.serialization.StringDeserializer) SimpleSpanProcessor(io.opentelemetry.sdk.trace.export.SimpleSpanProcessor) ContextPropagators(io.opentelemetry.context.propagation.ContextPropagators) Duration(java.time.Duration) IntegerSerializer(org.apache.kafka.common.serialization.IntegerSerializer) KafkaConnector(io.smallrye.reactive.messaging.kafka.KafkaConnector) Context(io.opentelemetry.context.Context) Awaitility.await(org.awaitility.Awaitility.await) Span(io.opentelemetry.api.trace.Span) SpanContext(io.opentelemetry.api.trace.SpanContext) Publisher(org.reactivestreams.Publisher) TracingMetadata(io.smallrye.reactive.messaging.TracingMetadata) RecordQualifiers.until(io.smallrye.reactive.messaging.kafka.companion.RecordQualifiers.until) KafkaMapBasedConfig(io.smallrye.reactive.messaging.kafka.base.KafkaMapBasedConfig) Collectors(java.util.stream.Collectors) SpanKind(io.opentelemetry.api.trace.SpanKind) StandardCharsets(java.nio.charset.StandardCharsets) SdkTracerProvider(io.opentelemetry.sdk.trace.SdkTracerProvider) GlobalOpenTelemetry(io.opentelemetry.api.GlobalOpenTelemetry) Test(org.junit.jupiter.api.Test) List(java.util.List) AfterEach(org.junit.jupiter.api.AfterEach) CompletionStage(java.util.concurrent.CompletionStage) InMemorySpanExporter(io.opentelemetry.sdk.testing.exporter.InMemorySpanExporter) KafkaRecord(io.smallrye.reactive.messaging.kafka.KafkaRecord) Header(org.apache.kafka.common.header.Header) KafkaRecordBatch(io.smallrye.reactive.messaging.kafka.KafkaRecordBatch) IntegerDeserializer(org.apache.kafka.common.serialization.IntegerDeserializer) ApplicationScoped(javax.enterprise.context.ApplicationScoped) SpanData(io.opentelemetry.sdk.trace.data.SpanData) CopyOnWriteArrayList(java.util.concurrent.CopyOnWriteArrayList) Incoming(org.eclipse.microprofile.reactive.messaging.Incoming) SpanProcessor(io.opentelemetry.sdk.trace.SpanProcessor) SpanData(io.opentelemetry.sdk.trace.data.SpanData) ArrayList(java.util.ArrayList) CopyOnWriteArrayList(java.util.concurrent.CopyOnWriteArrayList) CopyOnWriteArrayList(java.util.concurrent.CopyOnWriteArrayList) Test(org.junit.jupiter.api.Test)

Example 3 with ConsumerTask

use of io.smallrye.reactive.messaging.kafka.companion.ConsumerTask in project smallrye-reactive-messaging by smallrye.

the class TracingPropagationTest method testFromKafkaToAppToKafka.

@Test
public void testFromKafkaToAppToKafka() {
    List<Context> receivedContexts = new CopyOnWriteArrayList<>();
    String resultTopic = topic + "-result";
    String parentTopic = topic + "-parent";
    ConsumerTask<String, Integer> consumed = companion.consumeIntegers().fromTopics(resultTopic, m -> m.plug(until(10L, Duration.ofMinutes(1), null)).onItem().invoke(record -> {
        receivedContexts.add(GlobalOpenTelemetry.getPropagators().getTextMapPropagator().extract(Context.current(), record.headers(), new HeaderExtractAdapter()));
    }));
    MyAppProcessingData bean = runApplication(getKafkaSinkConfigForMyAppProcessingData(resultTopic, parentTopic), MyAppProcessingData.class);
    List<SpanContext> producedSpanContexts = new CopyOnWriteArrayList<>();
    companion.produceIntegers().usingGenerator(i -> new ProducerRecord<>(parentTopic, null, null, "a-key", i, createTracingSpan(producedSpanContexts, parentTopic)), 10);
    await().until(() -> consumed.getRecords().size() >= 10);
    List<Integer> values = new ArrayList<>();
    assertThat(consumed.getRecords()).allSatisfy(record -> {
        assertThat(record.value()).isNotNull();
        values.add(record.value());
    });
    assertThat(values).containsExactly(1, 2, 3, 4, 5, 6, 7, 8, 9, 10);
    List<String> producedTraceIds = producedSpanContexts.stream().map(SpanContext::getTraceId).collect(Collectors.toList());
    assertThat(producedTraceIds).hasSize(10);
    assertThat(receivedContexts).hasSize(10);
    assertThat(receivedContexts).doesNotContainNull().doesNotHaveDuplicates();
    List<String> receivedSpanIds = receivedContexts.stream().map(context -> Span.fromContextOrNull(context).getSpanContext().getSpanId()).collect(Collectors.toList());
    assertThat(receivedSpanIds).doesNotContainNull().doesNotHaveDuplicates().hasSize(10);
    List<String> receivedTraceIds = receivedContexts.stream().map(context -> Span.fromContextOrNull(context).getSpanContext().getTraceId()).collect(Collectors.toList());
    assertThat(receivedTraceIds).doesNotContainNull().doesNotHaveDuplicates().hasSize(10);
    assertThat(receivedTraceIds).containsExactlyInAnyOrderElementsOf(producedTraceIds);
    assertThat(bean.tracing()).hasSizeGreaterThanOrEqualTo(10);
    assertThat(bean.tracing()).doesNotContainNull().doesNotHaveDuplicates();
    List<String> spanIds = new ArrayList<>();
    for (TracingMetadata tracing : bean.tracing()) {
        Span span = Span.fromContext(tracing.getCurrentContext());
        spanIds.add(span.getSpanContext().getSpanId());
        assertThat(Span.fromContextOrNull(tracing.getPreviousContext())).isNotNull();
    }
    await().atMost(Duration.ofMinutes(2)).until(() -> testExporter.getFinishedSpanItems().size() >= 10);
    List<String> outgoingParentIds = new ArrayList<>();
    List<String> incomingParentIds = new ArrayList<>();
    for (SpanData data : testExporter.getFinishedSpanItems()) {
        if (data.getKind().equals(SpanKind.CONSUMER)) {
            incomingParentIds.add(data.getParentSpanId());
            assertThat(data.getAttributes().get(SemanticAttributes.MESSAGING_KAFKA_CONSUMER_GROUP)).isNotNull();
            assertThat(data.getAttributes().get(AttributeKey.stringKey("messaging.consumer_id"))).isNotNull();
            // Need to skip the spans created during @Incoming processing
            continue;
        }
        assertThat(data.getSpanId()).isIn(receivedSpanIds);
        assertThat(data.getSpanId()).isNotEqualTo(data.getParentSpanId());
        assertThat(data.getTraceId()).isIn(producedTraceIds);
        assertThat(data.getKind()).isEqualByComparingTo(SpanKind.PRODUCER);
        outgoingParentIds.add(data.getParentSpanId());
    }
    // Assert span created on Kafka record is the parent of consumer span we create
    assertThat(producedSpanContexts.stream().map(SpanContext::getSpanId)).containsExactlyElementsOf(incomingParentIds);
    // Assert consumer span is the parent of the producer span we received in Kafka
    assertThat(spanIds.stream()).containsExactlyElementsOf(outgoingParentIds);
}
Also used : Context(io.opentelemetry.context.Context) SpanContext(io.opentelemetry.api.trace.SpanContext) Sampler(io.opentelemetry.sdk.trace.samplers.Sampler) BeforeEach(org.junit.jupiter.api.BeforeEach) ProducerRecord(org.apache.kafka.clients.producer.ProducerRecord) Outgoing(org.eclipse.microprofile.reactive.messaging.Outgoing) KafkaCompanionTestBase(io.smallrye.reactive.messaging.kafka.base.KafkaCompanionTestBase) W3CTraceContextPropagator(io.opentelemetry.api.trace.propagation.W3CTraceContextPropagator) OpenTelemetrySdk(io.opentelemetry.sdk.OpenTelemetrySdk) Assertions.assertThat(org.assertj.core.api.Assertions.assertThat) SemanticAttributes(io.opentelemetry.semconv.trace.attributes.SemanticAttributes) Multi(io.smallrye.mutiny.Multi) MapBasedConfig(io.smallrye.reactive.messaging.test.common.config.MapBasedConfig) ArrayList(java.util.ArrayList) AfterAll(org.junit.jupiter.api.AfterAll) RecordHeaders(org.apache.kafka.common.header.internals.RecordHeaders) ConsumerTask(io.smallrye.reactive.messaging.kafka.companion.ConsumerTask) StringDeserializer(org.apache.kafka.common.serialization.StringDeserializer) SimpleSpanProcessor(io.opentelemetry.sdk.trace.export.SimpleSpanProcessor) ContextPropagators(io.opentelemetry.context.propagation.ContextPropagators) Duration(java.time.Duration) IntegerSerializer(org.apache.kafka.common.serialization.IntegerSerializer) KafkaConnector(io.smallrye.reactive.messaging.kafka.KafkaConnector) Context(io.opentelemetry.context.Context) Awaitility.await(org.awaitility.Awaitility.await) Span(io.opentelemetry.api.trace.Span) SpanContext(io.opentelemetry.api.trace.SpanContext) Publisher(org.reactivestreams.Publisher) TracingMetadata(io.smallrye.reactive.messaging.TracingMetadata) RecordQualifiers.until(io.smallrye.reactive.messaging.kafka.companion.RecordQualifiers.until) KafkaMapBasedConfig(io.smallrye.reactive.messaging.kafka.base.KafkaMapBasedConfig) Collectors(java.util.stream.Collectors) SpanKind(io.opentelemetry.api.trace.SpanKind) StandardCharsets(java.nio.charset.StandardCharsets) SdkTracerProvider(io.opentelemetry.sdk.trace.SdkTracerProvider) GlobalOpenTelemetry(io.opentelemetry.api.GlobalOpenTelemetry) Test(org.junit.jupiter.api.Test) List(java.util.List) Message(org.eclipse.microprofile.reactive.messaging.Message) AfterEach(org.junit.jupiter.api.AfterEach) CompletionStage(java.util.concurrent.CompletionStage) InMemorySpanExporter(io.opentelemetry.sdk.testing.exporter.InMemorySpanExporter) Header(org.apache.kafka.common.header.Header) AttributeKey(io.opentelemetry.api.common.AttributeKey) IntegerDeserializer(org.apache.kafka.common.serialization.IntegerDeserializer) ApplicationScoped(javax.enterprise.context.ApplicationScoped) SpanData(io.opentelemetry.sdk.trace.data.SpanData) CopyOnWriteArrayList(java.util.concurrent.CopyOnWriteArrayList) Incoming(org.eclipse.microprofile.reactive.messaging.Incoming) SpanProcessor(io.opentelemetry.sdk.trace.SpanProcessor) SpanContext(io.opentelemetry.api.trace.SpanContext) SpanData(io.opentelemetry.sdk.trace.data.SpanData) ArrayList(java.util.ArrayList) CopyOnWriteArrayList(java.util.concurrent.CopyOnWriteArrayList) Span(io.opentelemetry.api.trace.Span) TracingMetadata(io.smallrye.reactive.messaging.TracingMetadata) CopyOnWriteArrayList(java.util.concurrent.CopyOnWriteArrayList) Test(org.junit.jupiter.api.Test)

Example 4 with ConsumerTask

use of io.smallrye.reactive.messaging.kafka.companion.ConsumerTask in project smallrye-reactive-messaging by smallrye.

the class TracingPropagationTest method testFromAppToKafka.

@SuppressWarnings("ConstantConditions")
@Test
public void testFromAppToKafka() {
    List<Context> contexts = new CopyOnWriteArrayList<>();
    ConsumerTask<String, Integer> consumed = companion.consumeIntegers().fromTopics(topic, m -> m.plug(until(10L, Duration.ofMinutes(1), null)).onItem().invoke(record -> {
        contexts.add(GlobalOpenTelemetry.getPropagators().getTextMapPropagator().extract(Context.current(), record.headers(), new HeaderExtractAdapter()));
    }));
    runApplication(getKafkaSinkConfigForMyAppGeneratingData(), MyAppGeneratingData.class);
    await().until(() -> consumed.getRecords().size() >= 10);
    List<Integer> values = new ArrayList<>();
    assertThat(consumed.getRecords()).allSatisfy(record -> {
        assertThat(record.value()).isNotNull();
        values.add(record.value());
    });
    assertThat(values).containsExactly(0, 1, 2, 3, 4, 5, 6, 7, 8, 9);
    assertThat(contexts).hasSize(10);
    assertThat(contexts).doesNotContainNull().doesNotHaveDuplicates();
    List<String> spanIds = contexts.stream().map(context -> Span.fromContextOrNull(context).getSpanContext().getSpanId()).collect(Collectors.toList());
    assertThat(spanIds).doesNotContainNull().doesNotHaveDuplicates().hasSize(10);
    List<String> traceIds = contexts.stream().map(context -> Span.fromContextOrNull(context).getSpanContext().getTraceId()).collect(Collectors.toList());
    assertThat(traceIds).doesNotContainNull().doesNotHaveDuplicates().hasSize(10);
    for (SpanData data : testExporter.getFinishedSpanItems()) {
        assertThat(data.getSpanId()).isIn(spanIds);
        assertThat(data.getSpanId()).isNotEqualTo(data.getParentSpanId());
        assertThat(data.getTraceId()).isIn(traceIds);
        assertThat(data.getKind()).isEqualByComparingTo(SpanKind.PRODUCER);
    }
}
Also used : Context(io.opentelemetry.context.Context) SpanContext(io.opentelemetry.api.trace.SpanContext) Sampler(io.opentelemetry.sdk.trace.samplers.Sampler) BeforeEach(org.junit.jupiter.api.BeforeEach) ProducerRecord(org.apache.kafka.clients.producer.ProducerRecord) Outgoing(org.eclipse.microprofile.reactive.messaging.Outgoing) KafkaCompanionTestBase(io.smallrye.reactive.messaging.kafka.base.KafkaCompanionTestBase) W3CTraceContextPropagator(io.opentelemetry.api.trace.propagation.W3CTraceContextPropagator) OpenTelemetrySdk(io.opentelemetry.sdk.OpenTelemetrySdk) Assertions.assertThat(org.assertj.core.api.Assertions.assertThat) SemanticAttributes(io.opentelemetry.semconv.trace.attributes.SemanticAttributes) Multi(io.smallrye.mutiny.Multi) MapBasedConfig(io.smallrye.reactive.messaging.test.common.config.MapBasedConfig) ArrayList(java.util.ArrayList) AfterAll(org.junit.jupiter.api.AfterAll) RecordHeaders(org.apache.kafka.common.header.internals.RecordHeaders) ConsumerTask(io.smallrye.reactive.messaging.kafka.companion.ConsumerTask) StringDeserializer(org.apache.kafka.common.serialization.StringDeserializer) SimpleSpanProcessor(io.opentelemetry.sdk.trace.export.SimpleSpanProcessor) ContextPropagators(io.opentelemetry.context.propagation.ContextPropagators) Duration(java.time.Duration) IntegerSerializer(org.apache.kafka.common.serialization.IntegerSerializer) KafkaConnector(io.smallrye.reactive.messaging.kafka.KafkaConnector) Context(io.opentelemetry.context.Context) Awaitility.await(org.awaitility.Awaitility.await) Span(io.opentelemetry.api.trace.Span) SpanContext(io.opentelemetry.api.trace.SpanContext) Publisher(org.reactivestreams.Publisher) TracingMetadata(io.smallrye.reactive.messaging.TracingMetadata) RecordQualifiers.until(io.smallrye.reactive.messaging.kafka.companion.RecordQualifiers.until) KafkaMapBasedConfig(io.smallrye.reactive.messaging.kafka.base.KafkaMapBasedConfig) Collectors(java.util.stream.Collectors) SpanKind(io.opentelemetry.api.trace.SpanKind) StandardCharsets(java.nio.charset.StandardCharsets) SdkTracerProvider(io.opentelemetry.sdk.trace.SdkTracerProvider) GlobalOpenTelemetry(io.opentelemetry.api.GlobalOpenTelemetry) Test(org.junit.jupiter.api.Test) List(java.util.List) Message(org.eclipse.microprofile.reactive.messaging.Message) AfterEach(org.junit.jupiter.api.AfterEach) CompletionStage(java.util.concurrent.CompletionStage) InMemorySpanExporter(io.opentelemetry.sdk.testing.exporter.InMemorySpanExporter) Header(org.apache.kafka.common.header.Header) AttributeKey(io.opentelemetry.api.common.AttributeKey) IntegerDeserializer(org.apache.kafka.common.serialization.IntegerDeserializer) ApplicationScoped(javax.enterprise.context.ApplicationScoped) SpanData(io.opentelemetry.sdk.trace.data.SpanData) CopyOnWriteArrayList(java.util.concurrent.CopyOnWriteArrayList) Incoming(org.eclipse.microprofile.reactive.messaging.Incoming) SpanProcessor(io.opentelemetry.sdk.trace.SpanProcessor) SpanData(io.opentelemetry.sdk.trace.data.SpanData) ArrayList(java.util.ArrayList) CopyOnWriteArrayList(java.util.concurrent.CopyOnWriteArrayList) CopyOnWriteArrayList(java.util.concurrent.CopyOnWriteArrayList) Test(org.junit.jupiter.api.Test)

Example 5 with ConsumerTask

use of io.smallrye.reactive.messaging.kafka.companion.ConsumerTask in project smallrye-reactive-messaging by smallrye.

the class KafkaSinkWithLegacyMetadataTest 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);
    // 3 and 5 are ignored.
    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) 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)

Aggregations

KafkaCompanionTestBase (io.smallrye.reactive.messaging.kafka.base.KafkaCompanionTestBase)6 KafkaMapBasedConfig (io.smallrye.reactive.messaging.kafka.base.KafkaMapBasedConfig)6 ConsumerTask (io.smallrye.reactive.messaging.kafka.companion.ConsumerTask)6 Duration (java.time.Duration)6 List (java.util.List)6 CopyOnWriteArrayList (java.util.concurrent.CopyOnWriteArrayList)6 ApplicationScoped (javax.enterprise.context.ApplicationScoped)6 IntegerSerializer (org.apache.kafka.common.serialization.IntegerSerializer)6 Assertions.assertThat (org.assertj.core.api.Assertions.assertThat)6 Awaitility.await (org.awaitility.Awaitility.await)6 Incoming (org.eclipse.microprofile.reactive.messaging.Incoming)6 Outgoing (org.eclipse.microprofile.reactive.messaging.Outgoing)6 Test (org.junit.jupiter.api.Test)6 Multi (io.smallrye.mutiny.Multi)5 AfterEach (org.junit.jupiter.api.AfterEach)5 Publisher (org.reactivestreams.Publisher)5 RecordQualifiers.until (io.smallrye.reactive.messaging.kafka.companion.RecordQualifiers.until)4 MapBasedConfig (io.smallrye.reactive.messaging.test.common.config.MapBasedConfig)4 ProducerRecord (org.apache.kafka.clients.producer.ProducerRecord)4 Header (org.apache.kafka.common.header.Header)4