5763f76c20fc7802482cae1e29732db1a333f493
[demos/kafka/training] / src / test / java / de / juplo / kafka / ApplicationTests.java
1 package de.juplo.kafka;
2
3 import lombok.extern.slf4j.Slf4j;
4 import org.assertj.core.api.Assertions;
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;
10
11 import java.time.Duration;
12 import java.util.concurrent.CompletableFuture;
13
14 import static de.juplo.kafka.ApplicationTests.PARTITIONS;
15 import static de.juplo.kafka.ApplicationTests.TOPIC;
16 import static org.awaitility.Awaitility.*;
17
18
19 @ExtendWith(SpringExtension.class)
20 @EmbeddedKafka(
21   topics = TOPIC,
22   partitions = PARTITIONS,
23   brokerProperties = {
24     "transaction.state.log.replication.factor=1",
25     "transaction.state.log.min.isr=1" })
26 @Slf4j
27 public class ApplicationTests
28 {
29         static final String TOPIC = "FOO";
30         static final int PARTITIONS = 10;
31
32   @Value("${spring.embedded.kafka.brokers}")
33   String bootstrapServers;
34
35         @Test
36         void testConcurrentProducers() throws Exception
37         {
38     SimpleProducer instanceA = new SimpleProducer(
39       bootstrapServers,
40       TOPIC,
41       "A");
42     SimpleProducer instanceB = new SimpleProducer(
43       bootstrapServers,
44       TOPIC,
45       "B");
46
47     CompletableFuture<Long> result;
48
49     instanceA.begin();
50     result = instanceA.send("a","message #1");
51     Assertions.assertThat(result.isCompletedExceptionally()).isFalse();
52
53     instanceB.begin();
54     result = instanceB.send("b","message #1");
55     Assertions.assertThat(result.isCompletedExceptionally()).isFalse();
56
57     result = instanceA.send("a","message #2");
58     Assertions.assertThat(result.isCompletedExceptionally()).isTrue();
59
60     result = instanceB.send("b","message #2");
61     Assertions.assertThat(result.isCompletedExceptionally()).isFalse();
62   }
63 }