|
| 1 | +package software.amazon.jdbc.plugin.cache; |
| 2 | + |
| 3 | +import static org.junit.jupiter.api.Assertions.assertEquals; |
| 4 | +import static org.junit.jupiter.api.Assertions.assertNull; |
| 5 | + |
| 6 | +import io.lettuce.core.RedisFuture; |
| 7 | +import io.lettuce.core.api.StatefulRedisConnection; |
| 8 | +import io.lettuce.core.api.async.RedisAsyncCommands; |
| 9 | +import io.lettuce.core.api.sync.RedisCommands; |
| 10 | +import org.apache.commons.pool2.impl.GenericObjectPool; |
| 11 | +import org.junit.jupiter.api.AfterEach; |
| 12 | +import org.junit.jupiter.api.BeforeEach; |
| 13 | +import org.junit.jupiter.api.Test; |
| 14 | +import org.mockito.Mock; |
| 15 | +import org.mockito.MockitoAnnotations; |
| 16 | +import java.nio.charset.StandardCharsets; |
| 17 | +import java.util.function.BiConsumer; |
| 18 | +import java.util.Properties; |
| 19 | + |
| 20 | +import static org.mockito.Mockito.*; |
| 21 | + |
| 22 | +public class CacheConnectionTest { |
| 23 | + @Mock GenericObjectPool<StatefulRedisConnection<byte[], byte[]>> mockReadConnPool; |
| 24 | + @Mock GenericObjectPool<StatefulRedisConnection<byte[], byte[]>> mockWriteConnPool; |
| 25 | + @Mock StatefulRedisConnection<byte[], byte[]> mockConnection; |
| 26 | + @Mock RedisCommands<byte[], byte[]> mockSyncCommands; |
| 27 | + @Mock RedisAsyncCommands<byte[], byte[]> mockAsyncCommands; |
| 28 | + @Mock RedisFuture<String> mockCacheResult; |
| 29 | + private AutoCloseable closeable; |
| 30 | + private CacheConnection cacheConnection; |
| 31 | + |
| 32 | + @BeforeEach |
| 33 | + void setUp() { |
| 34 | + closeable = MockitoAnnotations.openMocks(this); |
| 35 | + Properties props = new Properties(); |
| 36 | + props.setProperty("wrapperPlugins", "dataRemoteCache"); |
| 37 | + props.setProperty("cacheEndpointAddrRw", "localhost:6379"); |
| 38 | + props.setProperty("cacheEndpointAddrRo", "localhost:6380"); |
| 39 | + cacheConnection = new CacheConnection(props); |
| 40 | + cacheConnection.setConnectionPools(mockReadConnPool, mockWriteConnPool); |
| 41 | + } |
| 42 | + |
| 43 | + @AfterEach |
| 44 | + void cleanUp() throws Exception { |
| 45 | + closeable.close(); |
| 46 | + } |
| 47 | + |
| 48 | + @Test |
| 49 | + void test_writeToCache() throws Exception { |
| 50 | + String key = "myQueryKey"; |
| 51 | + byte[] value = "myValue".getBytes(StandardCharsets.UTF_8); |
| 52 | + when(mockWriteConnPool.borrowObject()).thenReturn(mockConnection); |
| 53 | + when(mockConnection.async()).thenReturn(mockAsyncCommands); |
| 54 | + when(mockAsyncCommands.set(any(), any(), any())).thenReturn(mockCacheResult); |
| 55 | + when(mockCacheResult.whenComplete(any(BiConsumer.class))).thenReturn(null); |
| 56 | + cacheConnection.writeToCache(key, value, 100); |
| 57 | + verify(mockWriteConnPool).borrowObject(); |
| 58 | + verify(mockConnection).async(); |
| 59 | + verify(mockAsyncCommands).set(any(), any(), any()); |
| 60 | + verify(mockCacheResult).whenComplete(any(BiConsumer.class)); |
| 61 | + } |
| 62 | + |
| 63 | + @Test |
| 64 | + void test_writeToCacheException() throws Exception { |
| 65 | + String key = "myQueryKey"; |
| 66 | + byte[] value = "myValue".getBytes(StandardCharsets.UTF_8); |
| 67 | + when(mockWriteConnPool.borrowObject()).thenReturn(mockConnection); |
| 68 | + when(mockConnection.async()).thenReturn(mockAsyncCommands); |
| 69 | + when(mockAsyncCommands.set(any(), any(), any())).thenThrow(new RuntimeException("test exception")); |
| 70 | + cacheConnection.writeToCache(key, value, 100); |
| 71 | + verify(mockWriteConnPool).borrowObject(); |
| 72 | + verify(mockConnection).async(); |
| 73 | + verify(mockAsyncCommands).set(any(), any(), any()); |
| 74 | + verify(mockWriteConnPool).invalidateObject(mockConnection); |
| 75 | + } |
| 76 | + |
| 77 | + @Test |
| 78 | + void test_handleCompletedCacheWrite() throws Exception { |
| 79 | + cacheConnection.handleCompletedCacheWrite(mockConnection, null); |
| 80 | + verify(mockWriteConnPool).returnObject(mockConnection); |
| 81 | + cacheConnection.handleCompletedCacheWrite(mockConnection, new RuntimeException("test")); |
| 82 | + verify(mockWriteConnPool).invalidateObject(mockConnection); |
| 83 | + } |
| 84 | + |
| 85 | + @Test |
| 86 | + void test_readFromCache() throws Exception { |
| 87 | + byte[] value = "myValue".getBytes(StandardCharsets.UTF_8); |
| 88 | + when(mockReadConnPool.borrowObject()).thenReturn(mockConnection); |
| 89 | + when(mockConnection.sync()).thenReturn(mockSyncCommands); |
| 90 | + when(mockSyncCommands.get(any())).thenReturn(value); |
| 91 | + byte[] result = cacheConnection.readFromCache("myQueryKey"); |
| 92 | + assertEquals(value, result); |
| 93 | + verify(mockReadConnPool).borrowObject(); |
| 94 | + verify(mockConnection).sync(); |
| 95 | + verify(mockSyncCommands).get(any()); |
| 96 | + verify(mockReadConnPool).returnObject(mockConnection); |
| 97 | + } |
| 98 | + |
| 99 | + @Test |
| 100 | + void test_readFromCacheException() throws Exception { |
| 101 | + when(mockReadConnPool.borrowObject()).thenReturn(mockConnection); |
| 102 | + when(mockConnection.sync()).thenReturn(mockSyncCommands); |
| 103 | + when(mockSyncCommands.get(any())).thenThrow(new RuntimeException("test")); |
| 104 | + assertNull(cacheConnection.readFromCache("myQueryKey")); |
| 105 | + verify(mockReadConnPool).borrowObject(); |
| 106 | + verify(mockConnection).sync(); |
| 107 | + verify(mockSyncCommands).get(any()); |
| 108 | + verify(mockReadConnPool).invalidateObject(mockConnection); |
| 109 | + } |
| 110 | +} |
0 commit comments