From: Kai Moritz Date: Wed, 23 Aug 2023 16:30:37 +0000 (+0200) Subject: WIP:test: Simp... X-Git-Tag: wip-kafka-chat-home-test--springboot~8 X-Git-Url: http://juplo.de/gitweb/?a=commitdiff_plain;h=3cb0ac8b376573c1229ee525d83611b8af7a7a2b;p=demos%2Fkafka%2Fchat WIP:test: Simp... --- diff --git a/src/main/java/de/juplo/kafka/chat/backend/persistence/inmemory/ShardedChatHome.java b/src/main/java/de/juplo/kafka/chat/backend/persistence/inmemory/ShardedChatHome.java index 4fa4d9bd..ac7a9808 100644 --- a/src/main/java/de/juplo/kafka/chat/backend/persistence/inmemory/ShardedChatHome.java +++ b/src/main/java/de/juplo/kafka/chat/backend/persistence/inmemory/ShardedChatHome.java @@ -44,9 +44,9 @@ public class ShardedChatHome implements ChatHome public Mono getChatRoom(UUID id) { int shard = selectShard(id); - if (chatHomes[shard] == null) - throw new ShardNotOwnedException(shard); - return chatHomes[shard].getChatRoom(id); + return chatHomes[shard] == null + ? Mono.error(new ShardNotOwnedException(shard)) + : chatHomes[shard].getChatRoom(id); } @Override diff --git a/src/test/java/de/juplo/kafka/chat/backend/domain/ChatHomeWithShardsTestBase.java b/src/test/java/de/juplo/kafka/chat/backend/domain/ChatHomeWithShardsTestBase.java index a417e6f9..587c754e 100644 --- a/src/test/java/de/juplo/kafka/chat/backend/domain/ChatHomeWithShardsTestBase.java +++ b/src/test/java/de/juplo/kafka/chat/backend/domain/ChatHomeWithShardsTestBase.java @@ -24,9 +24,9 @@ public class ChatHomeWithShardsTestBase extends ChatHomeTestBase // Then assertThat(mono).sendsError(e -> { - assertThat(e).isInstanceOf(UnknownChatroomException.class); - UnknownChatroomException unknownChatroomException = (UnknownChatroomException) e; - assertThat(unknownChatroomException.getChatroomId()).isEqualTo(chatRoomId); + assertThat(e).isInstanceOf(ShardNotOwnedException.class); + ShardNotOwnedException shardNotOwnedException = (ShardNotOwnedException) e; + assertThat(shardNotOwnedException.getShard()).isEqualTo(0); }); } } diff --git a/src/test/java/de/juplo/kafka/chat/backend/persistence/inmemory/ShardedChatHomeTest.java b/src/test/java/de/juplo/kafka/chat/backend/persistence/inmemory/ShardedChatHomeTest.java index ff82986a..d36bf220 100644 --- a/src/test/java/de/juplo/kafka/chat/backend/persistence/inmemory/ShardedChatHomeTest.java +++ b/src/test/java/de/juplo/kafka/chat/backend/persistence/inmemory/ShardedChatHomeTest.java @@ -19,52 +19,46 @@ public class ShardedChatHomeTest extends ChatHomeWithShardsTestBase { @Bean ShardedChatHome chatHome( - Integer numShards, - int[] ownedShards, InMemoryChatHomeService chatHomeService) { - SimpleChatHome[] chatHomes = new SimpleChatHome[numShards]; + SimpleChatHome[] chatHomes = new SimpleChatHome[numShards()]; IntStream - .of(ownedShards) + .of(ownedShards()) .forEach(shard -> chatHomes[shard] = new SimpleChatHome(chatHomeService, shard)); - ShardingStrategy strategy = new KafkaLikeShardingStrategy(numShards); + ShardingStrategy strategy = new KafkaLikeShardingStrategy(numShards()); return new ShardedChatHome(chatHomes, strategy); } @Bean InMemoryChatHomeService chatHomeService( - Integer numShards, - int[] ownedShards, StorageStrategy storageStrategy) { return new InMemoryChatHomeService( - numShards, - ownedShards, + numShards(), + ownedShards(), storageStrategy.read()); } @Bean - public FilesStorageStrategy storageStrategy(Integer numShards) + public FilesStorageStrategy storageStrategy() { return new FilesStorageStrategy( Paths.get("target", "test-classes", "data", "files"), Clock.systemDefaultZone(), 8, - new KafkaLikeShardingStrategy(numShards), + new KafkaLikeShardingStrategy(numShards()), messageFlux -> new InMemoryChatRoomService(messageFlux), new ObjectMapper()); } - @Bean Integer numShards() { return 10; } - @Bean int[] ownedShards() { return new int[] { 2 };