WIP
[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     instanceA.begin();
48     CompletableFuture<Long> resultA1 = instanceA.send("a","message #1");
49                 await("Sending of 1. message for A is completed")
50                                 .atMost(Duration.ofSeconds(5))
51                                 .until(() -> resultA1.isDone());
52     Assertions.assertThat(resultA1.get())
53
54     instanceB.begin();
55     CompletableFuture<Long> resultB1 = instanceB.send("b","message #1");
56     await("Sending of 1. message for B is completed")
57       .atMost(Duration.ofSeconds(5))
58       .until(() -> resultB1.isDone());
59
60     CompletableFuture<Long> resultA2 = instanceA.send("a","message #2");
61     await("Sending of 2. message for A is completed")
62       .atMost(Duration.ofSeconds(5))
63       .until(() -> resultA2.isDone());
64
65     CompletableFuture<Long> resultB2 = instanceB.send("b","message #2");
66     await("Sending of 2. message for B is completed")
67       .atMost(Duration.ofSeconds(5))
68       .until(() -> resultB2.isDone());
69   }
70 }