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

@ -33,6 +33,7 @@ import google.registry.testing.AppEngineRule;
import google.registry.testing.FakeClock;
import google.registry.testing.FakeSleeper;
import org.joda.time.DateTime;
import org.junit.After;
import org.junit.Before;
import org.junit.Rule;
import org.junit.Test;
@ -49,6 +50,8 @@ public final class TaskQueueUtilsTest {
public final AppEngineRule appEngine =
AppEngineRule.builder().withDatastore().withTaskQueue().build();
private int origBatchSize;
private final FakeClock clock = new FakeClock(DateTime.parse("2000-01-01TZ"));
private final FakeSleeper sleeper = new FakeSleeper(clock);
private final TaskQueueUtils taskQueueUtils =
@ -59,9 +62,15 @@ public final class TaskQueueUtilsTest {
@Before
public void before() {
origBatchSize = TaskQueueUtils.BATCH_SIZE;
TaskQueueUtils.BATCH_SIZE = 2;
}
@After
public void after() {
TaskQueueUtils.BATCH_SIZE = origBatchSize;
}
@Test
public void testEnqueue_worksOnFirstTry_doesntSleep() {
when(queue.add(ImmutableList.of(task))).thenReturn(ImmutableList.of(handle));