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

@ -20,21 +20,25 @@ import static google.registry.model.EppResourceUtils.loadByForeignKey;
import static google.registry.testing.DatastoreHelper.persistActiveContact;
import static google.registry.testing.DatastoreHelper.persistActiveHost;
import static google.registry.testing.DatastoreHelper.persistResource;
import static java.util.concurrent.TimeUnit.DAYS;
import com.google.common.cache.CacheBuilder;
import com.google.common.collect.ImmutableList;
import com.googlecode.objectify.Key;
import google.registry.model.contact.ContactResource;
import google.registry.model.host.HostResource;
import google.registry.testing.TestCacheRule;
import org.joda.time.Duration;
import org.junit.Rule;
import org.junit.Test;
/** Unit tests for {@link EppResource}. */
public class EppResourceTest extends EntityTestCase {
@Rule
public final TestCacheRule testCacheRule =
new TestCacheRule.Builder().withEppResourceCache(Duration.standardDays(1)).build();
@Test
public void test_loadCached_ignoresContactChange() {
setNonZeroCachingInterval();
ContactResource originalContact = persistActiveContact("contact123");
assertThat(EppResource.loadCached(ImmutableList.of(Key.create(originalContact))))
.containsExactly(Key.create(originalContact), originalContact);
@ -48,7 +52,6 @@ public class EppResourceTest extends EntityTestCase {
@Test
public void test_loadCached_ignoresHostChange() {
setNonZeroCachingInterval();
HostResource originalHost = persistActiveHost("ns1.example.com");
assertThat(EppResource.loadCached(ImmutableList.of(Key.create(originalHost))))
.containsExactly(Key.create(originalHost), originalHost);
@ -60,8 +63,4 @@ public class EppResourceTest extends EntityTestCase {
assertThat(loadByForeignKey(HostResource.class, "ns1.example.com", clock.nowUtc()))
.hasValue(modifiedHost);
}
private static void setNonZeroCachingInterval() {
EppResource.setCacheForTest(CacheBuilder.newBuilder().expireAfterWrite(1L, DAYS));
}
}

View file

@ -25,21 +25,26 @@ import static google.registry.testing.DatastoreHelper.persistActiveHost;
import static google.registry.testing.DatastoreHelper.persistDeletedHost;
import static google.registry.testing.DatastoreHelper.persistResource;
import static google.registry.util.DateTimeUtils.END_OF_TIME;
import static java.util.concurrent.TimeUnit.DAYS;
import com.google.common.cache.CacheBuilder;
import com.google.common.collect.ImmutableList;
import com.googlecode.objectify.Key;
import google.registry.model.EntityTestCase;
import google.registry.model.contact.ContactResource;
import google.registry.model.host.HostResource;
import google.registry.model.index.ForeignKeyIndex.ForeignKeyHostIndex;
import google.registry.testing.TestCacheRule;
import org.joda.time.Duration;
import org.junit.Before;
import org.junit.Rule;
import org.junit.Test;
/** Unit tests for {@link ForeignKeyIndex}. */
public class ForeignKeyIndexTest extends EntityTestCase {
@Rule
public final TestCacheRule testCacheRule =
new TestCacheRule.Builder().withForeignIndexKeyCache(Duration.standardDays(1)).build();
@Before
public void setUp() {
createTld("com");
@ -120,7 +125,6 @@ public class ForeignKeyIndexTest extends EntityTestCase {
@Test
public void test_loadCached_cachesNonexistenceOfHosts() {
setNonZeroCachingInterval();
assertThat(
ForeignKeyIndex.loadCached(
HostResource.class,
@ -141,7 +145,6 @@ public class ForeignKeyIndexTest extends EntityTestCase {
@Test
public void test_loadCached_cachesExistenceOfHosts() {
setNonZeroCachingInterval();
HostResource host1 = persistActiveHost("ns1.example.com");
HostResource host2 = persistActiveHost("ns2.example.com");
assertThat(
@ -149,8 +152,11 @@ public class ForeignKeyIndexTest extends EntityTestCase {
HostResource.class,
ImmutableList.of("ns1.example.com", "ns2.example.com"),
clock.nowUtc()))
.containsExactly("ns1.example.com", loadHostFki("ns1.example.com"),
"ns2.example.com", loadHostFki("ns2.example.com"));
.containsExactly(
"ns1.example.com",
loadHostFki("ns1.example.com"),
"ns2.example.com",
loadHostFki("ns2.example.com"));
deleteResource(host1);
deleteResource(host2);
persistActiveHost("ns3.example.com");
@ -167,7 +173,6 @@ public class ForeignKeyIndexTest extends EntityTestCase {
@Test
public void test_loadCached_doesntSeeHostChangesWhileCacheIsValid() {
setNonZeroCachingInterval();
HostResource originalHost = persistActiveHost("ns1.example.com");
ForeignKeyIndex<HostResource> originalFki = loadHostFki("ns1.example.com");
clock.advanceOneMilli();
@ -191,7 +196,6 @@ public class ForeignKeyIndexTest extends EntityTestCase {
@Test
public void test_loadCached_filtersOutSoftDeletedHosts() {
setNonZeroCachingInterval();
persistActiveHost("ns1.example.com");
persistDeletedHost("ns2.example.com", clock.nowUtc().minusDays(1));
assertThat(
@ -204,7 +208,6 @@ public class ForeignKeyIndexTest extends EntityTestCase {
@Test
public void test_loadCached_cachesContactFkis() {
setNonZeroCachingInterval();
persistActiveContact("contactid1");
ForeignKeyIndex<ContactResource> fki1 = loadContactFki("contactid1");
assertThat(
@ -228,8 +231,4 @@ public class ForeignKeyIndexTest extends EntityTestCase {
clock.nowUtc()))
.containsExactly("contactid2", loadContactFki("contactid2"));
}
private static void setNonZeroCachingInterval() {
ForeignKeyIndex.setCacheForTest(CacheBuilder.newBuilder().expireAfterWrite(1L, DAYS));
}
}

View file

@ -26,7 +26,6 @@ import static google.registry.model.registry.label.DomainLabelMetrics.PremiumLis
import static google.registry.model.registry.label.DomainLabelMetrics.PremiumListCheckOutcome.UNCACHED_POSITIVE;
import static google.registry.model.registry.label.DomainLabelMetrics.premiumListChecks;
import static google.registry.model.registry.label.DomainLabelMetrics.premiumListProcessingTime;
import static google.registry.model.registry.label.PremiumList.createCachePremiumLists;
import static google.registry.model.registry.label.PremiumListUtils.deletePremiumList;
import static google.registry.model.registry.label.PremiumListUtils.doesPremiumListExist;
import static google.registry.model.registry.label.PremiumListUtils.getPremiumPrice;
@ -48,6 +47,7 @@ import google.registry.model.registry.Registry;
import google.registry.model.registry.label.PremiumList.PremiumListEntry;
import google.registry.model.registry.label.PremiumList.PremiumListRevision;
import google.registry.testing.AppEngineRule;
import google.registry.testing.TestCacheRule;
import java.util.Map;
import org.joda.money.Money;
import org.junit.Before;
@ -62,13 +62,17 @@ public class PremiumListUtilsTest {
@Rule public final AppEngineRule appEngine = AppEngineRule.builder().withDatastore().build();
// Set long persist times on caches so they can be tested (cache times default to 0 in tests).
@Rule
public final TestCacheRule testCacheRule =
new TestCacheRule.Builder()
.withPremiumListsCache(standardDays(1))
.withPremiumListEntriesCache(standardDays(1))
.build();
@Before
public void before() {
// Set long persist times on caches so they can be tested (cache times default to 0 in tests).
PremiumList.cachePremiumListEntries =
PremiumList.createCachePremiumListEntries(standardDays(1));
PremiumList.cachePremiumLists = createCachePremiumLists(standardDays(1));
// createTld() overwrites the premium list, so call it first.
// createTld() overwrites the premium list, so call it before persisting pl.
createTld("tld");
PremiumList pl =
persistPremiumList(

View file

@ -34,6 +34,7 @@ import google.registry.testing.InjectRule;
import google.registry.util.RequestStatusChecker;
import java.util.Optional;
import org.joda.time.Duration;
import org.junit.After;
import org.junit.Before;
import org.junit.Rule;
import org.junit.Test;
@ -50,6 +51,8 @@ public class LockTest {
private static final RequestStatusChecker requestStatusChecker = mock(RequestStatusChecker.class);
private static final FakeClock clock = new FakeClock();
private LockMetrics origLockMetrics;
@Rule public final AppEngineRule appEngine = AppEngineRule.builder().withDatastore().build();
@Rule public final InjectRule inject = new InjectRule();
@ -73,11 +76,17 @@ public class LockTest {
@Before public void setUp() {
inject.setStaticField(Ofy.class, "clock", clock);
origLockMetrics = Lock.lockMetrics;
Lock.lockMetrics = null;
when(requestStatusChecker.getLogId()).thenReturn("current-request-id");
when(requestStatusChecker.isRunning("current-request-id")).thenReturn(true);
}
@After
public void restoreLockMetric() {
Lock.lockMetrics = origLockMetrics;
}
@Test
public void testReleasedExplicitly() {
Optional<Lock> lock = acquire("", ONE_DAY, FREE);