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

@ -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);