Search in sources :

Example 16 with MapBasedConfig

use of io.smallrye.reactive.messaging.test.common.config.MapBasedConfig in project smallrye-reactive-messaging by smallrye.

the class PauseResumeTest method testRebalanceDuringPaused.

@Test
void testRebalanceDuringPaused() {
    MapBasedConfig config = commonConfiguration().with(ConsumerConfig.MAX_POLL_RECORDS_CONFIG, 10).with("client.id", UUID.randomUUID().toString());
    String group = UUID.randomUUID().toString();
    source = new KafkaSource<>(vertx, group, new KafkaConnectorIncomingConfiguration(config), getConsumerRebalanceListeners(), CountKafkaCdiEvents.noCdiEvents, getDeserializationFailureHandlers(), -1);
    injectMockConsumer(source, consumer);
    AssertSubscriber<IncomingKafkaRecord<String, String>> subscriber = source.getStream().subscribe().withSubscriber(AssertSubscriber.create(1));
    TopicPartition tp0 = new TopicPartition(TOPIC, 0);
    TopicPartition tp1 = new TopicPartition(TOPIC, 1);
    TopicPartition tp2 = new TopicPartition(TOPIC, 2);
    TopicPartition tp3 = new TopicPartition(TOPIC, 3);
    Map<TopicPartition, Long> beginning = new HashMap<>();
    beginning.put(tp0, 0L);
    beginning.put(tp1, 0L);
    beginning.put(tp2, 0L);
    beginning.put(tp3, 0L);
    consumer.updateBeginningOffsets(beginning);
    // Push 20
    consumer.schedulePollTask(() -> {
        consumer.rebalance(Arrays.asList(tp0, tp1, tp2, tp3));
        for (int i = 0; i < 5; i++) {
            consumer.addRecord(new ConsumerRecord<>(TOPIC, 0, i, "k", "v" + i));
            consumer.addRecord(new ConsumerRecord<>(TOPIC, 1, i, "k", "v" + i));
            consumer.addRecord(new ConsumerRecord<>(TOPIC, 2, i, "k", "v" + i));
            consumer.addRecord(new ConsumerRecord<>(TOPIC, 3, i, "k", "v" + i));
        }
    });
    // Received first
    await().until(() -> subscriber.getItems().size() == 1);
    // Await pause
    await().until(() -> !consumer.paused().isEmpty());
    consumer.schedulePollTask(() -> {
        consumer.rebalance(Arrays.asList(tp0, tp1));
        for (int i = 5; i < 15; i++) {
            consumer.addRecord(new ConsumerRecord<>(TOPIC, 0, i, "k", "v" + i));
            consumer.addRecord(new ConsumerRecord<>(TOPIC, 1, i, "k", "v" + i));
        }
    });
    // Pull 30
    subscriber.request(30);
    // Await resume
    await().until(() -> !resumedPartitions(consumer).isEmpty());
    // Received all
    await().until(() -> subscriber.getItems().size() >= 31);
}
Also used : TopicPartition(org.apache.kafka.common.TopicPartition) MapBasedConfig(io.smallrye.reactive.messaging.test.common.config.MapBasedConfig)

Example 17 with MapBasedConfig

use of io.smallrye.reactive.messaging.test.common.config.MapBasedConfig in project smallrye-reactive-messaging by smallrye.

the class PauseResumeTest method testPauseResumeWithBlockingConsumption.

@Test
void testPauseResumeWithBlockingConsumption() {
    MapBasedConfig config = commonConfiguration().with(ConsumerConfig.MAX_POLL_RECORDS_CONFIG, 2).with("client.id", UUID.randomUUID().toString());
    String group = UUID.randomUUID().toString();
    source = new KafkaSource<>(vertx, group, new KafkaConnectorIncomingConfiguration(config), getConsumerRebalanceListeners(), CountKafkaCdiEvents.noCdiEvents, getDeserializationFailureHandlers(), -1);
    injectMockConsumer(source, consumer);
    List<String> items = new ArrayList<>();
    source.getStream().onItem().transformToUniAndConcatenate(item -> Uni.createFrom().item(item).onItem().delayIt().by(Duration.ofMillis(1000)).onItem().invoke(() -> items.add(item.getPayload()))).subscribe().with(item -> {
    // do nothing
    });
    TopicPartition tp0 = new TopicPartition(TOPIC, 0);
    TopicPartition tp1 = new TopicPartition(TOPIC, 1);
    TopicPartition tp2 = new TopicPartition(TOPIC, 2);
    Map<TopicPartition, Long> beginning = new HashMap<>();
    beginning.put(tp0, 0L);
    beginning.put(tp1, 0L);
    beginning.put(tp2, 0L);
    consumer.updateBeginningOffsets(beginning);
    // There is demand for the first item.
    await().pollInterval(Duration.ofMillis(1)).until(() -> consumer.paused().isEmpty());
    consumer.schedulePollTask(() -> {
        consumer.rebalance(Arrays.asList(tp0, tp1, tp2));
        for (int i = 0; i < 5; i++) {
            consumer.addRecord(new ConsumerRecord<>(TOPIC, 0, i, "k", "v" + i));
        }
    });
    await().until(() -> !consumer.paused().isEmpty());
    // It may await until everything is consumed, but the goal is to verify it resumed.
    await().pollInterval(Duration.ofMillis(1)).until(() -> consumer.paused().isEmpty());
    await().until(() -> items.size() == 5);
}
Also used : TopicPartition(org.apache.kafka.common.TopicPartition) CopyOnWriteArrayList(java.util.concurrent.CopyOnWriteArrayList) MapBasedConfig(io.smallrye.reactive.messaging.test.common.config.MapBasedConfig)

