Skip to content

Commit 1f97357

Browse files
committed
Refine tests involving async caching support.
Closes #2741
1 parent d499c7c commit 1f97357

File tree

3 files changed

+66
-45
lines changed

3 files changed

+66
-45
lines changed

src/test/java/org/springframework/data/redis/cache/DefaultRedisCachWriterUnitTests.java

+22-28
Original file line numberDiff line numberDiff line change
@@ -15,9 +15,15 @@
1515
*/
1616
package org.springframework.data.redis.cache;
1717

18-
import static org.assertj.core.api.Assertions.*;
19-
import static org.mockito.ArgumentMatchers.*;
20-
import static org.mockito.Mockito.*;
18+
import static org.assertj.core.api.Assertions.assertThat;
19+
import static org.mockito.ArgumentMatchers.any;
20+
import static org.mockito.ArgumentMatchers.eq;
21+
import static org.mockito.Mockito.doReturn;
22+
import static org.mockito.Mockito.mock;
23+
import static org.mockito.Mockito.spy;
24+
import static org.mockito.Mockito.times;
25+
import static org.mockito.Mockito.verify;
26+
import static org.mockito.Mockito.verifyNoMoreInteractions;
2127

2228
import java.time.Duration;
2329

@@ -26,8 +32,7 @@
2632
import org.junit.jupiter.api.extension.ExtendWith;
2733
import org.mockito.Mock;
2834
import org.mockito.junit.jupiter.MockitoExtension;
29-
import org.springframework.data.redis.connection.ReactiveRedisConnection;
30-
import org.springframework.data.redis.connection.ReactiveRedisConnectionFactory;
35+
3136
import org.springframework.data.redis.connection.RedisConnection;
3237
import org.springframework.data.redis.connection.RedisConnectionFactory;
3338
import org.springframework.data.redis.connection.RedisStringCommands;
@@ -41,33 +46,25 @@
4146
@ExtendWith(MockitoExtension.class)
4247
class DefaultRedisCacheWriterUnitTests {
4348

44-
@Mock private CacheStatisticsCollector mockCacheStatisticsCollector = mock(CacheStatisticsCollector.class);
45-
46-
@Mock private RedisConnection mockConnection;
49+
@Mock
50+
private CacheStatisticsCollector mockCacheStatisticsCollector = mock(CacheStatisticsCollector.class);
4751

48-
@Mock(strictness = Mock.Strictness.LENIENT) private RedisConnectionFactory mockConnectionFactory;
52+
@Mock
53+
private RedisConnection mockConnection;
4954

50-
@Mock private ReactiveRedisConnection mockReactiveConnection;
51-
52-
@Mock(strictness = Mock.Strictness.LENIENT) private TestReactiveRedisConnectionFactory mockReactiveConnectionFactory;
55+
@Mock(strictness = Mock.Strictness.LENIENT)
56+
private RedisConnectionFactory mockConnectionFactory;
5357

5458
@BeforeEach
5559
void setup() {
5660
doReturn(this.mockConnection).when(this.mockConnectionFactory).getConnection();
57-
doReturn(this.mockConnection).when(this.mockReactiveConnectionFactory).getConnection();
58-
doReturn(this.mockReactiveConnection).when(this.mockReactiveConnectionFactory).getReactiveConnection();
5961
}
6062

6163
private RedisCacheWriter newRedisCacheWriter() {
6264
return spy(new DefaultRedisCacheWriter(this.mockConnectionFactory, mock(BatchStrategy.class))
6365
.withStatisticsCollector(this.mockCacheStatisticsCollector));
6466
}
6567

66-
private RedisCacheWriter newReactiveRedisCacheWriter() {
67-
return spy(new DefaultRedisCacheWriter(this.mockReactiveConnectionFactory, Duration.ZERO, mock(BatchStrategy.class))
68-
.withStatisticsCollector(this.mockCacheStatisticsCollector));
69-
}
70-
7168
@Test // GH-2351
7269
void getWithNonNullTtl() {
7370

@@ -86,9 +83,9 @@ void getWithNonNullTtl() {
8683

8784
assertThat(cacheWriter.get("TestCache", key, ttl)).isEqualTo(value);
8885

89-
verify(this.mockConnection).stringCommands();
90-
verify(mockStringCommands).getEx(eq(key), eq(expiration));
91-
verify(this.mockConnection).close();
86+
verify(this.mockConnection, times(1)).stringCommands();
87+
verify(mockStringCommands, times(1)).getEx(eq(key), eq(expiration));
88+
verify(this.mockConnection, times(1)).close();
9289
verifyNoMoreInteractions(this.mockConnection, mockStringCommands);
9390
}
9491

@@ -107,12 +104,9 @@ void getWithNullTtl() {
107104

108105
assertThat(cacheWriter.get("TestCache", key, null)).isEqualTo(value);
109106

110-
verify(this.mockConnection).stringCommands();
111-
verify(mockStringCommands).get(eq(key));
112-
verify(this.mockConnection).close();
107+
verify(this.mockConnection, times(1)).stringCommands();
108+
verify(mockStringCommands, times(1)).get(eq(key));
109+
verify(this.mockConnection, times(1)).close();
113110
verifyNoMoreInteractions(this.mockConnection, mockStringCommands);
114111
}
115-
116-
interface TestReactiveRedisConnectionFactory extends ReactiveRedisConnectionFactory, RedisConnectionFactory {}
117-
118112
}

src/test/java/org/springframework/data/redis/cache/RedisCacheTests.java

+24-9
Original file line numberDiff line numberDiff line change
@@ -15,8 +15,10 @@
1515
*/
1616
package org.springframework.data.redis.cache;
1717

18-
import static org.assertj.core.api.Assertions.*;
19-
import static org.awaitility.Awaitility.*;
18+
import static org.assertj.core.api.Assertions.assertThat;
19+
import static org.assertj.core.api.Assertions.assertThatExceptionOfType;
20+
import static org.assertj.core.api.Assertions.assertThatIllegalStateException;
21+
import static org.awaitility.Awaitility.await;
2022

2123
import io.netty.util.concurrent.DefaultThreadFactory;
2224

@@ -573,10 +575,16 @@ void cacheGetWithTimeToIdleExpirationAfterEntryExpiresShouldReturnNull() {
573575
void retrieveCacheValueUsingJedis() {
574576

575577
assertThatExceptionOfType(UnsupportedOperationException.class)
576-
.isThrownBy(() -> this.cache.retrieve(this.binaryCacheKey)).withMessageContaining("RedisCache");
578+
.isThrownBy(() -> this.cache.retrieve(this.binaryCacheKey))
579+
.withMessageContaining("RedisCache");
580+
}
581+
582+
@ParameterizedRedisTest // GH-2650
583+
@EnabledOnRedisDriver(RedisDriver.JEDIS)
584+
void retrieveLoadedValueUsingJedis() {
577585

578586
assertThatExceptionOfType(UnsupportedOperationException.class)
579-
.isThrownBy(() -> this.cache.retrieve(this.binaryCacheKey, () -> CompletableFuture.completedFuture("TEST")))
587+
.isThrownBy(() -> this.cache.retrieve(this.binaryCacheKey, () -> usingCompletedFuture("TEST")))
580588
.withMessageContaining("RedisCache");
581589
}
582590

@@ -611,9 +619,11 @@ void retrieveReturnsCachedValueWhenLockIsReleased() throws Exception {
611619
usingRedisCacheConfiguration());
612620

613621
DefaultRedisCacheWriter cacheWriter = (DefaultRedisCacheWriter) cache.getCacheWriter();
622+
614623
cacheWriter.lock("cache");
615624

616625
CompletableFuture<String> value = (CompletableFuture<String>) cache.retrieve(this.key);
626+
617627
assertThat(value).isNotDone();
618628

619629
cacheWriter.unlock("cache");
@@ -626,11 +636,12 @@ void retrieveReturnsCachedValueWhenLockIsReleased() throws Exception {
626636
@EnabledOnRedisDriver(RedisDriver.LETTUCE)
627637
void retrieveReturnsLoadedValue() throws Exception {
628638

629-
RedisCache cache = new RedisCache("cache", usingLockingRedisCacheWriter(), usingRedisCacheConfiguration());
630639
AtomicBoolean loaded = new AtomicBoolean(false);
631640
Person jon = new Person("Jon", Date.from(Instant.now()));
632641
CompletableFuture<Person> valueLoader = CompletableFuture.completedFuture(jon);
633642

643+
RedisCache cache = new RedisCache("cache", usingLockingRedisCacheWriter(), usingRedisCacheConfiguration());
644+
634645
Supplier<CompletableFuture<Person>> valueLoaderSupplier = () -> {
635646
loaded.set(true);
636647
return valueLoader;
@@ -648,15 +659,15 @@ void retrieveReturnsLoadedValue() throws Exception {
648659
@EnabledOnRedisDriver(RedisDriver.LETTUCE)
649660
void retrieveStoresLoadedValue() throws Exception {
650661

651-
RedisCache cache = new RedisCache("cache", usingLockingRedisCacheWriter(), usingRedisCacheConfiguration());
652662
Person jon = new Person("Jon", Date.from(Instant.now()));
653663
Supplier<CompletableFuture<Person>> valueLoaderSupplier = () -> CompletableFuture.completedFuture(jon);
654664

665+
RedisCache cache = new RedisCache("cache", usingLockingRedisCacheWriter(), usingRedisCacheConfiguration());
666+
655667
cache.retrieve(this.key, valueLoaderSupplier).get();
656668

657-
doWithConnection(
658-
connection -> assertThat(connection.keyCommands().exists("cache::key-1".getBytes(StandardCharsets.UTF_8)))
659-
.isTrue());
669+
doWithConnection(connection ->
670+
assertThat(connection.keyCommands().exists("cache::key-1".getBytes(StandardCharsets.UTF_8))).isTrue());
660671
}
661672

662673
@ParameterizedRedisTest // GH-2650
@@ -674,6 +685,10 @@ void retrieveReturnsNull() throws Exception {
674685
assertThat(value).isDone();
675686
}
676687

688+
private <T> CompletableFuture<T> usingCompletedFuture(T value) {
689+
return CompletableFuture.completedFuture(value);
690+
}
691+
677692
private RedisCacheConfiguration usingRedisCacheConfiguration() {
678693
return usingRedisCacheConfiguration(Function.identity());
679694
}

src/test/java/org/springframework/data/redis/cache/RedisCacheUnitTests.java

+20-8
Original file line numberDiff line numberDiff line change
@@ -15,9 +15,16 @@
1515
*/
1616
package org.springframework.data.redis.cache;
1717

18-
import static org.assertj.core.api.Assertions.*;
19-
import static org.mockito.ArgumentMatchers.*;
20-
import static org.mockito.Mockito.*;
18+
import static org.assertj.core.api.Assertions.assertThat;
19+
import static org.mockito.ArgumentMatchers.any;
20+
import static org.mockito.ArgumentMatchers.anyString;
21+
import static org.mockito.ArgumentMatchers.eq;
22+
import static org.mockito.ArgumentMatchers.isA;
23+
import static org.mockito.Mockito.doReturn;
24+
import static org.mockito.Mockito.mock;
25+
import static org.mockito.Mockito.times;
26+
import static org.mockito.Mockito.verify;
27+
import static org.mockito.Mockito.verifyNoMoreInteractions;
2128

2229
import java.util.concurrent.CompletableFuture;
2330

@@ -33,16 +40,18 @@
3340
class RedisCacheUnitTests {
3441

3542
@Test // GH-2650
43+
@SuppressWarnings("unchecked")
3644
void cacheRetrieveValueCallsCacheWriterRetrieveCorrectly() throws Exception {
3745

3846
RedisCacheWriter mockCacheWriter = mock(RedisCacheWriter.class);
3947

40-
when(mockCacheWriter.supportsAsyncRetrieve()).thenReturn(true);
41-
when(mockCacheWriter.retrieve(anyString(), any(byte[].class)))
42-
.thenReturn(CompletableFuture.completedFuture("TEST".getBytes()));
48+
doReturn(true).when(mockCacheWriter).supportsAsyncRetrieve();
49+
doReturn(usingCompletedFuture("TEST".getBytes())).when(mockCacheWriter).retrieve(anyString(), any(byte[].class));
4350

44-
RedisCache cache = new RedisCache("TestCache", mockCacheWriter,
45-
RedisCacheConfiguration.defaultCacheConfig().serializeValuesWith(SerializationPair.byteArray()));
51+
RedisCacheConfiguration cacheConfiguration = RedisCacheConfiguration.defaultCacheConfig()
52+
.serializeValuesWith(SerializationPair.byteArray());
53+
54+
RedisCache cache = new RedisCache("TestCache", mockCacheWriter, cacheConfiguration);
4655

4756
CompletableFuture<byte[]> value = (CompletableFuture<byte[]>) cache.retrieve("TestKey");
4857

@@ -54,4 +63,7 @@ void cacheRetrieveValueCallsCacheWriterRetrieveCorrectly() throws Exception {
5463
verifyNoMoreInteractions(mockCacheWriter);
5564
}
5665

66+
private <T> CompletableFuture<T> usingCompletedFuture(T value) {
67+
return CompletableFuture.completedFuture(value);
68+
}
5769
}

0 commit comments

Comments
 (0)