From 22d00505c587f5e97811a94a269ea79e0d1bd686 Mon Sep 17 00:00:00 2001 From: HHHHHHGGGGGG <133932662+HHHHHHGGGGGG@users.noreply.github.com> Date: Mon, 21 Aug 2023 18:42:29 +0800 Subject: [PATCH] Bug fix: get the right exception for error log and result data when decode value failed in RedisLettuceCache (#806) Co-authored-by: HHHHHHGGGGGG --- .../redis/lettuce/RedisLettuceCache.java | 8 ++-- .../lettuce/RedisLettuceCacheFailTest.java | 42 +++++++++++++++++++ 2 files changed, 46 insertions(+), 4 deletions(-) diff --git a/jetcache-support/jetcache-redis-lettuce/src/main/java/com/alicp/jetcache/redis/lettuce/RedisLettuceCache.java b/jetcache-support/jetcache-redis-lettuce/src/main/java/com/alicp/jetcache/redis/lettuce/RedisLettuceCache.java index 199f9b80d..56dd67b5c 100644 --- a/jetcache-support/jetcache-redis-lettuce/src/main/java/com/alicp/jetcache/redis/lettuce/RedisLettuceCache.java +++ b/jetcache-support/jetcache-redis-lettuce/src/main/java/com/alicp/jetcache/redis/lettuce/RedisLettuceCache.java @@ -179,8 +179,8 @@ protected CacheGetResult do_GET(K key) { return new ResultData(CacheResultCode.NOT_EXISTS, null, null); } } catch (Exception exception) { - logError("GET", key, ex); - return new ResultData(ex); + logError("GET", key, exception); + return new ResultData(exception); } } })); @@ -226,8 +226,8 @@ protected MultiGetResult do_GET_ALL(Set keys) { } return new ResultData(CacheResultCode.SUCCESS, null, resultMap); } catch (Exception exception) { - logError("GET_ALL", "keys(" + keys.size() + ")", ex); - return new ResultData(ex); + logError("GET_ALL", "keys(" + keys.size() + ")", exception); + return new ResultData(exception); } } })); diff --git a/jetcache-test/src/test/java/com/alicp/jetcache/redis/lettuce/RedisLettuceCacheFailTest.java b/jetcache-test/src/test/java/com/alicp/jetcache/redis/lettuce/RedisLettuceCacheFailTest.java index 94da60877..2db832637 100644 --- a/jetcache-test/src/test/java/com/alicp/jetcache/redis/lettuce/RedisLettuceCacheFailTest.java +++ b/jetcache-test/src/test/java/com/alicp/jetcache/redis/lettuce/RedisLettuceCacheFailTest.java @@ -4,6 +4,8 @@ package com.alicp.jetcache.redis.lettuce; import com.alicp.jetcache.*; +import com.alicp.jetcache.support.Fastjson2KeyConvertor; +import io.lettuce.core.KeyValue; import io.lettuce.core.RedisClient; import io.lettuce.core.RedisFuture; import io.lettuce.core.api.StatefulRedisConnection; @@ -13,15 +15,18 @@ import org.junit.jupiter.api.Test; import org.mockito.ArgumentMatchers; +import java.util.Arrays; import java.util.HashMap; import java.util.HashSet; import java.util.Map; import java.util.concurrent.CompletableFuture; import java.util.concurrent.TimeUnit; import java.util.function.BiFunction; +import java.util.function.Function; import static org.junit.jupiter.api.Assertions.assertEquals; import static org.junit.jupiter.api.Assertions.assertNull; +import static org.junit.jupiter.api.Assertions.assertTrue; import static org.mockito.Mockito.*; /** @@ -32,6 +37,7 @@ public class RedisLettuceCacheFailTest { private RedisClient client; private RedisAsyncCommands asyncCommands; private Cache cache; + private Function valueDecoder; @BeforeEach public void setup() { @@ -42,8 +48,12 @@ public void setup() { when(connection.sync()).thenReturn(null); when(connection.async()).thenReturn(asyncCommands); + valueDecoder = mock(Function.class); + cache = RedisLettuceCacheBuilder.createRedisLettuceCacheBuilder() .redisClient(client) + .valueDecoder(valueDecoder) + .keyConvertor(Fastjson2KeyConvertor.INSTANCE) // logout the readable Key or Keys in error message .keyPrefix("fail_test") .buildCache(); } @@ -76,6 +86,20 @@ public void test_GET() { assertNull(cr.getValue()); } + @Test + public void test_GET_DecodeValueFailed() { + String exceptionMessage = "decodeValueFailed"; + when(valueDecoder.apply(any())).thenThrow(new RuntimeException(exceptionMessage)); + + RedisFuture rf = mockFuture(new byte[]{0x01, 0x02}, null); + when(asyncCommands.get(any())).thenReturn(rf); + + CacheGetResult cr = cache.GET("K"); + assertEquals(CacheResultCode.FAIL, cr.getResultCode()); + assertNull(cr.getValue()); + assertTrue(cr.getMessage().contains(exceptionMessage)); + } + @Test public void test_GET_ALL() { when(asyncCommands.mget(any())).thenThrow(new RuntimeException("err")) @@ -92,6 +116,24 @@ public void test_GET_ALL() { assertNull(cr.getValues()); } + @Test + public void test_GET_ALL_DecodeValueFailed() { + String exceptionMessage = "decodeValueFailed"; + when(valueDecoder.apply(any())).thenThrow(new RuntimeException(exceptionMessage)); + + KeyValue kv = KeyValue.just(new byte[]{0x01}, new byte[]{0x01}); + RedisFuture rf = mockFuture(Arrays.asList(kv), null); + when(asyncCommands.mget(any())).thenReturn(rf); + + HashSet s = new HashSet(); + s.add("K"); + + MultiGetResult cr = cache.GET_ALL(s); + assertEquals(CacheResultCode.FAIL, cr.getResultCode()); + assertNull(cr.getValues()); + assertTrue(cr.getMessage().contains(exceptionMessage)); + } + @Test public void test_PUT() { when(asyncCommands.psetex(any(), anyLong(), any()))