Search in sources :

Example 1 with ProducerTask

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

the class MultiTopicsTest method testWithOnlyTwoTopicsReceiving.

@RepeatedTest(5)
public void testWithOnlyTwoTopicsReceiving() {
    String topic1 = UUID.randomUUID().toString();
    String topic2 = UUID.randomUUID().toString();
    String topic3 = UUID.randomUUID().toString();
    KafkaConsumer bean = runApplication(kafkaConfig("mp.messaging.incoming.kafka").with("value.deserializer", StringDeserializer.class.getName()).with("topics", topic1 + ", " + topic2 + ", " + topic3).with("graceful-shutdown", false).with("auto.offset.reset", "earliest"), KafkaConsumer.class);
    await().until(this::isReady);
    await().until(this::isAlive);
    assertThat(bean.getMessages()).isEmpty();
    ProducerTask pt1 = companion.produceStrings().usingGenerator(i -> new ProducerRecord<>(topic1, Integer.toString(i), "hello"), 3);
    ProducerTask pt2 = companion.produceStrings().usingGenerator(i -> new ProducerRecord<>(topic3, Integer.toString(i), "bonjour"), 3);
    await().until(() -> pt1.count() == 3 && pt2.count() == 3);
    await().until(() -> bean.getMessages().size() >= 6);
    AtomicInteger top1 = new AtomicInteger();
    AtomicInteger top2 = new AtomicInteger();
    AtomicInteger top3 = new AtomicInteger();
    bean.getMessages().forEach(message -> {
        io.smallrye.reactive.messaging.kafka.api.IncomingKafkaRecordMetadata record = message.getMetadata(io.smallrye.reactive.messaging.kafka.api.IncomingKafkaRecordMetadata.class).orElse(null);
        assertThat(record).isNotNull();
        String topic = record.getTopic();
        if (topic.equals(topic1)) {
            top1.incrementAndGet();
            assertThat(message.getPayload()).isEqualTo("hello");
        } else if (topic.equals(topic2)) {
            top2.incrementAndGet();
        } else if (topic.equals(topic3)) {
            top3.incrementAndGet();
            assertThat(message.getPayload()).isEqualTo("bonjour");
        }
        LegacyMetadataTestUtils.tempCompareLegacyAndApiMetadata(record, message);
    });
    assertThat(top1).hasValue(3);
    assertThat(top2).hasValue(0);
    assertThat(top3).hasValue(3);
}
Also used : StringDeserializer(org.apache.kafka.common.serialization.StringDeserializer) IncomingKafkaRecordMetadata(io.smallrye.reactive.messaging.kafka.api.IncomingKafkaRecordMetadata) IncomingKafkaRecordMetadata(io.smallrye.reactive.messaging.kafka.api.IncomingKafkaRecordMetadata) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) ProducerTask(io.smallrye.reactive.messaging.kafka.companion.ProducerTask) RepeatedTest(org.junit.jupiter.api.RepeatedTest)

Example 2 with ProducerTask

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

the class KafkaWithBaseTest method testWithBase.

@Test
public void testWithBase() {
    // companion is created by the base class
    // produce 100 records to messages topic
    ProducerTask producerTask = companion.produceIntegers().usingGenerator(i -> new ProducerRecord<>("messages", i), 100);
    long msgCount = producerTask.awaitCompletion().count();
    Assertions.assertEquals(msgCount, 100);
    // consume 100 records from messages topic
    ConsumerTask<String, Integer> consumerTask = companion.consumeIntegers().fromTopics("messages", 100);
    ConsumerRecord<String, Integer> lastRecord = consumerTask.awaitCompletion().getLastRecord();
    Assertions.assertEquals(lastRecord.value(), 99);
}
Also used : ProducerTask(io.smallrye.reactive.messaging.kafka.companion.ProducerTask) Test(org.junit.jupiter.api.Test)

Example 3 with ProducerTask

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

the class HealthCheckTest method testHealthOfApplicationWithChannel.

@Test
public void testHealthOfApplicationWithChannel() {
    KafkaMapBasedConfig config = getKafkaSourceConfig(topic);
    LazyConsumingBean bean = runApplication(config, LazyConsumingBean.class);
    ProducerTask produced = companion.produceIntegers().usingGenerator(i -> new ProducerRecord<>(topic, "key", i), 10);
    await().until(this::isStarted);
    await().until(this::isReady);
    await().until(this::isAlive);
    produced.awaitCompletion(Duration.ofMinutes(1));
    Multi<Integer> channel = bean.getChannel();
    channel.select().first(10).collect().asList().await().atMost(Duration.ofSeconds(10));
    HealthReport startup = getHealth().getStartup();
    HealthReport liveness = getHealth().getLiveness();
    HealthReport readiness = getHealth().getReadiness();
    assertThat(startup.isOk()).isTrue();
    assertThat(liveness.isOk()).isTrue();
    assertThat(readiness.isOk()).isTrue();
    assertThat(startup.getChannels()).hasSize(1);
    assertThat(liveness.getChannels()).hasSize(1);
    assertThat(readiness.getChannels()).hasSize(1);
}
Also used : HealthReport(io.smallrye.reactive.messaging.health.HealthReport) KafkaMapBasedConfig(io.smallrye.reactive.messaging.kafka.base.KafkaMapBasedConfig) ProducerTask(io.smallrye.reactive.messaging.kafka.companion.ProducerTask) Test(org.junit.jupiter.api.Test)

Aggregations

ProducerTask (io.smallrye.reactive.messaging.kafka.companion.ProducerTask)3 Test (org.junit.jupiter.api.Test)2 HealthReport (io.smallrye.reactive.messaging.health.HealthReport)1 IncomingKafkaRecordMetadata (io.smallrye.reactive.messaging.kafka.api.IncomingKafkaRecordMetadata)1 KafkaMapBasedConfig (io.smallrye.reactive.messaging.kafka.base.KafkaMapBasedConfig)1 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)1 StringDeserializer (org.apache.kafka.common.serialization.StringDeserializer)1 RepeatedTest (org.junit.jupiter.api.RepeatedTest)1