WIP:haproxy
[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.net.URI;
12 import java.nio.file.Paths;
13 import java.time.Clock;
14 import java.util.stream.IntStream;
15
16 public class ShardedChatHomeServiceTest extends ChatHomeServiceWithShardsTest
17 {
18   @TestConfiguration
19   static class Configuration
20   {
21     @Bean
22     ShardedChatHomeService chatHome(
23         StorageStrategy storageStrategy,
24         Clock clock)
25     {
26       SimpleChatHomeService[] chatHomes = new SimpleChatHomeService[NUM_SHARDS];
27
28       IntStream
29           .of(ownedShards())
30           .forEach(shard -> chatHomes[shard] = new SimpleChatHomeService(
31               shard,
32               storageStrategy,
33               clock,
34               bufferSize()));
35
36       ShardingStrategy strategy = new KafkaLikeShardingStrategy(NUM_SHARDS);
37
38       return new ShardedChatHomeService(
39           "http://instance-0",
40           chatHomes,
41           IntStream
42               .range(0, NUM_SHARDS)
43               .mapToObj(shard -> "http://instance-0")
44               .map(uriString -> URI.create(uriString))
45               .toArray(size -> new URI[size]),
46           strategy);
47     }
48
49     @Bean
50     public FilesStorageStrategy storageStrategy(Clock clock)
51     {
52       return new FilesStorageStrategy(
53           Paths.get("target", "test-classes", "data", "files"),
54           new KafkaLikeShardingStrategy(NUM_SHARDS),
55           new ObjectMapper());
56     }
57
58     @Bean
59     Clock clock()
60     {
61       return Clock.systemDefaultZone();
62     }
63
64     int[] ownedShards()
65     {
66       return new int[] { OWNED_SHARD };
67     }
68
69     int bufferSize()
70     {
71       return 8;
72     }
73   }
74 }