76ed2baa607c79821c852c54b4ce7b2c135d188d
[demos/kafka/chat] / src / test / java / de / juplo / kafka / chat / backend / implementation / inmemory / ShardedChatHomeServiceTest.java
1 package de.juplo.kafka.chat.backend.implementation.inmemory;
2
3 import com.fasterxml.jackson.databind.ObjectMapper;
4 import de.juplo.kafka.chat.backend.domain.ChatHomeServiceWithShardsTest;
5 import de.juplo.kafka.chat.backend.implementation.ShardingStrategy;
6 import de.juplo.kafka.chat.backend.implementation.StorageStrategy;
7 import de.juplo.kafka.chat.backend.storage.files.FilesStorageStrategy;
8 import org.springframework.boot.test.context.TestConfiguration;
9 import org.springframework.context.annotation.Bean;
10
11 import java.nio.file.Paths;
12 import java.time.Clock;
13 import java.util.stream.IntStream;
14
15 public class ShardedChatHomeServiceTest extends ChatHomeServiceWithShardsTest
16 {
17   @TestConfiguration
18   static class Configuration
19   {
20     @Bean
21     ShardedChatHomeService chatHome(
22         StorageStrategy storageStrategy,
23         Clock clock)
24     {
25       SimpleChatHomeService[] chatHomes = new SimpleChatHomeService[NUM_SHARDS];
26
27       IntStream
28           .of(ownedShards())
29           .forEach(shard -> chatHomes[shard] = new SimpleChatHomeService(
30               shard,
31               storageStrategy,
32               clock,
33               bufferSize()));
34
35       ShardingStrategy strategy = new KafkaLikeShardingStrategy(NUM_SHARDS);
36
37       return new ShardedChatHomeService(chatHomes, strategy);
38     }
39
40     @Bean
41     public FilesStorageStrategy storageStrategy(Clock clock)
42     {
43       return new FilesStorageStrategy(
44           Paths.get("target", "test-classes", "data", "files"),
45           new KafkaLikeShardingStrategy(NUM_SHARDS),
46           new ObjectMapper());
47     }
48
49     @Bean
50     Clock clock()
51     {
52       return Clock.systemDefaultZone();
53     }
54
55     int[] ownedShards()
56     {
57       return new int[] { OWNED_SHARD };
58     }
59
60     int bufferSize()
61     {
62       return 8;
63     }
64   }
65 }