Skip to content

Commit

Permalink
fix flaky penetrationProtectTestWithComputeIfAbsent (#872)
Browse files Browse the repository at this point in the history
* fix flaky penetrationProtectTestWithComputeIfAbsent

* add comment

* fix flaky

* fix typo

* refactor

* refactor

* delay execute
  • Loading branch information
Roiocam authored Mar 27, 2024
1 parent b934d9b commit 1b4dd04
Showing 1 changed file with 30 additions and 17 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,7 @@ protected void baseTest() throws Exception {

asyncTest();

penetrationProtectTestWrapper(cache);
penetrationProtectTest(cache);
}

private void illegalArgTest() {
Expand Down Expand Up @@ -745,38 +745,45 @@ private void checkResult(String key, Integer value, CacheGetResult result) {

}

private static void penetrationProtectTestWrapper(Cache cache) throws Exception {
private static void penetrationProtectTestWrapper(Cache cache, CheckedConsumer<Cache> testFunc) throws Exception {
boolean oldPenetrationProtect = cache.config().isCachePenetrationProtect();
Duration oldTime = cache.config().getPenetrationProtectTimeout();
long oldExpireAfterWriteInMillis = cache.config().getExpireAfterWriteInMillis();
long oldExpireAfterAccessInMillis = cache.config().getExpireAfterAccessInMillis();

cache.config().setCachePenetrationProtect(true);
cache.config().setPenetrationProtectTimeout(null);
cache.config().setExpireAfterWriteInMillis(Integer.MAX_VALUE);
cache.config().setExpireAfterAccessInMillis(Integer.MAX_VALUE);

penetrationProtectTest(cache);
testFunc.accept(cache);

cache.config().setCachePenetrationProtect(oldPenetrationProtect);
cache.config().setPenetrationProtectTimeout(oldTime);
cache.config().setExpireAfterWriteInMillis(oldExpireAfterWriteInMillis);
cache.config().setExpireAfterAccessInMillis(oldExpireAfterAccessInMillis);
}

public static void penetrationProtectTest(Cache cache) throws Exception {
boolean oldPenetrationProtect = cache.config().isCachePenetrationProtect();
Duration oldTime = cache.config().getPenetrationProtectTimeout();
cache.config().setCachePenetrationProtect(true);
penetrationProtectTestWrapper(cache, AbstractCacheTest::penetrationProtectTestWithComputeIfAbsent);

penetrationProtectTestWithComputeIfAbsent(cache);
if (cache instanceof LoadingCache) {
penetrationProtectTestWithLoadingCache(cache);
penetrationProtectTestWrapper(cache, AbstractCacheTest::penetrationProtectTestWithLoadingCache);
}

penetrationProtectReEntryTest(cache);
penetrationProtectTestWrapper(cache, AbstractCacheTest::penetrationProtectReEntryTest);

penetrationProtectTimeoutTest(cache);

cache.config().setCachePenetrationProtect(oldPenetrationProtect);
cache.config().setPenetrationProtectTimeout(oldTime);
penetrationProtectTestWrapper(cache, AbstractCacheTest::penetrationProtectTimeoutTest);
}

/**
* This unit test case verifies that for a single key, only one thread can enter.
*
* @param cache
* @throws Exception
*/
private static void penetrationProtectTestWithComputeIfAbsent(Cache cache) throws Exception {
String keyPrefix = "penetrationProtect_";

AtomicInteger loadSuccess = new AtomicInteger(0);
Function loader = new Function() {
private AtomicInteger count1 = new AtomicInteger(0);
Expand Down Expand Up @@ -832,11 +839,12 @@ public Object apply(Object k) {

Assert.assertFalse(fail.get());
Assert.assertEquals(3, loadSuccess.get());
// the rest of the threads will fail 5 times only.
Assert.assertEquals(2 + 3, getFailCount.get());

cache.remove(keyPrefix + "0");
cache.remove(keyPrefix + "1");
cache.remove(keyPrefix + "2");
Assert.assertTrue(cache.remove(keyPrefix + "0"));
Assert.assertTrue(cache.remove(keyPrefix + "1"));
Assert.assertTrue(cache.remove(keyPrefix + "2"));
}

private static void penetrationProtectTestWithLoadingCache(Cache cache) throws Exception {
Expand Down Expand Up @@ -996,4 +1004,9 @@ private static void penetrationProtectTimeoutTest(Cache cache) throws Exception
Assert.assertEquals(2, loadSuccess.intValue());
Assert.assertTrue(LONG_PROTECT_DURATION.compareTo(duration.get()) > 0);
}

@FunctionalInterface
interface CheckedConsumer<T> {
void accept(T t) throws Exception;
}
}

0 comments on commit 1b4dd04

Please sign in to comment.