Define TestRule that manages cache use in tests

All current tests that use caches with custom data expiry values
now restore the default config when teardown. We need to prevent
new unsafe uses from being introduced.

Restoration code have also been added to a few other tests that modifies
static fields.

-------------
Created by MOE: https://github.com/google/moe
MOE_MIGRATED_REVID=228888041
This commit is contained in:
weiminyu 2019-01-11 08:44:21 -08:00 committed by Ben McIlwain
parent a6476862fd
commit a80a44cd06
12 changed files with 231 additions and 53 deletions

View file

@ -50,6 +50,7 @@ import java.util.Optional;
import java.util.Set;
import java.util.concurrent.ExecutionException;
import org.joda.time.DateTime;
import org.joda.time.Duration;
/** An EPP entity object (i.e. a domain, contact, or host). */
public abstract class EppResource extends BackupGroupRoot implements Buildable {
@ -343,14 +344,20 @@ public abstract class EppResource extends BackupGroupRoot implements Buildable {
*/
@NonFinalForTesting
private static LoadingCache<Key<? extends EppResource>, EppResource> cacheEppResources =
CacheBuilder.newBuilder()
.expireAfterWrite(getEppResourceCachingDuration().getMillis(), MILLISECONDS)
.maximumSize(getEppResourceMaxCachedEntries())
.build(CACHE_LOADER);
createEppResourcesCache(getEppResourceCachingDuration());
private static LoadingCache<Key<? extends EppResource>, EppResource> createEppResourcesCache(
Duration expiry) {
return CacheBuilder.newBuilder()
.expireAfterWrite(expiry.getMillis(), MILLISECONDS)
.maximumSize(getEppResourceMaxCachedEntries())
.build(CACHE_LOADER);
}
@VisibleForTesting
public static void setCacheForTest(CacheBuilder<Object, Object> cacheBuilder) {
cacheEppResources = cacheBuilder.build(CACHE_LOADER);
public static void setCacheForTest(Optional<Duration> expiry) {
Duration effectiveExpiry = expiry.orElse(getEppResourceCachingDuration());
cacheEppResources = createEppResourcesCache(effectiveExpiry);
}
private static ImmutableMap<Key<? extends EppResource>, EppResource> loadMultiple(

View file

@ -48,6 +48,7 @@ import java.util.Optional;
import java.util.concurrent.ExecutionException;
import javax.annotation.Nullable;
import org.joda.time.DateTime;
import org.joda.time.Duration;
/**
* Class to map a foreign key to the active instance of {@link EppResource} whose unique id matches
@ -220,15 +221,20 @@ public abstract class ForeignKeyIndex<E extends EppResource> extends BackupGroup
*/
@NonFinalForTesting
private static LoadingCache<Key<ForeignKeyIndex<?>>, Optional<ForeignKeyIndex<?>>>
cacheForeignKeyIndexes =
CacheBuilder.newBuilder()
.expireAfterWrite(getEppResourceCachingDuration().getMillis(), MILLISECONDS)
.maximumSize(getEppResourceMaxCachedEntries())
.build(CACHE_LOADER);
cacheForeignKeyIndexes = createForeignKeyIndexesCache(getEppResourceCachingDuration());
private static LoadingCache<Key<ForeignKeyIndex<?>>, Optional<ForeignKeyIndex<?>>>
createForeignKeyIndexesCache(Duration expiry) {
return CacheBuilder.newBuilder()
.expireAfterWrite(expiry.getMillis(), MILLISECONDS)
.maximumSize(getEppResourceMaxCachedEntries())
.build(CACHE_LOADER);
}
@VisibleForTesting
public static void setCacheForTest(CacheBuilder<Object, Object> cacheBuilder) {
cacheForeignKeyIndexes = cacheBuilder.build(CACHE_LOADER);
public static void setCacheForTest(Optional<Duration> expiry) {
Duration effectiveExpiry = expiry.orElse(getEppResourceCachingDuration());
cacheForeignKeyIndexes = createForeignKeyIndexesCache(effectiveExpiry);
}
/**

View file

@ -136,6 +136,12 @@ public final class PremiumList extends BaseDomainLabelList<Money, PremiumList.Pr
static LoadingCache<String, PremiumList> cachePremiumLists =
createCachePremiumLists(getDomainLabelListCacheDuration());
@VisibleForTesting
public static void setPremiumListCacheForTest(Optional<Duration> expiry) {
Duration effectiveExpiry = expiry.orElse(getDomainLabelListCacheDuration());
cachePremiumLists = createCachePremiumLists(effectiveExpiry);
}
@VisibleForTesting
static LoadingCache<String, PremiumList> createCachePremiumLists(Duration cachePersistDuration) {
return CacheBuilder.newBuilder()
@ -188,10 +194,16 @@ public final class PremiumList extends BaseDomainLabelList<Money, PremiumList.Pr
* that exist, as well as those that might exist according to the Bloom filter, must be cached).
* The entries judged least likely to be accessed again will be evicted first.
*/
@NonFinalForTesting @VisibleForTesting
@NonFinalForTesting
static LoadingCache<Key<PremiumListEntry>, Optional<PremiumListEntry>> cachePremiumListEntries =
createCachePremiumListEntries(getSingletonCachePersistDuration());
@VisibleForTesting
public static void setPremiumListEntriesCacheForTest(Optional<Duration> expiry) {
Duration effectiveExpiry = expiry.orElse(getSingletonCachePersistDuration());
cachePremiumListEntries = createCachePremiumListEntries(effectiveExpiry);
}
@VisibleForTesting
static LoadingCache<Key<PremiumListEntry>, Optional<PremiumListEntry>>
createCachePremiumListEntries(Duration cachePersistDuration) {