From 1d8148f4c79f8b10ec433047163a038c3aae3459 Mon Sep 17 00:00:00 2001 From: LamGC Date: Fri, 1 Jan 2021 09:45:19 +0800 Subject: [PATCH] =?UTF-8?q?[Add][Update]=20CacheStore-API=20=E6=B7=BB?= =?UTF-8?q?=E5=8A=A0=E6=96=B0=E7=9A=84=E6=B5=8B=E8=AF=95,=20=E5=B9=B6?= =?UTF-8?q?=E5=AE=8C=E5=96=84=E5=85=B6=E4=BB=96=E6=B5=8B=E8=AF=95;?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit [Add] GetCacheStoreExceptionTest 添加针对 GetCacheStoreExceptionTest 的测试项; [Update] HashSingleCacheStoreTest 完善单元测试项; --- .../exception/GetCacheStoreExceptionTest.java | 39 +++++++++++++++++++ .../cache/local/HashSingleCacheStoreTest.java | 33 +++++++++++++++- 2 files changed, 71 insertions(+), 1 deletion(-) create mode 100644 ContentGrabbingJi-CacheStore-api/src/test/java/net/lamgc/cgj/bot/cache/exception/GetCacheStoreExceptionTest.java diff --git a/ContentGrabbingJi-CacheStore-api/src/test/java/net/lamgc/cgj/bot/cache/exception/GetCacheStoreExceptionTest.java b/ContentGrabbingJi-CacheStore-api/src/test/java/net/lamgc/cgj/bot/cache/exception/GetCacheStoreExceptionTest.java new file mode 100644 index 0000000..9013670 --- /dev/null +++ b/ContentGrabbingJi-CacheStore-api/src/test/java/net/lamgc/cgj/bot/cache/exception/GetCacheStoreExceptionTest.java @@ -0,0 +1,39 @@ +/* + * Copyright (C) 2021 LamGC + * + * ContentGrabbingJi is free software: you can redistribute it and/or modify + * it under the terms of the GNU Affero General Public License as + * published by the Free Software Foundation, either version 3 of the + * License, or (at your option) any later version. + * + * ContentGrabbingJi is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU Affero General Public License for more details. + * + * You should have received a copy of the GNU Affero General Public License + * along with this program. If not, see . + */ + +package net.lamgc.cgj.bot.cache.exception; + +import org.junit.Assert; +import org.junit.Test; + +public class GetCacheStoreExceptionTest { + + @Test + public void messageCheck() { + final String message = "uncaught exception"; + Assert.assertEquals(message, new GetCacheStoreException(message).getMessage()); + } + + @Test + public void causeCheck() { + final String message = "uncaught exception"; + final Throwable cause = new IllegalStateException(); + + Assert.assertEquals(cause, new GetCacheStoreException(message, cause).getCause()); + } + +} \ No newline at end of file diff --git a/ContentGrabbingJi-CacheStore-local/src/test/java/net/lamgc/cgj/bot/cache/local/HashSingleCacheStoreTest.java b/ContentGrabbingJi-CacheStore-local/src/test/java/net/lamgc/cgj/bot/cache/local/HashSingleCacheStoreTest.java index cd5eb90..7886a16 100644 --- a/ContentGrabbingJi-CacheStore-local/src/test/java/net/lamgc/cgj/bot/cache/local/HashSingleCacheStoreTest.java +++ b/ContentGrabbingJi-CacheStore-local/src/test/java/net/lamgc/cgj/bot/cache/local/HashSingleCacheStoreTest.java @@ -17,6 +17,7 @@ package net.lamgc.cgj.bot.cache.local; +import com.google.common.base.Throwables; import net.lamgc.cgj.bot.cache.CacheKey; import net.lamgc.cgj.bot.cache.SingleCacheStore; import org.junit.Assert; @@ -68,7 +69,9 @@ public class HashSingleCacheStoreTest { final CacheKey key = new CacheKey("testKey"); final String value = "testValue"; final String value2 = "testValue02"; - Assert.assertTrue("Set operation failed!", cacheStore.set(key, value)); + Assert.assertFalse(cacheStore.exists(key)); + Assert.assertTrue("The key does not exist but an unexpected result was returned!", + cacheStore.setIfNotExist(key, value)); Assert.assertFalse(cacheStore.setIfNotExist(key, value2)); Assert.assertEquals(value, cacheStore.get(key)); @@ -142,4 +145,32 @@ public class HashSingleCacheStoreTest { Assert.assertTrue(expectedMap.keySet().containsAll(cacheStore.keySet())); } + @Test + public void cleanTest() throws InterruptedException { + SingleCacheStore cacheStore = new HashSingleCacheStore<>(); + + final CacheKey persistenceKey = new CacheKey("persistenceKey"); + final CacheKey expireKey = new CacheKey("expireKey"); + final String value = "testValue"; + + // 过期键与持久键 + cacheStore.set(persistenceKey, value); + cacheStore.set(expireKey, value); + cacheStore.setTimeToLive(expireKey, 50); + + Thread.sleep(100L); + + Cleanable cleanableStore = (Cleanable) cacheStore; + try { + cleanableStore.clean(); + } catch (Exception e) { + Assert.fail("Cleaning up expired keys is an exception\n" + + Throwables.getStackTraceAsString(e)); + return; + } + + Assert.assertTrue(cacheStore.exists(persistenceKey)); + Assert.assertFalse(cacheStore.exists(expireKey)); + } + }