Example 18 with MapBasedConfig

use of io.smallrye.reactive.messaging.test.common.config.MapBasedConfig in project smallrye-reactive-messaging by smallrye.

the class ReactiveKafkaBatchConsumerTest method createSource.

public KafkaSource<Integer, String> createSource() {
    String groupId = UUID.randomUUID().toString();
    MapBasedConfig config = createConsumerConfig(groupId).put("topic", topic).put("batch", true);
    return createSource(config, groupId);
}
Also used : MapBasedConfig(io.smallrye.reactive.messaging.test.common.config.MapBasedConfig)

Example 19 with MapBasedConfig

use of io.smallrye.reactive.messaging.test.common.config.MapBasedConfig in project smallrye-reactive-messaging by smallrye.

the class ReactiveKafkaConsumerTest method testThrottledAcknowledgementWithDefaultValues.

@Test
public void testThrottledAcknowledgementWithDefaultValues() throws Exception {
    String groupId = UUID.randomUUID().toString();
    MapBasedConfig config = createConsumerConfig(groupId).put("topic", topic);
    Multi<IncomingKafkaRecord<Integer, String>> stream = createSource(config, groupId).getStream().invoke(IncomingKafkaRecord::ack);
    sendReceive(stream, 0, 100, 0, 100);
    restartAndCheck(config, groupId, 1);
}
Also used : MapBasedConfig(io.smallrye.reactive.messaging.test.common.config.MapBasedConfig) IncomingKafkaRecord(io.smallrye.reactive.messaging.kafka.IncomingKafkaRecord)

Example 20 with MapBasedConfig

use of io.smallrye.reactive.messaging.test.common.config.MapBasedConfig in project smallrye-reactive-messaging by smallrye.

the class ReactiveKafkaConsumerTest method testThatCloseReleaseTheAssignments.

@Test
public void testThatCloseReleaseTheAssignments() throws Exception {
    int count = 10;
    for (int i = 0; i < 2; i++) {
        String groupId = UUID.randomUUID().toString();
        MapBasedConfig config = createConsumerConfig(groupId).put("topic", topic);
        KafkaSource<Integer, String> source = createSource(config, groupId);
        sendAndWaitForMessages(source.getStream().onItem().invoke(IncomingKafkaRecord::ack), count);
        if (i == 0) {
            waitForCommits(source, count);
        }
        await().until(() -> !source.getConsumer().getAssignments().await().indefinitely().isEmpty());
        assertThat(source.getConsumer().getAssignments().await().indefinitely()).hasSize(partitions);
        source.closeQuietly();
    }
}
Also used : AtomicInteger(java.util.concurrent.atomic.AtomicInteger) MapBasedConfig(io.smallrye.reactive.messaging.test.common.config.MapBasedConfig)

Aggregations

MapBasedConfig (io.smallrye.reactive.messaging.test.common.config.MapBasedConfig)272 Test (org.junit.jupiter.api.Test)223 Message (org.eclipse.microprofile.reactive.messaging.Message)69 JsonObject (io.vertx.core.json.JsonObject)63 ArrayList (java.util.ArrayList)61 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)57 TopicPartition (org.apache.kafka.common.TopicPartition)43 Awaitility.await (org.awaitility.Awaitility.await)38 CopyOnWriteArrayList (java.util.concurrent.CopyOnWriteArrayList)37 Assertions.assertThat (org.assertj.core.api.Assertions.assertThat)34 Weld (org.jboss.weld.environment.se.Weld)32 KafkaMapBasedConfig (io.smallrye.reactive.messaging.kafka.base.KafkaMapBasedConfig)31 HashMap (java.util.HashMap)29 AfterEach (org.junit.jupiter.api.AfterEach)29 Collectors (java.util.stream.Collectors)28 HealthReport (io.smallrye.reactive.messaging.health.HealthReport)26 KafkaSource (io.smallrye.reactive.messaging.kafka.impl.KafkaSource)26 IntegerDeserializer (org.apache.kafka.common.serialization.IntegerDeserializer)25 ProducerRecord (org.apache.kafka.clients.producer.ProducerRecord)23 Duration (java.time.Duration)22