|
15 | 15 | */ |
16 | 16 | package org.springframework.data.redis.cache; |
17 | 17 |
|
18 | | -import static org.assertj.core.api.Assertions.*; |
19 | | -import static org.mockito.ArgumentMatchers.*; |
20 | | -import static org.mockito.Mockito.*; |
21 | | - |
| 18 | +import static org.assertj.core.api.Assertions.assertThat; |
| 19 | +import static org.assertj.core.api.Assertions.assertThatException; |
| 20 | +import static org.mockito.ArgumentMatchers.any; |
| 21 | +import static org.mockito.ArgumentMatchers.eq; |
| 22 | +import static org.mockito.Mockito.doReturn; |
| 23 | +import static org.mockito.Mockito.doThrow; |
| 24 | +import static org.mockito.Mockito.mock; |
| 25 | +import static org.mockito.Mockito.never; |
| 26 | +import static org.mockito.Mockito.spy; |
| 27 | +import static org.mockito.Mockito.times; |
| 28 | +import static org.mockito.Mockito.verify; |
| 29 | +import static org.mockito.Mockito.verifyNoMoreInteractions; |
| 30 | +import static org.mockito.Mockito.withSettings; |
| 31 | + |
| 32 | +import reactor.core.publisher.Mono; |
| 33 | + |
| 34 | +import java.nio.ByteBuffer; |
22 | 35 | import java.time.Duration; |
23 | 36 |
|
24 | 37 | import org.junit.jupiter.api.BeforeEach; |
25 | 38 | import org.junit.jupiter.api.Test; |
26 | 39 | import org.junit.jupiter.api.extension.ExtendWith; |
27 | 40 | import org.mockito.Mock; |
28 | 41 | import org.mockito.junit.jupiter.MockitoExtension; |
29 | | - |
| 42 | +import org.mockito.quality.Strictness; |
30 | 43 | import org.springframework.dao.PessimisticLockingFailureException; |
| 44 | +import org.springframework.data.redis.connection.ReactiveRedisConnection; |
| 45 | +import org.springframework.data.redis.connection.ReactiveRedisConnectionFactory; |
| 46 | +import org.springframework.data.redis.connection.ReactiveStringCommands; |
31 | 47 | import org.springframework.data.redis.connection.RedisConnection; |
32 | 48 | import org.springframework.data.redis.connection.RedisConnectionFactory; |
33 | 49 | import org.springframework.data.redis.connection.RedisKeyCommands; |
@@ -127,4 +143,62 @@ void mustNotUnlockWhenLockingFails() { |
127 | 143 |
|
128 | 144 | verify(mockKeyCommands, never()).del(any()); |
129 | 145 | } |
| 146 | + |
| 147 | + @Test // GH-3236 |
| 148 | + void usesAsyncPutIfPossible() { |
| 149 | + |
| 150 | + byte[] key = "TestKey".getBytes(); |
| 151 | + byte[] value = "TestValue".getBytes(); |
| 152 | + |
| 153 | + RedisConnectionFactory connectionFactory = mock(RedisConnectionFactory.class, |
| 154 | + withSettings().extraInterfaces(ReactiveRedisConnectionFactory.class)); |
| 155 | + ReactiveRedisConnection mockConnection = mock(ReactiveRedisConnection.class); |
| 156 | + ReactiveStringCommands mockStringCommands = mock(ReactiveStringCommands.class); |
| 157 | + |
| 158 | + doReturn(mockConnection).when((ReactiveRedisConnectionFactory) connectionFactory).getReactiveConnection(); |
| 159 | + doReturn(mockStringCommands).when(mockConnection).stringCommands(); |
| 160 | + doReturn(Mono.just(value)).when(mockStringCommands).set(any(), any(), any(), any()); |
| 161 | + |
| 162 | + RedisCacheWriter cacheWriter = RedisCacheWriter.create(connectionFactory, cfg -> { |
| 163 | + cfg.immediateWrites(false); |
| 164 | + }); |
| 165 | + |
| 166 | + cacheWriter.put("TestCache", key, value, null); |
| 167 | + |
| 168 | + verify(mockConnection, times(1)).stringCommands(); |
| 169 | + verify(mockStringCommands, times(1)).set(eq(ByteBuffer.wrap(key)), any()); |
| 170 | + } |
| 171 | + |
| 172 | + @Test // GH-3236 |
| 173 | + void usesBlockingWritesIfConfiguredWithImmediateWritesEnabled() { |
| 174 | + |
| 175 | + byte[] key = "TestKey".getBytes(); |
| 176 | + byte[] value = "TestValue".getBytes(); |
| 177 | + |
| 178 | + RedisConnectionFactory connectionFactory = mock(RedisConnectionFactory.class, |
| 179 | + withSettings().strictness(Strictness.LENIENT).extraInterfaces(ReactiveRedisConnectionFactory.class)); |
| 180 | + ReactiveRedisConnection reactiveMockConnection = mock(ReactiveRedisConnection.class, |
| 181 | + withSettings().strictness(Strictness.LENIENT)); |
| 182 | + ReactiveStringCommands reactiveMockStringCommands = mock(ReactiveStringCommands.class, |
| 183 | + withSettings().strictness(Strictness.LENIENT)); |
| 184 | + |
| 185 | + doReturn(reactiveMockConnection).when((ReactiveRedisConnectionFactory) connectionFactory).getReactiveConnection(); |
| 186 | + doReturn(reactiveMockStringCommands).when(reactiveMockConnection).stringCommands(); |
| 187 | + |
| 188 | + RedisStringCommands mockStringCommands = mock(RedisStringCommands.class); |
| 189 | + |
| 190 | + doReturn(mockStringCommands).when(this.mockConnection).stringCommands(); |
| 191 | + doReturn(this.mockConnection).when(connectionFactory).getConnection(); |
| 192 | + |
| 193 | + RedisCacheWriter cacheWriter = RedisCacheWriter.create(connectionFactory, cfg -> { |
| 194 | + cfg.immediateWrites(true); |
| 195 | + }); |
| 196 | + |
| 197 | + cacheWriter.put("TestCache", key, value, null); |
| 198 | + |
| 199 | + verify(this.mockConnection, times(1)).stringCommands(); |
| 200 | + verify(mockStringCommands, times(1)).set(eq(key), any()); |
| 201 | + verify(reactiveMockConnection, never()).stringCommands(); |
| 202 | + verify(reactiveMockStringCommands, never()).set(eq(ByteBuffer.wrap(key)), any()); |
| 203 | + } |
130 | 204 | } |
0 commit comments