Skip to content

Commit

Permalink
Fix the problem jsonToCacheWrapper crashes when cache value is not JS…
Browse files Browse the repository at this point in the history
…ON format (#1695)

* fix: json to cache wrapper convertor when other exception

* feat: Add test case for level cache
  • Loading branch information
guqing authored Mar 2, 2022
1 parent 4354991 commit 52b3e4f
Show file tree
Hide file tree
Showing 2 changed files with 58 additions and 2 deletions.
Original file line number Diff line number Diff line change
@@ -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;
Expand All @@ -22,8 +23,8 @@ protected Optional<CacheWrapper<String>> jsonToCacheWrapper(String json) {
Assert.hasText(json, "json value must not be null");
CacheWrapper<String> 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);
Expand Down
55 changes: 55 additions & 0 deletions src/test/java/run/halo/app/cache/LevelCacheStoreTest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
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<CacheWrapper> 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");
}
}

0 comments on commit 52b3e4f

Please sign in to comment.