Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix flaky penetrationProtectTestWithComputeIfAbsent #872

Merged
merged 7 commits into from
Mar 27, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -81,7 +81,7 @@ protected void baseTest() throws Exception {

asyncTest();

penetrationProtectTestWrapper(cache);
penetrationProtectTest(cache);
}

private void illegalArgTest() {
@@ -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);
@@ -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 {
@@ -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;
}
}