From afd09968ac39429f5731e9ab94d39431058e5a92 Mon Sep 17 00:00:00 2001 From: LamGC Date: Fri, 1 Jan 2021 09:29:23 +0800 Subject: [PATCH] =?UTF-8?q?[Add]=20CacheStore-Local=20=E4=B8=BA=20AutoClea?= =?UTF-8?q?nTimer=20=E6=B7=BB=E5=8A=A0=E5=8D=95=E5=85=83=E6=B5=8B=E8=AF=95?= =?UTF-8?q?;?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit [Add] AutoCleanTimer 添加针对 AutoCleanTimer 的单元测试类; --- .../bot/cache/local/AutoCleanTimerTest.java | 91 +++++++++++++++++++ 1 file changed, 91 insertions(+) create mode 100644 ContentGrabbingJi-CacheStore-local/src/test/java/net/lamgc/cgj/bot/cache/local/AutoCleanTimerTest.java diff --git a/ContentGrabbingJi-CacheStore-local/src/test/java/net/lamgc/cgj/bot/cache/local/AutoCleanTimerTest.java b/ContentGrabbingJi-CacheStore-local/src/test/java/net/lamgc/cgj/bot/cache/local/AutoCleanTimerTest.java new file mode 100644 index 0000000..71749e1 --- /dev/null +++ b/ContentGrabbingJi-CacheStore-local/src/test/java/net/lamgc/cgj/bot/cache/local/AutoCleanTimerTest.java @@ -0,0 +1,91 @@ +/* + * Copyright (C) 2020 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.local; + +import net.lamgc.cgj.bot.cache.CacheKey; +import org.junit.Assert; +import org.junit.BeforeClass; +import org.junit.Test; + +import java.lang.ref.ReferenceQueue; + +/** + * @see AutoCleanTimer + */ +public class AutoCleanTimerTest { + + @BeforeClass + public static void before() throws ClassNotFoundException, InterruptedException { + Class.forName(AutoCleanTimer.class.getName(), true, ClassLoader.getSystemClassLoader()); + Thread.sleep(150L); + } + + @Test + public void addTest() throws InterruptedException { + HashSingleCacheStore cacheStore = new HashSingleCacheStore<>(); + AutoCleanTimer.add(cacheStore); + + Thread.sleep(300L); + 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(1000L); + + Assert.assertTrue(cacheStore.exists(persistenceKey)); + Assert.assertFalse(cacheStore.exists(expireKey)); + } + + @Test + public void weakReferenceCleanTest() throws InterruptedException { + ReferenceQueue referenceQueue = new ReferenceQueue<>(); + AutoCleanTimer.setWeakReferenceQueue(referenceQueue); + AutoCleanTimer.add(new HashSingleCacheStore<>()); + System.gc(); + Assert.assertNotNull(referenceQueue.remove(100L)); + System.gc(); + Thread.sleep(300L); + Assert.assertEquals(0, AutoCleanTimer.size()); + } + + @Test + public void methodExceptionThrowTest() throws InterruptedException { + class ThrowExceptionCleanable implements Cleanable { + private boolean throed; + @Override + public long clean() throws Exception { + if (!throed) { + throed = true; + throw new Exception(); + } + return 0; + } + } + + ThrowExceptionCleanable cleanable = new ThrowExceptionCleanable(); + AutoCleanTimer.add(cleanable); + Thread.sleep(300L); + AutoCleanTimer.remove(cleanable); + } + +} \ No newline at end of file