From 0c4e788e00cb2f7ee7ba5b7246165eb68b9eaffd Mon Sep 17 00:00:00 2001 From: dobi Date: Fri, 14 Apr 2017 19:59:19 +0900 Subject: [PATCH] add unit tests for #843 --- .../RedissonSpringCacheShortTTLTest.java | 174 ++++++++++++++++++ .../spring/cache/cache-config-shortTTL.json | 1 + 2 files changed, 175 insertions(+) create mode 100644 redisson/src/test/java/org/redisson/spring/cache/RedissonSpringCacheShortTTLTest.java create mode 100644 redisson/src/test/resources/org/redisson/spring/cache/cache-config-shortTTL.json diff --git a/redisson/src/test/java/org/redisson/spring/cache/RedissonSpringCacheShortTTLTest.java b/redisson/src/test/java/org/redisson/spring/cache/RedissonSpringCacheShortTTLTest.java new file mode 100644 index 000000000..547e62901 --- /dev/null +++ b/redisson/src/test/java/org/redisson/spring/cache/RedissonSpringCacheShortTTLTest.java @@ -0,0 +1,174 @@ +package org.redisson.spring.cache; + +import static org.assertj.core.api.Assertions.assertThat; + +import java.io.IOException; +import java.util.Arrays; +import java.util.HashMap; +import java.util.Map; + +import org.junit.AfterClass; +import org.junit.Test; +import org.junit.runner.RunWith; +import org.junit.runners.Parameterized; +import org.redisson.BaseTest; +import org.redisson.RedisRunner; +import org.redisson.RedisRunner.RedisProcess; +import org.redisson.api.RedissonClient; +import org.springframework.cache.CacheManager; +import org.springframework.cache.annotation.CacheEvict; +import org.springframework.cache.annotation.CachePut; +import org.springframework.cache.annotation.Cacheable; +import org.springframework.cache.annotation.EnableCaching; +import org.springframework.context.ConfigurableApplicationContext; +import org.springframework.context.annotation.AnnotationConfigApplicationContext; +import org.springframework.context.annotation.Bean; +import org.springframework.context.annotation.ComponentScan; +import org.springframework.context.annotation.Configuration; +import org.springframework.stereotype.Service; + +@RunWith(Parameterized.class) +public class RedissonSpringCacheShortTTLTest { + + public static class SampleObject { + + private String name; + private String value; + + public SampleObject() { + } + + public SampleObject(String name, String value) { + super(); + this.name = name; + this.value = value; + } + + public String getName() { + return name; + } + + public String getValue() { + return value; + } + + } + + @Service + public static class SampleBean { + + @CachePut(cacheNames = "testMap", key = "#key") + public SampleObject store(String key, SampleObject object) { + return object; + } + + @CachePut(cacheNames = "testMap", key = "#key") + public SampleObject storeNull(String key) { + return null; + } + + @CacheEvict(cacheNames = "testMap", key = "#key") + public void remove(String key) { + } + + @Cacheable(cacheNames = "testMap", key = "#key") + public SampleObject read(String key) { + throw new IllegalStateException(); + } + + @Cacheable(cacheNames = "testMap", key = "#key", sync = true) + public SampleObject readNullSync(String key) { + return null; + } + + @Cacheable(cacheNames = "testMap", key = "#key") + public SampleObject readNull(String key) { + return null; + } + + } + + @Configuration + @ComponentScan + @EnableCaching + public static class Application { + + @Bean(destroyMethod = "shutdown") + RedissonClient redisson() { + return BaseTest.createInstance(); + } + + @Bean + CacheManager cacheManager(RedissonClient redissonClient) throws IOException { + Map config = new HashMap(); + config.put("testMap", new CacheConfig(1 * 1000, 1 * 1000)); + return new RedissonSpringCacheManager(redissonClient, config); + } + + } + + @Configuration + @ComponentScan + @EnableCaching + public static class JsonConfigApplication { + + @Bean(destroyMethod = "shutdown") + RedissonClient redisson() { + return BaseTest.createInstance(); + } + + @Bean + CacheManager cacheManager(RedissonClient redissonClient) throws IOException { + return new RedissonSpringCacheManager(redissonClient, "classpath:/org/redisson/spring/cache/cache-config-shortTTL.json"); + } + + } + + private static RedisProcess p; + + @Parameterized.Parameters(name = "{index} - {0}") + public static Iterable data() throws IOException, InterruptedException { + if (p == null) { + p = RedisRunner.startDefaultRedisServerInstance(); + } + return Arrays.asList(new Object[][]{ + {new AnnotationConfigApplicationContext(Application.class)}, + {new AnnotationConfigApplicationContext(JsonConfigApplication.class)}, + }); + } + + @Parameterized.Parameter(0) + public AnnotationConfigApplicationContext context; + + @AfterClass + public static void after() throws InterruptedException, IOException { + RedissonSpringCacheShortTTLTest.data().forEach(e -> ((ConfigurableApplicationContext) e[0]).close()); + RedisRunner.shutDownDefaultRedisServerInstance(); + } + + @Test(expected = IllegalStateException.class) + public void testPutGet() throws InterruptedException { + SampleBean bean = context.getBean(SampleBean.class); + bean.store("object1", new SampleObject("name1", "value1")); + SampleObject s = bean.read("object1"); + assertThat(s.getName()).isEqualTo("name1"); + assertThat(s.getValue()).isEqualTo("value1"); + + Thread.sleep(1100); + + bean.read("object1"); + } + + + @Test(expected = IllegalStateException.class) + public void testPutGetSync() throws InterruptedException { + SampleBean bean = context.getBean(SampleBean.class); + bean.readNullSync("object1"); + assertThat(bean.read("object1")).isNull(); + + Thread.sleep(1100); + + bean.read("object1"); + } + +} diff --git a/redisson/src/test/resources/org/redisson/spring/cache/cache-config-shortTTL.json b/redisson/src/test/resources/org/redisson/spring/cache/cache-config-shortTTL.json new file mode 100644 index 000000000..44fc9a7e1 --- /dev/null +++ b/redisson/src/test/resources/org/redisson/spring/cache/cache-config-shortTTL.json @@ -0,0 +1 @@ +{"testMap":{"ttl":1000,"maxIdleTime":1000}} \ No newline at end of file