1 package de.juplo.kafka;
3 import lombok.extern.slf4j.Slf4j;
4 import org.apache.kafka.common.errors.InvalidProducerEpochException;
5 import org.junit.jupiter.api.*;
6 import org.junit.jupiter.api.extension.ExtendWith;
7 import org.springframework.beans.factory.annotation.Value;
8 import org.springframework.kafka.test.context.EmbeddedKafka;
9 import org.springframework.test.context.junit.jupiter.SpringExtension;
11 import java.time.Duration;
12 import java.util.concurrent.CompletableFuture;
14 import static de.juplo.kafka.ApplicationTests.PARTITIONS;
15 import static de.juplo.kafka.ApplicationTests.TOPIC;
16 import static org.assertj.core.api.Assertions.*;
17 import static org.awaitility.Awaitility.*;
20 @ExtendWith(SpringExtension.class)
23 partitions = PARTITIONS,
25 "transaction.state.log.replication.factor=1",
26 "transaction.state.log.min.isr=1" })
28 public class ApplicationTests
30 static final String TOPIC = "FOO";
31 static final int PARTITIONS = 10;
33 @Value("${spring.embedded.kafka.brokers}")
34 String bootstrapServers;
37 void testConcurrentProducers() throws Exception
39 SimpleProducer instanceA = new SimpleProducer(
46 CompletableFuture<Long> resultA1 = instanceA.send("a","message #1");
47 await("Sending of 1. message for A is completed")
48 .atMost(Duration.ofSeconds(5))
49 .until(() -> resultA1.isDone());
50 assertThat(resultA1.isCompletedExceptionally()).isFalse();
52 SimpleProducer instanceB = new SimpleProducer(
59 CompletableFuture<Long> resultB1 = instanceB.send("b","message #1");
60 await("Sending of 1. message for B is completed")
61 .atMost(Duration.ofSeconds(5))
62 .until(() -> resultB1.isDone());
63 assertThat(resultB1.isCompletedExceptionally()).isFalse();
65 CompletableFuture<Long> resultA2 = instanceA.send("a","message #2");
66 await("Sending of 2. message for A is completed")
67 .atMost(Duration.ofSeconds(5))
68 .until(() -> resultA2.isDone());
69 assertThat(resultA2.isCompletedExceptionally()).isTrue();
71 CompletableFuture<Long> resultB2 = instanceB.send("b","message #2");
72 await("Sending of 2. message for B is completed")
73 .atMost(Duration.ofSeconds(5))
74 .until(() -> resultB2.isDone());
75 assertThat(resultB2.isCompletedExceptionally()).isFalse();
77 assertThatThrownBy(() -> instanceA.commit()).isInstanceOf(InvalidProducerEpochException.class);
78 assertThatCode(() -> instanceB.commit()).doesNotThrowAnyException();