import org.apache.kafka.common.serialization.StringSerializer;
import java.util.Properties;
+import java.util.concurrent.CompletableFuture;
@Slf4j
}
}
- void send(String key, String value)
+ CompletableFuture<Long> send(String key, String value)
{
+ final CompletableFuture<Long> result = new CompletableFuture<>();
final long time = System.currentTimeMillis();
final ProducerRecord<String, String> record = new ProducerRecord<>(
metadata.timestamp(),
now - time
);
+ result.complete(metadata.offset());
}
else
{
now - time,
e.toString()
);
+ result.completeExceptionally(e);
}
});
record.key(),
now - time
);
+
+ return result;
}
package de.juplo.kafka;
import lombok.extern.slf4j.Slf4j;
-import org.apache.kafka.clients.consumer.ConsumerRecord;
import org.junit.jupiter.api.*;
import org.junit.jupiter.api.extension.ExtendWith;
-import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
-import org.springframework.boot.test.autoconfigure.web.servlet.AutoConfigureMockMvc;
-import org.springframework.boot.test.context.SpringBootTest;
-import org.springframework.boot.test.context.TestConfiguration;
-import org.springframework.context.annotation.Bean;
-import org.springframework.kafka.annotation.KafkaListener;
import org.springframework.kafka.test.context.EmbeddedKafka;
import org.springframework.test.context.junit.jupiter.SpringExtension;
-import org.springframework.test.web.servlet.MockMvc;
import java.time.Duration;
-import java.util.LinkedList;
-import java.util.List;
+import java.util.concurrent.CompletableFuture;
import static de.juplo.kafka.ApplicationTests.PARTITIONS;
import static de.juplo.kafka.ApplicationTests.TOPIC;
import static org.awaitility.Awaitility.*;
-import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.post;
-import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.put;
-import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status;
@ExtendWith(SpringExtension.class)
TOPIC,
"B");
+ CompletableFuture<Long> result;
- mockMvc
- .perform(post("/peter").content("Hallo Welt!"))
- .andExpect(status().isOk());
- await("Message was send")
+ result = instanceA.send("a","message #1");
+ await("Sending of 1. message for A is completed")
.atMost(Duration.ofSeconds(5))
- .until(() -> consumer.received.size() == 1);
- }
-
- @Test
- void testSendFooMessage() throws Exception
- {
- mockMvc
- .perform(put("/peter"))
- .andExpect(status().isOk());
- await("Message was send")
- .atMost(Duration.ofSeconds(5))
- .until(() -> consumer.received.size() == 1);
- }
-
- @Test
- void testSendGreeting() throws Exception
- {
- mockMvc
- .perform(post("/").content("peter"))
- .andExpect(status().isOk());
- await("Message was send")
- .atMost(Duration.ofSeconds(5))
- .until(() -> consumer.received.size() == 1);
+ .until(() -> result.isDone());
}
}