Search in sources :

Example 6 with ConsumerTask

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

the class HeadersPropagationTest method testFromKafkaToAppToKafka.

@Test
void testFromKafkaToAppToKafka() {
    final List<Headers> receivedContexts = new CopyOnWriteArrayList<>();
    ConsumerTask<String, Integer> consumed = companion.consumeIntegers().fromTopics("result-topic", m -> m.plug(until(10L, Duration.ofMinutes(1), null)).onItem().invoke(record -> receivedContexts.add(record.headers())));
    runApplication(getKafkaSinkConfigForMyAppProcessingData(), MyApp.class);
    final Headers producedheaders = new RecordHeaders();
    producedheaders.add(new RecordHeader(FIRST_HEADER_TO_KEEP_KEY, FIRST_HEADER_TO_KEEP_VALUE.getBytes()));
    producedheaders.add(new RecordHeader(SECOND_HEADER_TO_KEEP_KEY, SECOND_HEADER_TO_KEEP_VALUE.getBytes()));
    producedheaders.add(new RecordHeader(THIRD_HEADER_TO_FILTER_KEY, THIRD_HEADER_TO_FILTER_VALUE.getBytes()));
    companion.produceIntegers().usingGenerator(i -> new ProducerRecord<>("parent-topic", null, null, "a-key", i, producedheaders), 10);
    await().atMost(Duration.ofMinutes(5)).until(() -> consumed.count() >= 10);
    assertThat(consumed).extracting(ConsumerRecord::value).containsExactly(1, 2, 3, 4, 5, 6, 7, 8, 9, 10);
    assertThat(receivedContexts).hasSize(10).doesNotContainNull();
    final long headerCount = receivedContexts.stream().filter(headers -> {
        return containsHeaderWithValue(headers, FIRST_HEADER_TO_KEEP_KEY, FIRST_HEADER_TO_KEEP_VALUE) && containsHeaderWithValue(headers, SECOND_HEADER_TO_KEEP_KEY, SECOND_HEADER_TO_KEEP_VALUE) && !containsHeaderWithValue(headers, THIRD_HEADER_TO_FILTER_KEY, THIRD_HEADER_TO_FILTER_VALUE);
    }).count();
    assertEquals(10, headerCount);
}
Also used : Awaitility.await(org.awaitility.Awaitility.await) ProducerRecord(org.apache.kafka.clients.producer.ProducerRecord) Iterator(java.util.Iterator) Outgoing(org.eclipse.microprofile.reactive.messaging.Outgoing) KafkaCompanionTestBase(io.smallrye.reactive.messaging.kafka.base.KafkaCompanionTestBase) Assertions.assertThat(org.assertj.core.api.Assertions.assertThat) Headers(org.apache.kafka.common.header.Headers) Assert.assertEquals(org.testng.Assert.assertEquals) RecordQualifiers.until(io.smallrye.reactive.messaging.kafka.companion.RecordQualifiers.until) KafkaMapBasedConfig(io.smallrye.reactive.messaging.kafka.base.KafkaMapBasedConfig) RecordHeader(org.apache.kafka.common.header.internals.RecordHeader) Test(org.junit.jupiter.api.Test) List(java.util.List) RecordHeaders(org.apache.kafka.common.header.internals.RecordHeaders) ConsumerTask(io.smallrye.reactive.messaging.kafka.companion.ConsumerTask) StringDeserializer(org.apache.kafka.common.serialization.StringDeserializer) Header(org.apache.kafka.common.header.Header) ConsumerRecord(org.apache.kafka.clients.consumer.ConsumerRecord) Duration(java.time.Duration) IntegerSerializer(org.apache.kafka.common.serialization.IntegerSerializer) IntegerDeserializer(org.apache.kafka.common.serialization.IntegerDeserializer) ApplicationScoped(javax.enterprise.context.ApplicationScoped) CopyOnWriteArrayList(java.util.concurrent.CopyOnWriteArrayList) Incoming(org.eclipse.microprofile.reactive.messaging.Incoming) RecordHeaders(org.apache.kafka.common.header.internals.RecordHeaders) Headers(org.apache.kafka.common.header.Headers) RecordHeaders(org.apache.kafka.common.header.internals.RecordHeaders) RecordHeader(org.apache.kafka.common.header.internals.RecordHeader) 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