RocksDBCacheTest.java 2.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697
  1. /*
  2. package com.qingjian.common.cache;
  3. import com.fasterxml.jackson.core.type.TypeReference;
  4. import com.qingjian.App;
  5. import com.qingjian.common.rocksdb.DefaultRocksDBStorage;
  6. import com.qingjian.common.utils.Json;
  7. import org.junit.jupiter.api.AfterEach;
  8. import org.junit.jupiter.api.Test;
  9. import org.noear.solon.test.SolonTest;
  10. import java.io.IOException;
  11. import java.nio.file.Files;
  12. import java.nio.file.Path;
  13. import java.util.List;
  14. import java.util.UUID;
  15. import static org.junit.jupiter.api.Assertions.*;
  16. @SolonTest(App.class)
  17. public class RocksDBCacheTest {
  18. private DefaultRocksDBStorage storage;
  19. private Path dbPath;
  20. @AfterEach
  21. void tearDown() throws IOException {
  22. if (storage != null) {
  23. storage.shutdown();
  24. }
  25. if (dbPath != null) {
  26. deleteDirectory(dbPath);
  27. }
  28. }
  29. @Test
  30. void shouldStoreAndReadGenericValue() throws IOException {
  31. RocksDBCache cacheService = newCacheService(30);
  32. List<String> expected = List.of("a", "b", "c");
  33. cacheService.store("list-key", expected, 60);
  34. List<String> actual = cacheService.get("list-key", new TypeReference<List<String>>() {
  35. }.getType());
  36. assertEquals(expected, actual);
  37. }
  38. @Test
  39. void shouldRemoveCachedValue() throws IOException {
  40. RocksDBCache cacheService = newCacheService(30);
  41. cacheService.store("remove-key", "value", 60);
  42. cacheService.remove("remove-key");
  43. assertNull(cacheService.get("remove-key", String.class));
  44. }
  45. @Test
  46. void shouldExpireCachedValueWithDefaultSeconds() throws Exception {
  47. RocksDBCache cacheService = newCacheService(1);
  48. cacheService.store("expire-key", "value", 0);
  49. Thread.sleep(1200L);
  50. assertNull(cacheService.get("expire-key", String.class));
  51. }
  52. private RocksDBCache newCacheService(int defaultSeconds) throws IOException {
  53. dbPath = Path.of("target", "rocksdb-cache-tests", UUID.randomUUID().toString());
  54. Files.createDirectories(dbPath);
  55. storage = new DefaultRocksDBStorage(dbPath.toString());
  56. assertTrue(storage.start());
  57. return new RocksDBCache(Json.objectMapper(), () -> storage);
  58. }
  59. private void deleteDirectory(Path path) throws IOException {
  60. if (Files.notExists(path)) {
  61. return;
  62. }
  63. try (var stream = Files.walk(path)) {
  64. stream.sorted((left, right) -> right.getNameCount() - left.getNameCount())
  65. .forEach(current -> {
  66. try {
  67. Files.deleteIfExists(current);
  68. } catch (IOException e) {
  69. throw new RuntimeException(e);
  70. }
  71. });
  72. } catch (RuntimeException e) {
  73. if (e.getCause() instanceof IOException ioException) {
  74. throw ioException;
  75. }
  76. throw e;
  77. }
  78. }
  79. }
  80. */