From ed777a6fb33ffc8b2cdb6cf7df9cce51c39d44da Mon Sep 17 00:00:00 2001 From: Kai Moritz Date: Thu, 22 Feb 2024 16:46:27 +0100 Subject: [PATCH] test: `StorageStrategy`-IT are restoring instead of recreating --- .../InMemoryServicesConfiguration.java | 4 +-- .../inmemory/SimpleChatHomeService.java | 3 +++ .../kafka/KafkaServicesConfiguration.java | 2 +- .../backend/AbstractInMemoryStorageIT.java | 12 ++++----- .../backend/AbstractStorageStrategyIT.java | 26 ++++++++----------- .../inmemory/InMemoryTestUtils.java | 15 +++-------- 6 files changed, 25 insertions(+), 37 deletions(-) diff --git a/src/main/java/de/juplo/kafka/chat/backend/implementation/inmemory/InMemoryServicesConfiguration.java b/src/main/java/de/juplo/kafka/chat/backend/implementation/inmemory/InMemoryServicesConfiguration.java index 518cf41b..3f3d8884 100644 --- a/src/main/java/de/juplo/kafka/chat/backend/implementation/inmemory/InMemoryServicesConfiguration.java +++ b/src/main/java/de/juplo/kafka/chat/backend/implementation/inmemory/InMemoryServicesConfiguration.java @@ -26,7 +26,7 @@ public class InMemoryServicesConfiguration name = "sharding-strategy", havingValue = "none", matchIfMissing = true) - ChatHomeService noneShardingChatHome( + SimpleChatHomeService noneShardingChatHome( ChatBackendProperties properties, StorageStrategy storageStrategy, Clock clock) @@ -43,7 +43,7 @@ public class InMemoryServicesConfiguration prefix = "chat.backend.inmemory", name = "sharding-strategy", havingValue = "kafkalike") - ChatHomeService kafkalikeShardingChatHome( + ShardedChatHomeService kafkalikeShardingChatHome( ChatBackendProperties properties, StorageStrategy storageStrategy, Clock clock) diff --git a/src/main/java/de/juplo/kafka/chat/backend/implementation/inmemory/SimpleChatHomeService.java b/src/main/java/de/juplo/kafka/chat/backend/implementation/inmemory/SimpleChatHomeService.java index 5c3fe2e5..371d4a87 100644 --- a/src/main/java/de/juplo/kafka/chat/backend/implementation/inmemory/SimpleChatHomeService.java +++ b/src/main/java/de/juplo/kafka/chat/backend/implementation/inmemory/SimpleChatHomeService.java @@ -49,6 +49,9 @@ public class SimpleChatHomeService implements ChatHomeService Mono restore(StorageStrategy storageStrategy) { + chatRoomInfo.clear(); + chatRoomData.clear(); + return storageStrategy .readChatRoomInfo() .filter(info -> diff --git a/src/main/java/de/juplo/kafka/chat/backend/implementation/kafka/KafkaServicesConfiguration.java b/src/main/java/de/juplo/kafka/chat/backend/implementation/kafka/KafkaServicesConfiguration.java index f8bebd6d..b5a442f8 100644 --- a/src/main/java/de/juplo/kafka/chat/backend/implementation/kafka/KafkaServicesConfiguration.java +++ b/src/main/java/de/juplo/kafka/chat/backend/implementation/kafka/KafkaServicesConfiguration.java @@ -109,7 +109,7 @@ public class KafkaServicesConfiguration } @Bean - ChatHomeService kafkaChatHome( + KafkaChatHomeService kafkaChatHome( ChatBackendProperties properties, InfoChannel infoChannel, DataChannel dataChannel) diff --git a/src/test/java/de/juplo/kafka/chat/backend/AbstractInMemoryStorageIT.java b/src/test/java/de/juplo/kafka/chat/backend/AbstractInMemoryStorageIT.java index 0ec0bc19..843b9490 100644 --- a/src/test/java/de/juplo/kafka/chat/backend/AbstractInMemoryStorageIT.java +++ b/src/test/java/de/juplo/kafka/chat/backend/AbstractInMemoryStorageIT.java @@ -1,15 +1,11 @@ package de.juplo.kafka.chat.backend; -import de.juplo.kafka.chat.backend.domain.ChatHomeService; -import de.juplo.kafka.chat.backend.implementation.StorageStrategy; -import de.juplo.kafka.chat.backend.implementation.inmemory.InMemoryServicesConfiguration; import de.juplo.kafka.chat.backend.implementation.inmemory.InMemoryTestUtils; +import de.juplo.kafka.chat.backend.implementation.inmemory.SimpleChatHomeService; import lombok.extern.slf4j.Slf4j; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.test.context.ContextConfiguration; -import java.time.Clock; - @ContextConfiguration(classes = InMemoryTestUtils.class) @Slf4j @@ -17,10 +13,12 @@ public abstract class AbstractInMemoryStorageIT extends AbstractStorageStrategyI { @Autowired InMemoryTestUtils testUtils; + @Autowired + SimpleChatHomeService simpleChatHomeService; @Override - ChatHomeService getChatHome() + void restore() { - return testUtils.createNoneShardingChatHomeService(); + testUtils.restore(simpleChatHomeService).block(); } } diff --git a/src/test/java/de/juplo/kafka/chat/backend/AbstractStorageStrategyIT.java b/src/test/java/de/juplo/kafka/chat/backend/AbstractStorageStrategyIT.java index 9568545a..7c9c07c3 100644 --- a/src/test/java/de/juplo/kafka/chat/backend/AbstractStorageStrategyIT.java +++ b/src/test/java/de/juplo/kafka/chat/backend/AbstractStorageStrategyIT.java @@ -28,19 +28,15 @@ import static pl.rzrz.assertj.reactor.Assertions.*; @Slf4j public abstract class AbstractStorageStrategyIT { + @Autowired ChatHomeService chathome; - @Autowired StorageStrategy storageStrategy; - abstract ChatHomeService getChatHome(); - protected void start() - { - chathome = getChatHome(); - } + abstract void restore(); - protected void stop() + void store() { storageStrategy .write(chathome) @@ -48,9 +44,9 @@ public abstract class AbstractStorageStrategyIT } @Test - protected void testStoreAndRecreate() + void testStoreAndRecreate() { - start(); + restore(); assertThat(chathome.getChatRoomInfo().toStream()).hasSize(0); @@ -69,8 +65,8 @@ public abstract class AbstractStorageStrategyIT .getChatRoomData(chatRoomId) .flatMapMany(cr -> cr.getMessages())).emitsExactly(m1, m2, m3, m4); - stop(); - start(); + store(); + restore(); assertThat(chathome.getChatRoomInfo().toStream()).containsExactlyElementsOf(List.of(info)); assertThat(chathome.getChatRoomInfo(chatRoomId)).emitsExactly(info); @@ -80,9 +76,9 @@ public abstract class AbstractStorageStrategyIT } @Test - protected void testStoreAndRecreateParallelChatRooms() + void testStoreAndRecreateParallelChatRooms() { - start(); + restore(); assertThat(chathome.getChatRoomInfo().toStream()).hasSize(0); @@ -114,8 +110,8 @@ public abstract class AbstractStorageStrategyIT .getChatRoomData(chatRoomBId) .flatMapMany(cr -> cr.getMessages())).emitsExactly(mb1, mb2, mb3, mb4); - stop(); - start(); + store(); + restore(); assertThat(chathome.getChatRoomInfo().toStream()).containsExactlyInAnyOrderElementsOf(List.of(infoA, infoB)); assertThat(chathome.getChatRoomInfo(chatRoomAId)).emitsExactly(infoA); diff --git a/src/test/java/de/juplo/kafka/chat/backend/implementation/inmemory/InMemoryTestUtils.java b/src/test/java/de/juplo/kafka/chat/backend/implementation/inmemory/InMemoryTestUtils.java index 3f6cf9c0..cfc8e728 100644 --- a/src/test/java/de/juplo/kafka/chat/backend/implementation/inmemory/InMemoryTestUtils.java +++ b/src/test/java/de/juplo/kafka/chat/backend/implementation/inmemory/InMemoryTestUtils.java @@ -4,28 +4,19 @@ import de.juplo.kafka.chat.backend.ChatBackendProperties; import de.juplo.kafka.chat.backend.domain.ChatHomeService; import de.juplo.kafka.chat.backend.implementation.StorageStrategy; import org.springframework.beans.factory.annotation.Autowired; +import reactor.core.publisher.Mono; import java.time.Clock; public class InMemoryTestUtils { - private final InMemoryServicesConfiguration config = - new InMemoryServicesConfiguration(); - - @Autowired - ChatBackendProperties properties; @Autowired StorageStrategy storageStrategy; - @Autowired - Clock clock; - public ChatHomeService createNoneShardingChatHomeService() + public Mono restore(SimpleChatHomeService simpleChatHomeService) { - return config.noneShardingChatHome( - properties, - storageStrategy, - clock); + return simpleChatHomeService.restore(storageStrategy); } } -- 2.20.1