Skip to content

Commit

Permalink
improve unit test stability
Browse files Browse the repository at this point in the history
  • Loading branch information
areyouok committed Oct 11, 2023
1 parent b6779f2 commit 9fbfef6
Showing 1 changed file with 59 additions and 38 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
import com.alicp.jetcache.support.DefaultCacheMonitor;
import com.alicp.jetcache.support.StatInfo;
import com.alicp.jetcache.support.StatInfoLogger;
import com.alicp.jetcache.test.anno.TestUtil;
import com.alicp.jetcache.test.support.DynamicQuery;
import org.junit.Assert;
import org.slf4j.Logger;
Expand Down Expand Up @@ -817,7 +818,7 @@ public Object apply(Object k) {
fail.set(true);
}
} catch (Throwable e) {
if(!"mock error".equals(e.getMessage())){
if (!"mock error".equals(e.getMessage())) {
e.printStackTrace();
}
getFailCount.incrementAndGet();
Expand All @@ -842,6 +843,7 @@ private static void penetrationProtectTestWithLoadingCache(Cache cache) throws E

Function<Integer, Integer> loaderFunction = new Function<Integer, Integer>() {
ConcurrentHashMap<Integer, Integer> map = new ConcurrentHashMap<>();

@Override
public Integer apply(Integer key) {
try {
Expand Down Expand Up @@ -914,44 +916,63 @@ private static void penetrationProtectReEntryTest(Cache cache) {
Assert.assertEquals("V", v);
}

private static class PenetrationTestThread extends Thread {

private final String keyPrefix;
private final Cache cache;
private final Function loader;
volatile boolean running;

PenetrationTestThread(String keyPrefix, Cache cache, AtomicInteger loadSuccess) {
this.keyPrefix = keyPrefix;
this.cache = cache;
this.loader = k -> {
try {
Thread.sleep(75);
} catch (InterruptedException e) {
throw new RuntimeException(e);
}
loadSuccess.incrementAndGet();
return k + "_V";
};
}

@Override
public void run() {
running = true;
cache.computeIfAbsent(keyPrefix, loader);
}
}

private static void penetrationProtectTimeoutTest(Cache cache) throws Exception {
String keyPrefix = "penetrationProtectTimeoutTest_";
AtomicInteger loadSuccess = new AtomicInteger(0);
Function loader = k -> {
try {
Thread.sleep(75);
} catch (InterruptedException e) {
throw new RuntimeException(e);
}
loadSuccess.incrementAndGet();
return k + "_V";
};

cache.config().setPenetrationProtectTimeout(Duration.ofMillis(1));
Runnable runnable = () -> cache.computeIfAbsent(keyPrefix + 1, loader);
Thread t1 = new Thread(runnable);
Thread t2 = new Thread(runnable);
t1.start();
t2.start();
t1.join();
t2.join();
Assert.assertEquals(2, loadSuccess.intValue());

cache.config().setPenetrationProtectTimeout(Duration.ofMillis(200));
loadSuccess.set(0);
runnable = () -> cache.computeIfAbsent(keyPrefix + 2, loader);
t1 = new Thread(runnable);
t2 = new Thread(runnable);
Thread t3 = new Thread(runnable);
t1.start();
t2.start();
Thread.sleep(25);
t3.start();
Thread.sleep(25);
t3.interrupt();
t1.join();
t2.join();
t3.join();
Assert.assertEquals(2, loadSuccess.intValue());
{
AtomicInteger loadSuccess = new AtomicInteger(0);
cache.config().setPenetrationProtectTimeout(Duration.ofMillis(1));
PenetrationTestThread t1 = new PenetrationTestThread(keyPrefix + 1, cache, loadSuccess);
PenetrationTestThread t2 = new PenetrationTestThread(keyPrefix + 1, cache, loadSuccess);
t1.start();
t2.start();
t1.join();
t2.join();
Assert.assertEquals(2, loadSuccess.intValue());
}
{
AtomicInteger loadSuccess = new AtomicInteger(0);
cache.config().setPenetrationProtectTimeout(Duration.ofMillis(200));
PenetrationTestThread t1 = new PenetrationTestThread(keyPrefix + 2, cache, loadSuccess);
PenetrationTestThread t2 = new PenetrationTestThread(keyPrefix + 2, cache, loadSuccess);
PenetrationTestThread t3 = new PenetrationTestThread(keyPrefix + 2, cache, loadSuccess);
t1.start();
t2.start();
TestUtil.waitUtil(() -> t1.running == true || t2.running == true);
t3.start();
TestUtil.waitUtil(() -> t3.running == true);
t3.interrupt();
t1.join();
t2.join();
t3.join();
Assert.assertEquals(2, loadSuccess.intValue());
}
}
}

0 comments on commit 9fbfef6

Please sign in to comment.