refactoring

pull/6406/head
Nikita Koksharov 1 week ago
parent dd4a63eb71
commit f7f0bd8750

@ -45,7 +45,7 @@ public class JCacheTest {
private static final GenericContainer<?> REDIS = private static final GenericContainer<?> REDIS =
new GenericContainer<>("redis:latest") new GenericContainer<>("redis:latest")
.withCreateContainerCmdModifier(cmd -> { .withCreateContainerCmdModifier(cmd -> {
cmd.withCmd("redis-server", "--save", "''"); cmd.withCmd("redis-server", "--save", "''", "--notify-keyspace-events", "Eh");
}) })
.withExposedPorts(6379); .withExposedPorts(6379);
@ -59,13 +59,16 @@ public class JCacheTest {
assertThat(r.getExitCode()).isEqualTo(0); assertThat(r.getExitCode()).isEqualTo(0);
} }
<K, V> MutableConfiguration<K, V> createConfig() {
return new MutableConfiguration<>();
}
@Test @Test
public void testClose() throws IOException { public void testClose() throws IOException {
URL configUrl = getClass().getResource("redisson-jcache.yaml"); URL configUrl = getClass().getResource("redisson-jcache.yaml");
Config cfg = Config.fromYAML(configUrl); Config cfg = Config.fromYAML(configUrl);
MutableConfiguration c = new MutableConfiguration(); MutableConfiguration<String, String> c = createConfig();
c.setStatisticsEnabled(true); c.setStatisticsEnabled(true);
Configuration<String, String> config = RedissonConfiguration.fromConfig(cfg, c); Configuration<String, String> config = RedissonConfiguration.fromConfig(cfg, c);
Cache<String, String> cache = Caching.getCachingProvider() Cache<String, String> cache = Caching.getCachingProvider()
@ -78,7 +81,7 @@ public class JCacheTest {
URL configUrl = getClass().getResource("redisson-jcache.yaml"); URL configUrl = getClass().getResource("redisson-jcache.yaml");
Config cfg = Config.fromYAML(configUrl); Config cfg = Config.fromYAML(configUrl);
MutableConfiguration c = new MutableConfiguration(); MutableConfiguration c = createConfig();
c.setExpiryPolicyFactory(CreatedExpiryPolicy.factoryOf(new Duration(MILLISECONDS, 500))); c.setExpiryPolicyFactory(CreatedExpiryPolicy.factoryOf(new Duration(MILLISECONDS, 500)));
Configuration<String, String> config = RedissonConfiguration.fromConfig(cfg, c); Configuration<String, String> config = RedissonConfiguration.fromConfig(cfg, c);
Cache<String, String> cache = Caching.getCachingProvider().getCacheManager() Cache<String, String> cache = Caching.getCachingProvider().getCacheManager()
@ -121,7 +124,7 @@ public class JCacheTest {
public void testAsync() throws Exception { public void testAsync() throws Exception {
URL configUrl = getClass().getResource("redisson-jcache.yaml"); URL configUrl = getClass().getResource("redisson-jcache.yaml");
Config cfg = Config.fromYAML(configUrl); Config cfg = Config.fromYAML(configUrl);
Configuration<String, String> config = RedissonConfiguration.fromConfig(cfg); Configuration<String, String> config = RedissonConfiguration.fromConfig(cfg);
Cache<String, String> cache = Caching.getCachingProvider().getCacheManager() Cache<String, String> cache = Caching.getCachingProvider().getCacheManager()
.createCache("test", config); .createCache("test", config);
@ -129,15 +132,15 @@ public class JCacheTest {
CacheAsync<String, String> async = cache.unwrap(CacheAsync.class); CacheAsync<String, String> async = cache.unwrap(CacheAsync.class);
async.putAsync("1", "2").get(); async.putAsync("1", "2").get();
assertThat(async.getAsync("1").get()).isEqualTo("2"); assertThat(async.getAsync("1").get()).isEqualTo("2");
cache.close(); cache.close();
} }
@Test @Test
public void testReactive() throws Exception { public void testReactive() throws Exception {
URL configUrl = getClass().getResource("redisson-jcache.yaml"); URL configUrl = getClass().getResource("redisson-jcache.yaml");
Config cfg = Config.fromYAML(configUrl); Config cfg = Config.fromYAML(configUrl);
Configuration<String, String> config = RedissonConfiguration.fromConfig(cfg); Configuration<String, String> config = RedissonConfiguration.fromConfig(cfg);
Cache<String, String> cache = Caching.getCachingProvider().getCacheManager() Cache<String, String> cache = Caching.getCachingProvider().getCacheManager()
.createCache("test", config); .createCache("test", config);
@ -145,15 +148,15 @@ public class JCacheTest {
CacheReactive<String, String> reactive = cache.unwrap(CacheReactive.class); CacheReactive<String, String> reactive = cache.unwrap(CacheReactive.class);
reactive.put("1", "2").block(); reactive.put("1", "2").block();
assertThat(reactive.get("1").block()).isEqualTo("2"); assertThat(reactive.get("1").block()).isEqualTo("2");
cache.close(); cache.close();
} }
@Test @Test
public void testRx() throws Exception { public void testRx() throws Exception {
URL configUrl = getClass().getResource("redisson-jcache.yaml"); URL configUrl = getClass().getResource("redisson-jcache.yaml");
Config cfg = Config.fromYAML(configUrl); Config cfg = Config.fromYAML(configUrl);
Configuration<String, String> config = RedissonConfiguration.fromConfig(cfg); Configuration<String, String> config = RedissonConfiguration.fromConfig(cfg);
Cache<String, String> cache = Caching.getCachingProvider().getCacheManager() Cache<String, String> cache = Caching.getCachingProvider().getCacheManager()
.createCache("test", config); .createCache("test", config);
@ -161,56 +164,56 @@ public class JCacheTest {
CacheRx<String, String> rx = cache.unwrap(CacheRx.class); CacheRx<String, String> rx = cache.unwrap(CacheRx.class);
rx.put("1", "2").blockingAwait(); rx.put("1", "2").blockingAwait();
assertThat(rx.get("1").blockingGet()).isEqualTo("2"); assertThat(rx.get("1").blockingGet()).isEqualTo("2");
cache.close(); cache.close();
} }
@Test @Test
public void testPutAll() throws Exception { public void testPutAll() throws Exception {
URL configUrl = getClass().getResource("redisson-jcache.yaml"); URL configUrl = getClass().getResource("redisson-jcache.yaml");
Config cfg = Config.fromYAML(configUrl); Config cfg = Config.fromYAML(configUrl);
Configuration<String, String> config = RedissonConfiguration.fromConfig(cfg); Configuration<String, String> config = RedissonConfiguration.fromConfig(cfg);
Cache<String, String> cache = Caching.getCachingProvider().getCacheManager() Cache<String, String> cache = Caching.getCachingProvider().getCacheManager()
.createCache("test", config); .createCache("test", config);
Map<String, String> map = new HashMap<>(); Map<String, String> map = new HashMap<>();
for (int i = 0; i < 10000; i++) { for (int i = 0; i < 10000; i++) {
map.put("" + i, "" + i); map.put("" + i, "" + i);
} }
long start = System.currentTimeMillis(); long start = System.currentTimeMillis();
cache.putAll(map); cache.putAll(map);
System.out.println(System.currentTimeMillis() - start); System.out.println(System.currentTimeMillis() - start);
for (int i = 0; i < 10000; i++) { for (int i = 0; i < 10000; i++) {
assertThat(cache.containsKey("" + i)).isTrue(); assertThat(cache.containsKey("" + i)).isTrue();
} }
cache.close(); cache.close();
} }
@Test @Test
public void testRemoveAll() throws Exception { public void testRemoveAll() throws Exception {
URL configUrl = getClass().getResource("redisson-jcache.yaml"); URL configUrl = getClass().getResource("redisson-jcache.yaml");
Config cfg = Config.fromYAML(configUrl); Config cfg = Config.fromYAML(configUrl);
Configuration<String, String> config = RedissonConfiguration.fromConfig(cfg); Configuration<String, String> config = RedissonConfiguration.fromConfig(cfg);
Cache<String, String> cache = Caching.getCachingProvider().getCacheManager() Cache<String, String> cache = Caching.getCachingProvider().getCacheManager()
.createCache("test", config); .createCache("test", config);
cache.put("1", "2"); cache.put("1", "2");
cache.put("3", "4"); cache.put("3", "4");
cache.put("4", "4"); cache.put("4", "4");
cache.put("5", "5"); cache.put("5", "5");
Set<? extends String> keys = new HashSet<String>(Arrays.asList("1", "3", "4", "5")); Set<? extends String> keys = new HashSet<String>(Arrays.asList("1", "3", "4", "5"));
cache.removeAll(keys); cache.removeAll(keys);
assertThat(cache.containsKey("1")).isFalse(); assertThat(cache.containsKey("1")).isFalse();
assertThat(cache.containsKey("3")).isFalse(); assertThat(cache.containsKey("3")).isFalse();
assertThat(cache.containsKey("4")).isFalse(); assertThat(cache.containsKey("4")).isFalse();
assertThat(cache.containsKey("5")).isFalse(); assertThat(cache.containsKey("5")).isFalse();
cache.close(); cache.close();
} }
@ -218,7 +221,7 @@ public class JCacheTest {
public void testGetAllHighVolume() throws Exception { public void testGetAllHighVolume() throws Exception {
URL configUrl = getClass().getResource("redisson-jcache.yaml"); URL configUrl = getClass().getResource("redisson-jcache.yaml");
Config cfg = Config.fromYAML(configUrl); Config cfg = Config.fromYAML(configUrl);
Configuration<String, String> config = RedissonConfiguration.fromConfig(cfg); Configuration<String, String> config = RedissonConfiguration.fromConfig(cfg);
Cache<String, String> cache = Caching.getCachingProvider().getCacheManager() Cache<String, String> cache = Caching.getCachingProvider().getCacheManager()
.createCache("test", config); .createCache("test", config);
@ -228,31 +231,31 @@ public class JCacheTest {
m.put("" + i, "" + i); m.put("" + i, "" + i);
} }
cache.putAll(m); cache.putAll(m);
Map<String, String> entries = cache.getAll(m.keySet()); Map<String, String> entries = cache.getAll(m.keySet());
assertThat(entries).isEqualTo(m); assertThat(entries).isEqualTo(m);
cache.close(); cache.close();
} }
@Test @Test
public void testGetAll() throws Exception { public void testGetAll() throws Exception {
URL configUrl = getClass().getResource("redisson-jcache.yaml"); URL configUrl = getClass().getResource("redisson-jcache.yaml");
Config cfg = Config.fromYAML(configUrl); Config cfg = Config.fromYAML(configUrl);
Configuration<String, String> config = RedissonConfiguration.fromConfig(cfg); Configuration<String, String> config = RedissonConfiguration.fromConfig(cfg);
Cache<String, String> cache = Caching.getCachingProvider().getCacheManager() Cache<String, String> cache = Caching.getCachingProvider().getCacheManager()
.createCache("test", config); .createCache("test", config);
cache.put("1", "2"); cache.put("1", "2");
cache.put("3", "4"); cache.put("3", "4");
Map<String, String> entries = cache.getAll(new HashSet<String>(Arrays.asList("1", "3", "7"))); Map<String, String> entries = cache.getAll(new HashSet<String>(Arrays.asList("1", "3", "7")));
Map<String, String> expected = new HashMap<String, String>(); Map<String, String> expected = new HashMap<String, String>();
expected.put("1", "2"); expected.put("1", "2");
expected.put("3", "4"); expected.put("3", "4");
assertThat(entries).isEqualTo(expected); assertThat(entries).isEqualTo(expected);
cache.close(); cache.close();
} }
@ -261,7 +264,7 @@ public class JCacheTest {
URL configUrl = getClass().getResource("redisson-jcache.yaml"); URL configUrl = getClass().getResource("redisson-jcache.yaml");
Config cfg = Config.fromYAML(configUrl); Config cfg = Config.fromYAML(configUrl);
MutableConfiguration<String, String> jcacheConfig = new MutableConfiguration<>(); MutableConfiguration<String, String> jcacheConfig = createConfig();
jcacheConfig.setReadThrough(true); jcacheConfig.setReadThrough(true);
jcacheConfig.setCacheLoaderFactory(new Factory<CacheLoader<String, String>>() { jcacheConfig.setCacheLoaderFactory(new Factory<CacheLoader<String, String>>() {
@Override @Override
@ -308,15 +311,15 @@ public class JCacheTest {
ObjectMapper objectMapper = new ObjectMapper(); ObjectMapper objectMapper = new ObjectMapper();
objectMapper.registerModule(new JavaTimeModule()); objectMapper.registerModule(new JavaTimeModule());
cfg.setCodec(new TypedJsonJacksonCodec(String.class, LocalDateTime.class, objectMapper)); cfg.setCodec(new TypedJsonJacksonCodec(String.class, LocalDateTime.class, objectMapper));
Configuration<String, LocalDateTime> config = RedissonConfiguration.fromConfig(cfg); Configuration<String, LocalDateTime> config = RedissonConfiguration.fromConfig(cfg);
Cache<String, LocalDateTime> cache = Caching.getCachingProvider().getCacheManager() Cache<String, LocalDateTime> cache = Caching.getCachingProvider().getCacheManager()
.createCache("test", config); .createCache("test", config);
LocalDateTime t = LocalDateTime.now(); LocalDateTime t = LocalDateTime.now();
cache.put("1", t); cache.put("1", t);
Assertions.assertEquals(t, cache.get("1")); Assertions.assertEquals(t, cache.get("1"));
cache.close(); cache.close();
} }
@ -324,14 +327,14 @@ public class JCacheTest {
public void testRedissonConfig() throws IllegalArgumentException, IOException { public void testRedissonConfig() throws IllegalArgumentException, IOException {
URL configUrl = getClass().getResource("redisson-jcache.yaml"); URL configUrl = getClass().getResource("redisson-jcache.yaml");
Config cfg = Config.fromYAML(configUrl); Config cfg = Config.fromYAML(configUrl);
Configuration<String, String> config = RedissonConfiguration.fromConfig(cfg); Configuration<String, String> config = RedissonConfiguration.fromConfig(cfg);
Cache<String, String> cache = Caching.getCachingProvider().getCacheManager() Cache<String, String> cache = Caching.getCachingProvider().getCacheManager()
.createCache("test", config); .createCache("test", config);
cache.put("1", "2"); cache.put("1", "2");
Assertions.assertEquals("2", cache.get("1")); Assertions.assertEquals("2", cache.get("1"));
cache.put("key", "value"); cache.put("key", "value");
String result = cache.getAndRemove("key"); String result = cache.getAndRemove("key");
@ -341,7 +344,7 @@ public class JCacheTest {
cache.put("key", "value"); cache.put("key", "value");
cache.remove("key"); cache.remove("key");
Assertions.assertNull(cache.get("key")); Assertions.assertNull(cache.get("key"));
cache.close(); cache.close();
} }
@ -369,26 +372,26 @@ public class JCacheTest {
Configuration<String, String> config = RedissonConfiguration.fromInstance(redisson); Configuration<String, String> config = RedissonConfiguration.fromInstance(redisson);
Cache<String, String> cache = Caching.getCachingProvider().getCacheManager() Cache<String, String> cache = Caching.getCachingProvider().getCacheManager()
.createCache("test", config); .createCache("test", config);
cache.put("1", "2"); cache.put("1", "2");
Assertions.assertEquals("2", cache.get("1")); Assertions.assertEquals("2", cache.get("1"));
cache.close(); cache.close();
redisson.shutdown(); redisson.shutdown();
} }
@Test @Test
public void testExpiration() throws InterruptedException, IllegalArgumentException, URISyntaxException { public void testExpiration() throws InterruptedException, IllegalArgumentException, URISyntaxException {
MutableConfiguration<String, String> config = new MutableConfiguration<>(); MutableConfiguration<String, String> config = createConfig();
config.setExpiryPolicyFactory(CreatedExpiryPolicy.factoryOf(new Duration(TimeUnit.SECONDS, 1))); config.setExpiryPolicyFactory(CreatedExpiryPolicy.factoryOf(new Duration(TimeUnit.SECONDS, 1)));
config.setStoreByValue(true); config.setStoreByValue(true);
URI configUri = getClass().getResource("redisson-jcache.yaml").toURI(); URI configUri = getClass().getResource("redisson-jcache.yaml").toURI();
Cache<String, String> cache = Caching.getCachingProvider().getCacheManager(configUri, null) Cache<String, String> cache = Caching.getCachingProvider().getCacheManager(configUri, null)
.createCache("test", config); .createCache("test", config);
CountDownLatch latch = new CountDownLatch(1); CountDownLatch latch = new CountDownLatch(1);
String key = "123"; String key = "123";
ExpiredListener clientListener = new ExpiredListener(latch, key, "90"); ExpiredListener clientListener = new ExpiredListener(latch, key, "90");
MutableCacheEntryListenerConfiguration<String, String> listenerConfiguration = MutableCacheEntryListenerConfiguration<String, String> listenerConfiguration =
@ -397,17 +400,17 @@ public class JCacheTest {
cache.put(key, "90"); cache.put(key, "90");
Assertions.assertNotNull(cache.get(key)); Assertions.assertNotNull(cache.get(key));
latch.await(); latch.await();
Assertions.assertNull(cache.get(key)); Assertions.assertNull(cache.get(key));
cache.close(); cache.close();
} }
@Test @Test
public void testUpdate() throws InterruptedException, URISyntaxException { public void testUpdate() throws InterruptedException, URISyntaxException {
MutableConfiguration<String, String> config = new MutableConfiguration<>(); MutableConfiguration<String, String> config = createConfig();
config.setStoreByValue(true); config.setStoreByValue(true);
URI configUri = getClass().getResource("redisson-jcache.yaml").toURI(); URI configUri = getClass().getResource("redisson-jcache.yaml").toURI();
@ -437,7 +440,7 @@ public class JCacheTest {
@Test @Test
public void testUpdateAsync() throws InterruptedException, URISyntaxException { public void testUpdateAsync() throws InterruptedException, URISyntaxException {
MutableConfiguration<String, String> config = new MutableConfiguration<>(); MutableConfiguration<String, String> config = createConfig();
config.setStoreByValue(true); config.setStoreByValue(true);
URI configUri = getClass().getResource("redisson-jcache.yaml").toURI(); URI configUri = getClass().getResource("redisson-jcache.yaml").toURI();
@ -472,7 +475,7 @@ public class JCacheTest {
@Test @Test
public void testUpdateWithoutOldValue() throws InterruptedException, URISyntaxException { public void testUpdateWithoutOldValue() throws InterruptedException, URISyntaxException {
MutableConfiguration<String, String> config = new MutableConfiguration<>(); MutableConfiguration<String, String> config = createConfig();
config.setStoreByValue(true); config.setStoreByValue(true);
URI configUri = getClass().getResource("redisson-jcache.yaml").toURI(); URI configUri = getClass().getResource("redisson-jcache.yaml").toURI();
@ -502,7 +505,7 @@ public class JCacheTest {
@Test @Test
public void testRemoveListener() throws InterruptedException, URISyntaxException { public void testRemoveListener() throws InterruptedException, URISyntaxException {
MutableConfiguration<String, String> config = new MutableConfiguration<>(); MutableConfiguration<String, String> config = createConfig();
config.setStoreByValue(true); config.setStoreByValue(true);
URI configUri = getClass().getResource("redisson-jcache.yaml").toURI(); URI configUri = getClass().getResource("redisson-jcache.yaml").toURI();

Loading…
Cancel
Save