diff --git a/src/test/java/org/springframework/data/redis/cache/DefaultRedisCachWriterUnitTests.java b/src/test/java/org/springframework/data/redis/cache/DefaultRedisCachWriterUnitTests.java index c518052f17..7896b5837f 100644 --- a/src/test/java/org/springframework/data/redis/cache/DefaultRedisCachWriterUnitTests.java +++ b/src/test/java/org/springframework/data/redis/cache/DefaultRedisCachWriterUnitTests.java @@ -15,9 +15,15 @@ */ package org.springframework.data.redis.cache; -import static org.assertj.core.api.Assertions.*; -import static org.mockito.ArgumentMatchers.*; -import static org.mockito.Mockito.*; +import static org.assertj.core.api.Assertions.assertThat; +import static org.mockito.ArgumentMatchers.any; +import static org.mockito.ArgumentMatchers.eq; +import static org.mockito.Mockito.doReturn; +import static org.mockito.Mockito.mock; +import static org.mockito.Mockito.spy; +import static org.mockito.Mockito.times; +import static org.mockito.Mockito.verify; +import static org.mockito.Mockito.verifyNoMoreInteractions; import java.time.Duration; @@ -26,8 +32,7 @@ import org.junit.jupiter.api.extension.ExtendWith; import org.mockito.Mock; import org.mockito.junit.jupiter.MockitoExtension; -import org.springframework.data.redis.connection.ReactiveRedisConnection; -import org.springframework.data.redis.connection.ReactiveRedisConnectionFactory; + import org.springframework.data.redis.connection.RedisConnection; import org.springframework.data.redis.connection.RedisConnectionFactory; import org.springframework.data.redis.connection.RedisStringCommands; @@ -41,21 +46,18 @@ @ExtendWith(MockitoExtension.class) class DefaultRedisCacheWriterUnitTests { - @Mock private CacheStatisticsCollector mockCacheStatisticsCollector = mock(CacheStatisticsCollector.class); - - @Mock private RedisConnection mockConnection; + @Mock + private CacheStatisticsCollector mockCacheStatisticsCollector = mock(CacheStatisticsCollector.class); - @Mock(strictness = Mock.Strictness.LENIENT) private RedisConnectionFactory mockConnectionFactory; + @Mock + private RedisConnection mockConnection; - @Mock private ReactiveRedisConnection mockReactiveConnection; - - @Mock(strictness = Mock.Strictness.LENIENT) private TestReactiveRedisConnectionFactory mockReactiveConnectionFactory; + @Mock(strictness = Mock.Strictness.LENIENT) + private RedisConnectionFactory mockConnectionFactory; @BeforeEach void setup() { doReturn(this.mockConnection).when(this.mockConnectionFactory).getConnection(); - doReturn(this.mockConnection).when(this.mockReactiveConnectionFactory).getConnection(); - doReturn(this.mockReactiveConnection).when(this.mockReactiveConnectionFactory).getReactiveConnection(); } private RedisCacheWriter newRedisCacheWriter() { @@ -63,11 +65,6 @@ private RedisCacheWriter newRedisCacheWriter() { .withStatisticsCollector(this.mockCacheStatisticsCollector)); } - private RedisCacheWriter newReactiveRedisCacheWriter() { - return spy(new DefaultRedisCacheWriter(this.mockReactiveConnectionFactory, Duration.ZERO, mock(BatchStrategy.class)) - .withStatisticsCollector(this.mockCacheStatisticsCollector)); - } - @Test // GH-2351 void getWithNonNullTtl() { @@ -86,9 +83,9 @@ void getWithNonNullTtl() { assertThat(cacheWriter.get("TestCache", key, ttl)).isEqualTo(value); - verify(this.mockConnection).stringCommands(); - verify(mockStringCommands).getEx(eq(key), eq(expiration)); - verify(this.mockConnection).close(); + verify(this.mockConnection, times(1)).stringCommands(); + verify(mockStringCommands, times(1)).getEx(eq(key), eq(expiration)); + verify(this.mockConnection, times(1)).close(); verifyNoMoreInteractions(this.mockConnection, mockStringCommands); } @@ -107,12 +104,9 @@ void getWithNullTtl() { assertThat(cacheWriter.get("TestCache", key, null)).isEqualTo(value); - verify(this.mockConnection).stringCommands(); - verify(mockStringCommands).get(eq(key)); - verify(this.mockConnection).close(); + verify(this.mockConnection, times(1)).stringCommands(); + verify(mockStringCommands, times(1)).get(eq(key)); + verify(this.mockConnection, times(1)).close(); verifyNoMoreInteractions(this.mockConnection, mockStringCommands); } - - interface TestReactiveRedisConnectionFactory extends ReactiveRedisConnectionFactory, RedisConnectionFactory {} - } diff --git a/src/test/java/org/springframework/data/redis/cache/RedisCacheTests.java b/src/test/java/org/springframework/data/redis/cache/RedisCacheTests.java index 2d0a1bce1f..26fe4ecc29 100644 --- a/src/test/java/org/springframework/data/redis/cache/RedisCacheTests.java +++ b/src/test/java/org/springframework/data/redis/cache/RedisCacheTests.java @@ -15,8 +15,10 @@ */ package org.springframework.data.redis.cache; -import static org.assertj.core.api.Assertions.*; -import static org.awaitility.Awaitility.*; +import static org.assertj.core.api.Assertions.assertThat; +import static org.assertj.core.api.Assertions.assertThatExceptionOfType; +import static org.assertj.core.api.Assertions.assertThatIllegalStateException; +import static org.awaitility.Awaitility.await; import io.netty.util.concurrent.DefaultThreadFactory; @@ -573,10 +575,16 @@ void cacheGetWithTimeToIdleExpirationAfterEntryExpiresShouldReturnNull() { void retrieveCacheValueUsingJedis() { assertThatExceptionOfType(UnsupportedOperationException.class) - .isThrownBy(() -> this.cache.retrieve(this.binaryCacheKey)).withMessageContaining("RedisCache"); + .isThrownBy(() -> this.cache.retrieve(this.binaryCacheKey)) + .withMessageContaining("RedisCache"); + } + + @ParameterizedRedisTest // GH-2650 + @EnabledOnRedisDriver(RedisDriver.JEDIS) + void retrieveLoadedValueUsingJedis() { assertThatExceptionOfType(UnsupportedOperationException.class) - .isThrownBy(() -> this.cache.retrieve(this.binaryCacheKey, () -> CompletableFuture.completedFuture("TEST"))) + .isThrownBy(() -> this.cache.retrieve(this.binaryCacheKey, () -> usingCompletedFuture("TEST"))) .withMessageContaining("RedisCache"); } @@ -611,9 +619,11 @@ void retrieveReturnsCachedValueWhenLockIsReleased() throws Exception { usingRedisCacheConfiguration()); DefaultRedisCacheWriter cacheWriter = (DefaultRedisCacheWriter) cache.getCacheWriter(); + cacheWriter.lock("cache"); CompletableFuture value = (CompletableFuture) cache.retrieve(this.key); + assertThat(value).isNotDone(); cacheWriter.unlock("cache"); @@ -626,11 +636,12 @@ void retrieveReturnsCachedValueWhenLockIsReleased() throws Exception { @EnabledOnRedisDriver(RedisDriver.LETTUCE) void retrieveReturnsLoadedValue() throws Exception { - RedisCache cache = new RedisCache("cache", usingLockingRedisCacheWriter(), usingRedisCacheConfiguration()); AtomicBoolean loaded = new AtomicBoolean(false); Person jon = new Person("Jon", Date.from(Instant.now())); CompletableFuture valueLoader = CompletableFuture.completedFuture(jon); + RedisCache cache = new RedisCache("cache", usingLockingRedisCacheWriter(), usingRedisCacheConfiguration()); + Supplier> valueLoaderSupplier = () -> { loaded.set(true); return valueLoader; @@ -648,15 +659,15 @@ void retrieveReturnsLoadedValue() throws Exception { @EnabledOnRedisDriver(RedisDriver.LETTUCE) void retrieveStoresLoadedValue() throws Exception { - RedisCache cache = new RedisCache("cache", usingLockingRedisCacheWriter(), usingRedisCacheConfiguration()); Person jon = new Person("Jon", Date.from(Instant.now())); Supplier> valueLoaderSupplier = () -> CompletableFuture.completedFuture(jon); + RedisCache cache = new RedisCache("cache", usingLockingRedisCacheWriter(), usingRedisCacheConfiguration()); + cache.retrieve(this.key, valueLoaderSupplier).get(); - doWithConnection( - connection -> assertThat(connection.keyCommands().exists("cache::key-1".getBytes(StandardCharsets.UTF_8))) - .isTrue()); + doWithConnection(connection -> + assertThat(connection.keyCommands().exists("cache::key-1".getBytes(StandardCharsets.UTF_8))).isTrue()); } @ParameterizedRedisTest // GH-2650 @@ -674,6 +685,10 @@ void retrieveReturnsNull() throws Exception { assertThat(value).isDone(); } + private CompletableFuture usingCompletedFuture(T value) { + return CompletableFuture.completedFuture(value); + } + private RedisCacheConfiguration usingRedisCacheConfiguration() { return usingRedisCacheConfiguration(Function.identity()); } diff --git a/src/test/java/org/springframework/data/redis/cache/RedisCacheUnitTests.java b/src/test/java/org/springframework/data/redis/cache/RedisCacheUnitTests.java index 17663734a3..45d1747d02 100644 --- a/src/test/java/org/springframework/data/redis/cache/RedisCacheUnitTests.java +++ b/src/test/java/org/springframework/data/redis/cache/RedisCacheUnitTests.java @@ -15,9 +15,16 @@ */ package org.springframework.data.redis.cache; -import static org.assertj.core.api.Assertions.*; -import static org.mockito.ArgumentMatchers.*; -import static org.mockito.Mockito.*; +import static org.assertj.core.api.Assertions.assertThat; +import static org.mockito.ArgumentMatchers.any; +import static org.mockito.ArgumentMatchers.anyString; +import static org.mockito.ArgumentMatchers.eq; +import static org.mockito.ArgumentMatchers.isA; +import static org.mockito.Mockito.doReturn; +import static org.mockito.Mockito.mock; +import static org.mockito.Mockito.times; +import static org.mockito.Mockito.verify; +import static org.mockito.Mockito.verifyNoMoreInteractions; import java.util.concurrent.CompletableFuture; @@ -33,16 +40,18 @@ class RedisCacheUnitTests { @Test // GH-2650 + @SuppressWarnings("unchecked") void cacheRetrieveValueCallsCacheWriterRetrieveCorrectly() throws Exception { RedisCacheWriter mockCacheWriter = mock(RedisCacheWriter.class); - when(mockCacheWriter.supportsAsyncRetrieve()).thenReturn(true); - when(mockCacheWriter.retrieve(anyString(), any(byte[].class))) - .thenReturn(CompletableFuture.completedFuture("TEST".getBytes())); + doReturn(true).when(mockCacheWriter).supportsAsyncRetrieve(); + doReturn(usingCompletedFuture("TEST".getBytes())).when(mockCacheWriter).retrieve(anyString(), any(byte[].class)); - RedisCache cache = new RedisCache("TestCache", mockCacheWriter, - RedisCacheConfiguration.defaultCacheConfig().serializeValuesWith(SerializationPair.byteArray())); + RedisCacheConfiguration cacheConfiguration = RedisCacheConfiguration.defaultCacheConfig() + .serializeValuesWith(SerializationPair.byteArray()); + + RedisCache cache = new RedisCache("TestCache", mockCacheWriter, cacheConfiguration); CompletableFuture value = (CompletableFuture) cache.retrieve("TestKey"); @@ -54,4 +63,7 @@ void cacheRetrieveValueCallsCacheWriterRetrieveCorrectly() throws Exception { verifyNoMoreInteractions(mockCacheWriter); } + private CompletableFuture usingCompletedFuture(T value) { + return CompletableFuture.completedFuture(value); + } }