From 1c72c849e389fa03a0e7f7006ec2fae1d039596a Mon Sep 17 00:00:00 2001 From: guqing <1484563614@qq.com> Date: Wed, 2 Mar 2022 16:26:20 +0800 Subject: [PATCH 1/3] fix: json to cache wrapper convertor when other exception --- .../java/run/halo/app/cache/AbstractStringCacheStore.java | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/src/main/java/run/halo/app/cache/AbstractStringCacheStore.java b/src/main/java/run/halo/app/cache/AbstractStringCacheStore.java index 3c870bc51d..c9c6f203f2 100644 --- a/src/main/java/run/halo/app/cache/AbstractStringCacheStore.java +++ b/src/main/java/run/halo/app/cache/AbstractStringCacheStore.java @@ -1,6 +1,7 @@ package run.halo.app.cache; import com.fasterxml.jackson.core.JsonProcessingException; +import com.fasterxml.jackson.core.type.TypeReference; import java.io.IOException; import java.util.Optional; import java.util.concurrent.TimeUnit; @@ -22,8 +23,8 @@ protected Optional> jsonToCacheWrapper(String json) { Assert.hasText(json, "json value must not be null"); CacheWrapper cacheWrapper = null; try { - cacheWrapper = JsonUtils.jsonToObject(json, CacheWrapper.class); - } catch (IOException e) { + cacheWrapper = JsonUtils.jsonToObject(json, new TypeReference<>() {}); + } catch (Exception e) { log.debug("Failed to convert json to wrapper value bytes: [{}]", json, e); } return Optional.ofNullable(cacheWrapper); From d1db03fcfd86bc1ffc5b194cbf21e731178432c0 Mon Sep 17 00:00:00 2001 From: guqing <1484563614@qq.com> Date: Wed, 2 Mar 2022 17:33:09 +0800 Subject: [PATCH 2/3] feat: Add test case for level cache --- .../halo/app/cache/LevelCacheStoreTest.java | 54 +++++++++++++++++++ 1 file changed, 54 insertions(+) create mode 100644 src/test/java/run/halo/app/cache/LevelCacheStoreTest.java diff --git a/src/test/java/run/halo/app/cache/LevelCacheStoreTest.java b/src/test/java/run/halo/app/cache/LevelCacheStoreTest.java new file mode 100644 index 0000000000..f0d2a90207 --- /dev/null +++ b/src/test/java/run/halo/app/cache/LevelCacheStoreTest.java @@ -0,0 +1,54 @@ +package run.halo.app.cache; + +import static org.assertj.core.api.Assertions.assertThat; +import static run.halo.app.model.support.HaloConst.FILE_SEPARATOR; + +import java.io.IOException; +import java.nio.charset.StandardCharsets; +import java.util.Optional; +import org.iq80.leveldb.DB; +import org.junit.jupiter.api.AfterEach; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; +import org.springframework.test.util.ReflectionTestUtils; +import run.halo.app.config.properties.HaloProperties; +import run.halo.app.utils.FileUtils; + +/** + * @author guqing + * @date 2022-03-02 + */ +public class LevelCacheStoreTest { + LevelCacheStore cacheStore; + HaloProperties haloProperties = new HaloProperties(); + + @BeforeEach + void setUp() throws IOException { + String testDir = FileUtils.createTempDirectory().toString(); + System.out.println(testDir); + haloProperties.setWorkDir(testDir + FILE_SEPARATOR); + cacheStore = new LevelCacheStore(haloProperties); + cacheStore.init(); + } + + @Test + public void corruptCacheStructureTest() { + cacheStore.put("A", "B"); + + // Simulate corrupt cache structure + DB levelDb = (DB) ReflectionTestUtils.getField(cacheStore, "LEVEL_DB"); + levelDb.put("B".getBytes(StandardCharsets.UTF_8), "NOT_JSON".getBytes(StandardCharsets.UTF_8)); + + Optional bOpt = cacheStore.getAny("B", CacheWrapper.class); + assertThat(bOpt).isNotNull(); + assertThat(bOpt.isEmpty()).isTrue(); + + assertThat(cacheStore.toMap().toString()).isEqualTo("{A=B, B=null}"); + } + + @AfterEach + public void cleanUp() { + cacheStore.delete("A"); + cacheStore.delete("B"); + } +} From eff1f183a9a9c1e450c6e5c2c9f90e78222ac092 Mon Sep 17 00:00:00 2001 From: guqing <1484563614@qq.com> Date: Wed, 2 Mar 2022 17:36:50 +0800 Subject: [PATCH 3/3] fix: code style --- src/test/java/run/halo/app/cache/LevelCacheStoreTest.java | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/src/test/java/run/halo/app/cache/LevelCacheStoreTest.java b/src/test/java/run/halo/app/cache/LevelCacheStoreTest.java index f0d2a90207..d269f7555e 100644 --- a/src/test/java/run/halo/app/cache/LevelCacheStoreTest.java +++ b/src/test/java/run/halo/app/cache/LevelCacheStoreTest.java @@ -37,7 +37,8 @@ public void corruptCacheStructureTest() { // Simulate corrupt cache structure DB levelDb = (DB) ReflectionTestUtils.getField(cacheStore, "LEVEL_DB"); - levelDb.put("B".getBytes(StandardCharsets.UTF_8), "NOT_JSON".getBytes(StandardCharsets.UTF_8)); + levelDb.put("B".getBytes(StandardCharsets.UTF_8), + "NOT_JSON".getBytes(StandardCharsets.UTF_8)); Optional bOpt = cacheStore.getAny("B", CacheWrapper.class); assertThat(bOpt).isNotNull();