Files
spring-messaging-demo/kafka-basics/src/test/java/com/ankurm/kafkabasics/ManualAckTest.java
T
2026-08-29 09:47:25 +05:30

86 lines
3.0 KiB
Java

package com.ankurm.kafkabasics;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.kafka.annotation.KafkaListener;
import org.springframework.kafka.config.KafkaListenerEndpointRegistry;
import org.springframework.kafka.core.KafkaTemplate;
import org.springframework.kafka.listener.ContainerProperties.AckMode;
import org.springframework.kafka.support.Acknowledgment;
import org.springframework.kafka.test.context.EmbeddedKafka;
import org.springframework.boot.test.context.TestConfiguration;
import org.springframework.context.annotation.Bean;
import org.springframework.test.context.ActiveProfiles;
import org.springframework.test.context.TestPropertySource;
import java.time.Duration;
import java.util.List;
import java.util.concurrent.CopyOnWriteArrayList;
import static org.assertj.core.api.Assertions.assertThat;
import static org.awaitility.Awaitility.await;
/**
* Manual acknowledgement, and the guard that stands behind it.
*
* <p>{@code ConsumerFactory.isAutoCommit()} answers {@code true} on a stock configuration
* (see {@link EffectiveConfigTest}), so the interesting question is whether asking for
* {@code AckMode.MANUAL} trips Spring Kafka's assertion. It does not, because
* {@code determineAutoCommit} sets the per-container property to {@code false} first and the
* check that follows uses that, not the factory's opinion.
*
* @see <a href="../../../../../docs/06-acknowledgement.md">docs/06-acknowledgement.md</a>
*/
@SpringBootTest
@ActiveProfiles("test")
@TestPropertySource(properties = {
"spring.kafka.listener.ack-mode=MANUAL",
"spring.kafka.consumer.group-id=orders-manual" })
@EmbeddedKafka(topics = { "orders" }, partitions = 1)
class ManualAckTest {
static final List<String> acked = new CopyOnWriteArrayList<>();
@TestConfiguration
static class Listeners {
@Bean
ManualListener manualListener() {
return new ManualListener();
}
}
static class ManualListener {
@KafkaListener(topics = "orders", groupId = "orders-manual")
void onOrder(OrderEvent event, Acknowledgment acknowledgment) {
acked.add(event.orderId());
// Nothing is committed until this line runs. If the process dies above it, the
// record is redelivered - which is the whole point, and also why an Acknowledgment
// you forget to call silently stalls the partition once max.poll.records is reached.
acknowledgment.acknowledge();
}
}
@Autowired
KafkaTemplate<String, OrderEvent> template;
@Autowired
KafkaListenerEndpointRegistry registry;
@Test
void manualAckModeIsAcceptedAndUsed() {
assertThat(this.registry.getListenerContainers())
.allSatisfy((container) -> assertThat(container.getContainerProperties().getAckMode())
.isEqualTo(AckMode.MANUAL));
this.template.send("orders", "o-1", OrderEvent.of("o-1", "c-1", "10.00"));
await().atMost(Duration.ofSeconds(20)).until(() -> acked.contains("o-1"));
assertThat(acked).contains("o-1");
}
}