Skip to content

Commit ea074e5

Browse files
committed
test(kafka): add transaction tests for Kafka containers
1 parent 8e54951 commit ea074e5

3 files changed

Lines changed: 73 additions & 0 deletions

File tree

‎modules/kafka/src/test/java/org/testcontainers/AbstractKafka.java‎

Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -141,6 +141,63 @@ protected void testKafkaFunctionality(
141141
}
142142
}
143143

144+
protected void testKafkaTransactionalFunctionality(String bootstrapServers) throws Exception {
145+
Properties adminClientProperties = new Properties();
146+
adminClientProperties.put(AdminClientConfig.BOOTSTRAP_SERVERS_CONFIG, bootstrapServers);
147+
148+
Properties consumerProperties = new Properties();
149+
consumerProperties.put(ConsumerConfig.BOOTSTRAP_SERVERS_CONFIG, bootstrapServers);
150+
consumerProperties.put(ConsumerConfig.GROUP_ID_CONFIG, "tc-" + UUID.randomUUID());
151+
consumerProperties.put(ConsumerConfig.AUTO_OFFSET_RESET_CONFIG, "earliest");
152+
consumerProperties.put(ConsumerConfig.ISOLATION_LEVEL_CONFIG, "read_committed");
153+
154+
Properties producerProperties = new Properties();
155+
producerProperties.put(ProducerConfig.BOOTSTRAP_SERVERS_CONFIG, bootstrapServers);
156+
producerProperties.put(ProducerConfig.TRANSACTIONAL_ID_CONFIG, "tc-" + UUID.randomUUID());
157+
// Fail fast if the transaction state topic can not be created
158+
producerProperties.put(ProducerConfig.MAX_BLOCK_MS_CONFIG, "10000");
159+
160+
try (
161+
AdminClient adminClient = AdminClient.create(adminClientProperties);
162+
KafkaProducer<String, String> producer = new KafkaProducer<>(
163+
producerProperties,
164+
new StringSerializer(),
165+
new StringSerializer()
166+
);
167+
KafkaConsumer<String, String> consumer = new KafkaConsumer<>(
168+
consumerProperties,
169+
new StringDeserializer(),
170+
new StringDeserializer()
171+
);
172+
) {
173+
String topicName = "messages-" + UUID.randomUUID();
174+
175+
Collection<NewTopic> topics = Collections.singletonList(new NewTopic(topicName, 1, (short) 1));
176+
adminClient.createTopics(topics).all().get(30, TimeUnit.SECONDS);
177+
178+
consumer.subscribe(Collections.singletonList(topicName));
179+
180+
producer.initTransactions();
181+
producer.beginTransaction();
182+
producer.send(new ProducerRecord<>(topicName, "testcontainers", "rulezzz")).get();
183+
producer.commitTransaction();
184+
185+
Awaitility
186+
.await()
187+
.atMost(Duration.ofSeconds(10))
188+
.untilAsserted(() -> {
189+
ConsumerRecords<String, String> records = consumer.poll(Duration.ofMillis(100));
190+
191+
assertThat(records)
192+
.hasSize(1)
193+
.extracting(ConsumerRecord::topic, ConsumerRecord::key, ConsumerRecord::value)
194+
.containsExactly(tuple(topicName, "testcontainers", "rulezzz"));
195+
});
196+
197+
consumer.unsubscribe();
198+
}
199+
}
200+
144201
protected static String getJaasConfig() {
145202
String jaasConfig =
146203
"org.apache.kafka.common.security.plain.PlainLoginModule required " +

‎modules/kafka/src/test/java/org/testcontainers/kafka/ConfluentKafkaContainerTest.java‎

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,14 @@ void testUsage() throws Exception {
2424
}
2525
}
2626

27+
@Test
28+
void testUsageWithTransactions() throws Exception {
29+
try (ConfluentKafkaContainer kafka = new ConfluentKafkaContainer("confluentinc/cp-kafka:7.4.0")) {
30+
kafka.start();
31+
testKafkaTransactionalFunctionality(kafka.getBootstrapServers());
32+
}
33+
}
34+
2735
@Test
2836
void testUsageWithListener() throws Exception {
2937
try (

‎modules/kafka/src/test/java/org/testcontainers/kafka/KafkaContainerTest.java‎

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,14 @@ void testUsage() throws Exception {
2121
}
2222
}
2323

24+
@Test
25+
void testUsageWithTransactions() throws Exception {
26+
try (KafkaContainer kafka = new KafkaContainer("apache/kafka-native:3.8.0")) {
27+
kafka.start();
28+
testKafkaTransactionalFunctionality(kafka.getBootstrapServers());
29+
}
30+
}
31+
2432
@Test
2533
void testUsageWithListener() throws Exception {
2634
try (

0 commit comments

Comments
 (0)