package de.juplo.kafka.chat.backend;
-import de.juplo.kafka.chat.backend.implementation.kafka.*;
+import de.juplo.kafka.chat.backend.implementation.kafka.ChannelTaskExecutor;
+import de.juplo.kafka.chat.backend.implementation.kafka.DataChannel;
+import de.juplo.kafka.chat.backend.implementation.kafka.KafkaTestUtils;
+import de.juplo.kafka.chat.backend.implementation.kafka.WorkAssignor;
+import de.juplo.kafka.chat.backend.implementation.kafka.messages.AbstractMessageTo;
import lombok.extern.slf4j.Slf4j;
-import org.apache.kafka.clients.producer.ProducerRecord;
-import org.apache.kafka.common.TopicPartition;
-import org.junit.jupiter.api.AfterAll;
+import org.apache.kafka.clients.consumer.Consumer;
import org.junit.jupiter.api.BeforeAll;
import org.springframework.beans.factory.annotation.Autowired;
-import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.boot.test.context.TestConfiguration;
-import org.springframework.boot.test.mock.mockito.MockBean;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Import;
import org.springframework.kafka.core.KafkaTemplate;
-import org.springframework.kafka.support.SendResult;
import org.springframework.kafka.test.context.EmbeddedKafka;
-
-import java.util.List;
+import org.springframework.scheduling.concurrent.ThreadPoolTaskExecutor;
import static de.juplo.kafka.chat.backend.KafkaConfigurationIT.DATA_TOPIC;
import static de.juplo.kafka.chat.backend.KafkaConfigurationIT.INFO_TOPIC;
final static String INFO_TOPIC = "KAFKA_CONFIGURATION_IT_INFO_CHANNEL";
final static String DATA_TOPIC = "KAFKA_CONFIGURATION_IT_DATA_CHANNEL";
- @MockBean
- KafkaServicesApplicationRunner kafkaServicesApplicationRunner;
-
@BeforeAll
- public static void sendAndLoadStoredData(
+ static void sendAndLoadStoredData(
@Autowired KafkaTemplate<String, String> messageTemplate,
- @Autowired ConsumerTaskRunner consumerTaskRunner)
+ @Autowired ChannelTaskExecutor dataChannelTaskExecutor)
{
- send(messageTemplate, INFO_TOPIC, "5c73531c-6fc4-426c-adcb-afc5c140a0f7","{ \"id\": \"5c73531c-6fc4-426c-adcb-afc5c140a0f7\", \"shard\": 2, \"name\": \"FOO\" }", "event_chatroom_created");
- send(messageTemplate, DATA_TOPIC, "5c73531c-6fc4-426c-adcb-afc5c140a0f7","{ \"id\" : 1, \"user\" : \"peter\", \"text\" : \"Hallo, ich heiße Peter!\" }", "event_chatmessage_received");
- send(messageTemplate, DATA_TOPIC, "5c73531c-6fc4-426c-adcb-afc5c140a0f7","{ \"id\" : 1, \"user\" : \"ute\", \"text\" : \"Ich bin Ute...\" }", "event_chatmessage_received");
- send(messageTemplate, DATA_TOPIC, "5c73531c-6fc4-426c-adcb-afc5c140a0f7","{ \"id\" : 2, \"user\" : \"peter\", \"text\" : \"Willst du mit mir gehen?\" }", "event_chatmessage_received");
- send(messageTemplate, DATA_TOPIC, "5c73531c-6fc4-426c-adcb-afc5c140a0f7","{ \"id\" : 1, \"user\" : \"klaus\", \"text\" : \"Ja? Nein? Vielleicht??\" }", "event_chatmessage_received");
-
- consumerTaskRunner.executeConsumerTasks();
- }
+ KafkaTestUtils.sendAndLoadStoredData(
+ messageTemplate,
+ INFO_TOPIC,
+ DATA_TOPIC);
- static void send(
- KafkaTemplate<String, String> kafkaTemplate,
- String topic,
- String key,
- String value,
- String typeId)
- {
- ProducerRecord<String, String> record = new ProducerRecord<>(topic, key, value);
- record.headers().add("__TypeId__", typeId.getBytes());
- SendResult<String, String> result = kafkaTemplate.send(record).join();
- log.info(
- "Sent {}={} to {}",
- key,
- value,
- new TopicPartition(result.getRecordMetadata().topic(), result.getRecordMetadata().partition()));
- }
- @AfterAll
- static void joinConsumerTasks(@Autowired ConsumerTaskRunner consumerTaskRunner)
- {
- consumerTaskRunner.joinConsumerTasks();
+ // The initialization of the data-channel must happen,
+ // after the messages were sent into the topic of the
+ // test-cluster.
+ // Otherwise, the initial loading of the data might be
+ // completed, before these messages arrive, so that
+ // they are ignored and the state is never restored.
+ dataChannelTaskExecutor.executeChannelTask();
}
@TestConfiguration
- @EnableConfigurationProperties(ChatBackendProperties.class)
- @Import(KafkaServicesConfiguration.class)
+ @Import(KafkaTestUtils.KafkaTestConfiguration.class)
static class KafkaConfigurationITConfiguration
{
- @Bean
- WorkAssignor dataChannelWorkAssignor(
- DataChannel dataChannel)
+ /**
+ * The definition of this bean has to be overruled, so
+ * that the configuration of the `initMethod`, which
+ * has to be called explicitly, _after_ the messages
+ * were sent to and received by the test-culster, can
+ * be dropped.
+ */
+ @Bean(destroyMethod = "join")
+ ChannelTaskExecutor dataChannelTaskExecutor(
+ ThreadPoolTaskExecutor taskExecutor,
+ DataChannel dataChannel,
+ Consumer<String, AbstractMessageTo> dataChannelConsumer,
+ WorkAssignor dataChannelWorkAssignor)
{
- return consumer ->
- {
- List<TopicPartition> assignedPartitions =
- List.of(new TopicPartition(DATA_TOPIC, 2));
- consumer.assign(assignedPartitions);
- dataChannel.onPartitionsAssigned(assignedPartitions);
- };
+ return new ChannelTaskExecutor(
+ taskExecutor,
+ dataChannel,
+ dataChannelConsumer,
+ dataChannelWorkAssignor);
}
}
}