Skip to content

Commit

Permalink
Bug fix: get the right exception for error log and result data when d…
Browse files Browse the repository at this point in the history
…ecode value failed in RedisLettuceCache (#806)

Co-authored-by: HHHHHHGGGGGG <gausszhang@hotmail.com>
  • Loading branch information
HHHHHHGGGGGG and ailtex committed Aug 21, 2023
1 parent 8de0ea4 commit 22d0050
Show file tree
Hide file tree
Showing 2 changed files with 46 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -179,8 +179,8 @@ protected CacheGetResult<V> 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);
}
}
}));
Expand Down Expand Up @@ -226,8 +226,8 @@ protected MultiGetResult<K, V> do_GET_ALL(Set<? extends K> 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);
}
}
}));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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.*;

/**
Expand All @@ -32,6 +37,7 @@ public class RedisLettuceCacheFailTest {
private RedisClient client;
private RedisAsyncCommands asyncCommands;
private Cache cache;
private Function<byte[], Object> valueDecoder;

@BeforeEach
public void setup() {
Expand All @@ -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();
}
Expand Down Expand Up @@ -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"))
Expand All @@ -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<byte[],byte[]> 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()))
Expand Down

0 comments on commit 22d0050

Please sign in to comment.