Increment the clock in expand recurring billings events test

Also adds some clarity on when to use the non-incrementing version.

-------------
Created by MOE: https://github.com/google/moe
MOE_MIGRATED_REVID=143037508
This commit is contained in:
mcilwain 2016-12-27 09:42:20 -08:00 committed by Ben McIlwain
parent 48a1bacbb0
commit be523f7ce2
4 changed files with 86 additions and 64 deletions

View file

@ -142,6 +142,7 @@ public class DeleteContactsAndHostsActionTest
createTld("tld"); createTld("tld");
clock.advanceOneMilli(); clock.advanceOneMilli();
} }
@Test @Test
public void testSuccess_contact_referencedByActiveDomain_doesNotGetDeleted() throws Exception { public void testSuccess_contact_referencedByActiveDomain_doesNotGetDeleted() throws Exception {
ContactResource contact = persistContactPendingDelete("blah8221"); ContactResource contact = persistContactPendingDelete("blah8221");

View file

@ -38,11 +38,13 @@ import google.registry.model.billing.BillingEvent.Flag;
import google.registry.model.billing.BillingEvent.Reason; import google.registry.model.billing.BillingEvent.Reason;
import google.registry.model.common.Cursor; import google.registry.model.common.Cursor;
import google.registry.model.domain.DomainResource; import google.registry.model.domain.DomainResource;
import google.registry.model.ofy.Ofy;
import google.registry.model.registry.Registry; import google.registry.model.registry.Registry;
import google.registry.model.reporting.HistoryEntry; import google.registry.model.reporting.HistoryEntry;
import google.registry.testing.ExceptionRule; import google.registry.testing.ExceptionRule;
import google.registry.testing.FakeClock; import google.registry.testing.FakeClock;
import google.registry.testing.FakeResponse; import google.registry.testing.FakeResponse;
import google.registry.testing.InjectRule;
import google.registry.testing.mapreduce.MapreduceTestCase; import google.registry.testing.mapreduce.MapreduceTestCase;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.List; import java.util.List;
@ -62,7 +64,11 @@ public class ExpandRecurringBillingEventsActionTest
@Rule @Rule
public final ExceptionRule thrown = new ExceptionRule(); public final ExceptionRule thrown = new ExceptionRule();
final FakeClock clock = new FakeClock(DateTime.parse("2000-10-02T00:00:00Z")); @Rule
public final InjectRule inject = new InjectRule();
private final DateTime beginningOfTest = DateTime.parse("2000-10-02T00:00:00Z");
private final FakeClock clock = new FakeClock(beginningOfTest);
DomainResource domain; DomainResource domain;
HistoryEntry historyEntry; HistoryEntry historyEntry;
@ -70,6 +76,7 @@ public class ExpandRecurringBillingEventsActionTest
@Before @Before
public void init() { public void init() {
inject.setStaticField(Ofy.class, "clock", clock);
action = new ExpandRecurringBillingEventsAction(); action = new ExpandRecurringBillingEventsAction();
action.mrRunner = makeDefaultRunner(); action.mrRunner = makeDefaultRunner();
action.clock = clock; action.clock = clock;
@ -100,7 +107,7 @@ public class ExpandRecurringBillingEventsActionTest
void runMapreduce() throws Exception { void runMapreduce() throws Exception {
action.response = new FakeResponse(); action.response = new FakeResponse();
action.run(); action.run();
executeTasksUntilEmpty("mapreduce"); executeTasksUntilEmpty("mapreduce", clock);
ofy().clearSessionCache(); ofy().clearSessionCache();
} }
@ -125,12 +132,12 @@ public class ExpandRecurringBillingEventsActionTest
.setParent(historyEntry) .setParent(historyEntry)
.setPeriodYears(1) .setPeriodYears(1)
.setReason(Reason.RENEW) .setReason(Reason.RENEW)
.setSyntheticCreationTime(clock.nowUtc()) .setSyntheticCreationTime(beginningOfTest)
.setCancellationMatchingBillingEvent(Key.create(recurring)) .setCancellationMatchingBillingEvent(Key.create(recurring))
.setTargetId(domain.getFullyQualifiedDomainName()) .setTargetId(domain.getFullyQualifiedDomainName())
.build(); .build();
assertBillingEventsForResource(domain, expected, recurring); assertBillingEventsForResource(domain, expected, recurring);
assertCursorAt(clock.nowUtc()); assertCursorAt(beginningOfTest);
} }
@Test @Test
@ -148,14 +155,15 @@ public class ExpandRecurringBillingEventsActionTest
.setParent(historyEntry) .setParent(historyEntry)
.setPeriodYears(1) .setPeriodYears(1)
.setReason(Reason.RENEW) .setReason(Reason.RENEW)
.setSyntheticCreationTime(clock.nowUtc()) .setSyntheticCreationTime(beginningOfTest)
.setCancellationMatchingBillingEvent(Key.create(recurring)) .setCancellationMatchingBillingEvent(Key.create(recurring))
.setTargetId(domain.getFullyQualifiedDomainName()) .setTargetId(domain.getFullyQualifiedDomainName())
.build(); .build();
assertCursorAt(clock.nowUtc()); assertCursorAt(beginningOfTest);
DateTime beginningOfSecondRun = clock.nowUtc();
action.response = new FakeResponse(); action.response = new FakeResponse();
runMapreduce(); runMapreduce();
assertCursorAt(clock.nowUtc()); assertCursorAt(beginningOfSecondRun);
assertBillingEventsForResource(domain, expected, recurring); assertBillingEventsForResource(domain, expected, recurring);
} }
@ -172,13 +180,13 @@ public class ExpandRecurringBillingEventsActionTest
.setParent(historyEntry) .setParent(historyEntry)
.setPeriodYears(1) .setPeriodYears(1)
.setReason(Reason.RENEW) .setReason(Reason.RENEW)
.setSyntheticCreationTime(clock.nowUtc()) .setSyntheticCreationTime(beginningOfTest)
.setCancellationMatchingBillingEvent(Key.create(recurring)) .setCancellationMatchingBillingEvent(Key.create(recurring))
.setTargetId(domain.getFullyQualifiedDomainName()) .setTargetId(domain.getFullyQualifiedDomainName())
.build()); .build());
action.cursorTimeParam = Optional.of(START_OF_TIME); action.cursorTimeParam = Optional.of(START_OF_TIME);
runMapreduce(); runMapreduce();
assertCursorAt(clock.nowUtc()); assertCursorAt(beginningOfTest);
assertBillingEventsForResource(domain, persisted, recurring); // no additional billing events assertBillingEventsForResource(domain, persisted, recurring); // no additional billing events
} }
@ -195,7 +203,7 @@ public class ExpandRecurringBillingEventsActionTest
.setParent(historyEntry) .setParent(historyEntry)
.setPeriodYears(1) .setPeriodYears(1)
.setReason(Reason.RENEW) .setReason(Reason.RENEW)
.setSyntheticCreationTime(clock.nowUtc()) .setSyntheticCreationTime(beginningOfTest)
.setCancellationMatchingBillingEvent(Key.create(recurring)) .setCancellationMatchingBillingEvent(Key.create(recurring))
.setTargetId(domain.getFullyQualifiedDomainName()) .setTargetId(domain.getFullyQualifiedDomainName())
.build(); .build();
@ -206,7 +214,7 @@ public class ExpandRecurringBillingEventsActionTest
.build()); .build());
action.cursorTimeParam = Optional.of(START_OF_TIME); action.cursorTimeParam = Optional.of(START_OF_TIME);
runMapreduce(); runMapreduce();
assertCursorAt(clock.nowUtc()); assertCursorAt(beginningOfTest);
assertBillingEventsForResource(domain, persisted, expected, recurring); assertBillingEventsForResource(domain, persisted, expected, recurring);
} }
@ -226,7 +234,7 @@ public class ExpandRecurringBillingEventsActionTest
.setParent(historyEntry) .setParent(historyEntry)
.setPeriodYears(1) .setPeriodYears(1)
.setReason(Reason.RENEW) .setReason(Reason.RENEW)
.setSyntheticCreationTime(clock.nowUtc()) .setSyntheticCreationTime(beginningOfTest)
.setCancellationMatchingBillingEvent(Key.create(recurring)) .setCancellationMatchingBillingEvent(Key.create(recurring))
.setTargetId(domain.getFullyQualifiedDomainName()) .setTargetId(domain.getFullyQualifiedDomainName())
.build(); .build();
@ -236,7 +244,7 @@ public class ExpandRecurringBillingEventsActionTest
.build()); .build());
action.cursorTimeParam = Optional.of(START_OF_TIME); action.cursorTimeParam = Optional.of(START_OF_TIME);
runMapreduce(); runMapreduce();
assertCursorAt(clock.nowUtc()); assertCursorAt(beginningOfTest);
assertBillingEventsForResource(domain, persisted, expected, recurring, recurring2); assertBillingEventsForResource(domain, persisted, expected, recurring, recurring2);
} }
@ -249,7 +257,7 @@ public class ExpandRecurringBillingEventsActionTest
action.cursorTimeParam = Optional.of(DateTime.parse("2000-01-01T00:00:00Z")); action.cursorTimeParam = Optional.of(DateTime.parse("2000-01-01T00:00:00Z"));
runMapreduce(); runMapreduce();
assertBillingEventsForResource(domain, recurring); assertBillingEventsForResource(domain, recurring);
assertCursorAt(clock.nowUtc()); assertCursorAt(beginningOfTest);
} }
@Test @Test
@ -277,12 +285,12 @@ public class ExpandRecurringBillingEventsActionTest
.setParent(historyEntry) .setParent(historyEntry)
.setPeriodYears(1) .setPeriodYears(1)
.setReason(Reason.RENEW) .setReason(Reason.RENEW)
.setSyntheticCreationTime(clock.nowUtc()) .setSyntheticCreationTime(beginningOfTest)
.setCancellationMatchingBillingEvent(Key.create(recurring)) .setCancellationMatchingBillingEvent(Key.create(recurring))
.setTargetId(domain.getFullyQualifiedDomainName()) .setTargetId(domain.getFullyQualifiedDomainName())
.build(); .build();
assertBillingEventsForResource(domain, expected, recurring); assertBillingEventsForResource(domain, expected, recurring);
assertCursorAt(clock.nowUtc()); assertCursorAt(beginningOfTest);
} }
@Test @Test
@ -301,35 +309,37 @@ public class ExpandRecurringBillingEventsActionTest
.setParent(historyEntry) .setParent(historyEntry)
.setPeriodYears(1) .setPeriodYears(1)
.setReason(Reason.RENEW) .setReason(Reason.RENEW)
.setSyntheticCreationTime(clock.nowUtc()) .setSyntheticCreationTime(beginningOfTest)
.setCancellationMatchingBillingEvent(Key.create(recurring)) .setCancellationMatchingBillingEvent(Key.create(recurring))
.setTargetId(domain.getFullyQualifiedDomainName()) .setTargetId(domain.getFullyQualifiedDomainName())
.build(); .build();
assertBillingEventsForResource(domain, expected, recurring); assertBillingEventsForResource(domain, expected, recurring);
assertCursorAt(clock.nowUtc()); assertCursorAt(beginningOfTest);
} }
@Test @Test
public void testSuccess_expandSingleEvent_billingTimeAtExecutionTime() throws Exception { public void testSuccess_expandSingleEvent_billingTimeAtExecutionTime() throws Exception {
DateTime testTime = DateTime.parse("2000-02-19T00:00:00Z").minusMillis(1);
persistResource(recurring); persistResource(recurring);
action.cursorTimeParam = Optional.of(START_OF_TIME); action.cursorTimeParam = Optional.of(START_OF_TIME);
// Clock is advanced one milli in runMapreduce() // Clock is advanced one milli in runMapreduce()
clock.setTo(DateTime.parse("2000-02-19T00:00:00Z").minusMillis(1)); clock.setTo(testTime);
runMapreduce(); runMapreduce();
// A candidate billing event is set to be billed exactly on 2/19/00 @ 00:00, // A candidate billing event is set to be billed exactly on 2/19/00 @ 00:00,
// but these should not be generated as the interval is closed on cursorTime, open on // but these should not be generated as the interval is closed on cursorTime, open on
// executeTime. // executeTime.
assertBillingEventsForResource(domain, recurring); assertBillingEventsForResource(domain, recurring);
assertCursorAt(clock.nowUtc()); assertCursorAt(testTime);
} }
@Test @Test
public void testSuccess_expandSingleEvent_multipleYearCreate() throws Exception { public void testSuccess_expandSingleEvent_multipleYearCreate() throws Exception {
DateTime testTime = beginningOfTest.plusYears(2);
action.cursorTimeParam = Optional.of(recurring.getEventTime()); action.cursorTimeParam = Optional.of(recurring.getEventTime());
recurring = persistResource(recurring.asBuilder() recurring =
.setEventTime(recurring.getEventTime().plusYears(2)) persistResource(
.build()); recurring.asBuilder().setEventTime(recurring.getEventTime().plusYears(2)).build());
clock.setTo(clock.nowUtc().plusYears(2)); clock.setTo(testTime);
runMapreduce(); runMapreduce();
BillingEvent.OneTime expected = new BillingEvent.OneTime.Builder() BillingEvent.OneTime expected = new BillingEvent.OneTime.Builder()
// Default renew grace period of 45 days. // Default renew grace period of 45 days.
@ -341,12 +351,12 @@ public class ExpandRecurringBillingEventsActionTest
.setParent(historyEntry) .setParent(historyEntry)
.setPeriodYears(1) .setPeriodYears(1)
.setReason(Reason.RENEW) .setReason(Reason.RENEW)
.setSyntheticCreationTime(clock.nowUtc()) .setSyntheticCreationTime(testTime)
.setCancellationMatchingBillingEvent(Key.create(recurring)) .setCancellationMatchingBillingEvent(Key.create(recurring))
.setTargetId(domain.getFullyQualifiedDomainName()) .setTargetId(domain.getFullyQualifiedDomainName())
.build(); .build();
assertBillingEventsForResource(domain, expected, recurring); assertBillingEventsForResource(domain, expected, recurring);
assertCursorAt(clock.nowUtc()); assertCursorAt(testTime);
} }
@Test @Test
@ -364,12 +374,12 @@ public class ExpandRecurringBillingEventsActionTest
.setParent(historyEntry) .setParent(historyEntry)
.setPeriodYears(1) .setPeriodYears(1)
.setReason(Reason.RENEW) .setReason(Reason.RENEW)
.setSyntheticCreationTime(clock.nowUtc()) .setSyntheticCreationTime(beginningOfTest)
.setCancellationMatchingBillingEvent(Key.create(recurring)) .setCancellationMatchingBillingEvent(Key.create(recurring))
.setTargetId(domain.getFullyQualifiedDomainName()) .setTargetId(domain.getFullyQualifiedDomainName())
.build(); .build();
assertBillingEventsForResource(domain, expected, recurring); assertBillingEventsForResource(domain, expected, recurring);
assertCursorAt(clock.nowUtc()); assertCursorAt(beginningOfTest);
} }
@Test @Test
@ -379,7 +389,7 @@ public class ExpandRecurringBillingEventsActionTest
saveCursor(clock.nowUtc().minusSeconds(1)); saveCursor(clock.nowUtc().minusSeconds(1));
runMapreduce(); runMapreduce();
assertBillingEventsForResource(domain, recurring); assertBillingEventsForResource(domain, recurring);
assertCursorAt(clock.nowUtc()); assertCursorAt(beginningOfTest);
} }
@Test @Test
@ -394,7 +404,8 @@ public class ExpandRecurringBillingEventsActionTest
@Test @Test
public void testSuccess_expandSingleEvent_multipleYears() throws Exception { public void testSuccess_expandSingleEvent_multipleYears() throws Exception {
clock.setTo(clock.nowUtc().plusYears(5)); DateTime testTime = clock.nowUtc().plusYears(5);
clock.setTo(testTime);
List<BillingEvent> expectedEvents = new ArrayList<>(); List<BillingEvent> expectedEvents = new ArrayList<>();
expectedEvents.add(persistResource(recurring)); expectedEvents.add(persistResource(recurring));
action.cursorTimeParam = Optional.of(START_OF_TIME); action.cursorTimeParam = Optional.of(START_OF_TIME);
@ -413,18 +424,19 @@ public class ExpandRecurringBillingEventsActionTest
.setParent(historyEntry) .setParent(historyEntry)
.setPeriodYears(1) .setPeriodYears(1)
.setReason(Reason.RENEW) .setReason(Reason.RENEW)
.setSyntheticCreationTime(clock.nowUtc()) .setSyntheticCreationTime(testTime)
.setCancellationMatchingBillingEvent(Key.create(recurring)) .setCancellationMatchingBillingEvent(Key.create(recurring))
.setTargetId(domain.getFullyQualifiedDomainName()) .setTargetId(domain.getFullyQualifiedDomainName())
.build()); .build());
} }
assertBillingEventsForResource(domain, Iterables.toArray(expectedEvents, BillingEvent.class)); assertBillingEventsForResource(domain, Iterables.toArray(expectedEvents, BillingEvent.class));
assertCursorAt(clock.nowUtc()); assertCursorAt(testTime);
} }
@Test @Test
public void testSuccess_expandSingleEvent_multipleYears_cursorInBetweenYears() throws Exception { public void testSuccess_expandSingleEvent_multipleYears_cursorInBetweenYears() throws Exception {
clock.setTo(clock.nowUtc().plusYears(5)); DateTime testTime = clock.nowUtc().plusYears(5);
clock.setTo(testTime);
List<BillingEvent> expectedEvents = new ArrayList<>(); List<BillingEvent> expectedEvents = new ArrayList<>();
expectedEvents.add(persistResource(recurring)); expectedEvents.add(persistResource(recurring));
saveCursor(DateTime.parse("2003-10-02T00:00:00Z")); saveCursor(DateTime.parse("2003-10-02T00:00:00Z"));
@ -443,28 +455,30 @@ public class ExpandRecurringBillingEventsActionTest
.setParent(historyEntry) .setParent(historyEntry)
.setPeriodYears(1) .setPeriodYears(1)
.setReason(Reason.RENEW) .setReason(Reason.RENEW)
.setSyntheticCreationTime(clock.nowUtc()) .setSyntheticCreationTime(testTime)
.setCancellationMatchingBillingEvent(Key.create(recurring)) .setCancellationMatchingBillingEvent(Key.create(recurring))
.setTargetId(domain.getFullyQualifiedDomainName()) .setTargetId(domain.getFullyQualifiedDomainName())
.build()); .build());
} }
assertBillingEventsForResource(domain, Iterables.toArray(expectedEvents, BillingEvent.class)); assertBillingEventsForResource(domain, Iterables.toArray(expectedEvents, BillingEvent.class));
assertCursorAt(clock.nowUtc()); assertCursorAt(testTime);
} }
@Test @Test
public void testSuccess_singleEvent_beforeRenewal() throws Exception { public void testSuccess_singleEvent_beforeRenewal() throws Exception {
clock.setTo(DateTime.parse("2000-01-04T00:00:00Z")); DateTime testTime = DateTime.parse("2000-01-04T00:00:00Z");
clock.setTo(testTime);
persistResource(recurring); persistResource(recurring);
action.cursorTimeParam = Optional.of(START_OF_TIME); action.cursorTimeParam = Optional.of(START_OF_TIME);
runMapreduce(); runMapreduce();
assertBillingEventsForResource(domain, recurring); assertBillingEventsForResource(domain, recurring);
assertCursorAt(clock.nowUtc()); assertCursorAt(testTime);
} }
@Test @Test
public void testSuccess_singleEvent_afterRecurrenceEnd() throws Exception { public void testSuccess_singleEvent_afterRecurrenceEnd() throws Exception {
clock.setTo(clock.nowUtc().plusYears(2)); DateTime testTime = beginningOfTest.plusYears(2);
clock.setTo(testTime);
recurring = persistResource(recurring.asBuilder() recurring = persistResource(recurring.asBuilder()
// Set between event time and billing time (i.e. before the grace period expires) for 2000. // Set between event time and billing time (i.e. before the grace period expires) for 2000.
// We should still expect a billing event. // We should still expect a billing event.
@ -481,19 +495,19 @@ public class ExpandRecurringBillingEventsActionTest
.setParent(historyEntry) .setParent(historyEntry)
.setPeriodYears(1) .setPeriodYears(1)
.setReason(Reason.RENEW) .setReason(Reason.RENEW)
.setSyntheticCreationTime(clock.nowUtc()) .setSyntheticCreationTime(testTime)
.setCancellationMatchingBillingEvent(Key.create(recurring)) .setCancellationMatchingBillingEvent(Key.create(recurring))
.setTargetId(domain.getFullyQualifiedDomainName()) .setTargetId(domain.getFullyQualifiedDomainName())
.build(); .build();
assertBillingEventsForResource(domain, recurring, expected); assertBillingEventsForResource(domain, recurring, expected);
assertCursorAt(clock.nowUtc()); assertCursorAt(testTime);
} }
@Test @Test
public void testSuccess_expandSingleEvent_billingTimeOnLeapYear() throws Exception { public void testSuccess_expandSingleEvent_billingTimeOnLeapYear() throws Exception {
recurring = persistResource(recurring.asBuilder() recurring =
.setEventTime(DateTime.parse("2000-01-15T00:00:00Z")) persistResource(
.build()); recurring.asBuilder().setEventTime(DateTime.parse("2000-01-15T00:00:00Z")).build());
action.cursorTimeParam = Optional.of(START_OF_TIME); action.cursorTimeParam = Optional.of(START_OF_TIME);
runMapreduce(); runMapreduce();
BillingEvent.OneTime expected = new BillingEvent.OneTime.Builder() BillingEvent.OneTime expected = new BillingEvent.OneTime.Builder()
@ -506,38 +520,39 @@ public class ExpandRecurringBillingEventsActionTest
.setParent(historyEntry) .setParent(historyEntry)
.setPeriodYears(1) .setPeriodYears(1)
.setReason(Reason.RENEW) .setReason(Reason.RENEW)
.setSyntheticCreationTime(clock.nowUtc()) .setSyntheticCreationTime(beginningOfTest)
.setCancellationMatchingBillingEvent(Key.create(recurring)) .setCancellationMatchingBillingEvent(Key.create(recurring))
.setTargetId(domain.getFullyQualifiedDomainName()) .setTargetId(domain.getFullyQualifiedDomainName())
.build(); .build();
assertBillingEventsForResource(domain, expected, recurring); assertBillingEventsForResource(domain, expected, recurring);
assertCursorAt(clock.nowUtc()); assertCursorAt(beginningOfTest);
} }
@Test @Test
public void testSuccess_expandSingleEvent_billingTimeNotOnLeapYear() throws Exception { public void testSuccess_expandSingleEvent_billingTimeNotOnLeapYear() throws Exception {
recurring = persistResource(recurring.asBuilder() DateTime testTime = DateTime.parse("2001-12-01T00:00:00Z");
.setEventTime(DateTime.parse("1999-01-15T00:00:00Z")) recurring =
.build()); persistResource(
recurring.asBuilder().setEventTime(DateTime.parse("2001-01-15T00:00:00Z")).build());
action.cursorTimeParam = Optional.of(START_OF_TIME); action.cursorTimeParam = Optional.of(START_OF_TIME);
clock.setTo(DateTime.parse("1999-12-01T00:00:00Z")); clock.setTo(testTime);
runMapreduce(); runMapreduce();
BillingEvent.OneTime expected = new BillingEvent.OneTime.Builder() BillingEvent.OneTime expected = new BillingEvent.OneTime.Builder()
// Default renew grace period of 45 days. // Default renew grace period of 45 days.
.setBillingTime(DateTime.parse("1999-03-01T00:00:00Z")) .setBillingTime(DateTime.parse("2001-03-01T00:00:00Z"))
.setClientId("TheRegistrar") .setClientId("TheRegistrar")
.setCost(Money.of(USD, 11)) .setCost(Money.of(USD, 11))
.setEventTime(DateTime.parse("1999-01-15T00:00:00Z")) .setEventTime(DateTime.parse("2001-01-15T00:00:00Z"))
.setFlags(ImmutableSet.of(Flag.AUTO_RENEW, Flag.SYNTHETIC)) .setFlags(ImmutableSet.of(Flag.AUTO_RENEW, Flag.SYNTHETIC))
.setParent(historyEntry) .setParent(historyEntry)
.setPeriodYears(1) .setPeriodYears(1)
.setReason(Reason.RENEW) .setReason(Reason.RENEW)
.setSyntheticCreationTime(clock.nowUtc()) .setSyntheticCreationTime(testTime)
.setCancellationMatchingBillingEvent(Key.create(recurring)) .setCancellationMatchingBillingEvent(Key.create(recurring))
.setTargetId(domain.getFullyQualifiedDomainName()) .setTargetId(domain.getFullyQualifiedDomainName())
.build(); .build();
assertBillingEventsForResource(domain, expected, recurring); assertBillingEventsForResource(domain, expected, recurring);
assertCursorAt(clock.nowUtc()); assertCursorAt(testTime);
} }
@Test @Test
@ -559,7 +574,7 @@ public class ExpandRecurringBillingEventsActionTest
.setParent(historyEntry) .setParent(historyEntry)
.setPeriodYears(1) .setPeriodYears(1)
.setReason(Reason.RENEW) .setReason(Reason.RENEW)
.setSyntheticCreationTime(clock.nowUtc()) .setSyntheticCreationTime(beginningOfTest)
.setCancellationMatchingBillingEvent(Key.create(recurring)) .setCancellationMatchingBillingEvent(Key.create(recurring))
.setTargetId(domain.getFullyQualifiedDomainName()) .setTargetId(domain.getFullyQualifiedDomainName())
.build(); .build();
@ -573,12 +588,12 @@ public class ExpandRecurringBillingEventsActionTest
.setParent(historyEntry) .setParent(historyEntry)
.setPeriodYears(1) .setPeriodYears(1)
.setReason(Reason.RENEW) .setReason(Reason.RENEW)
.setSyntheticCreationTime(clock.nowUtc()) .setSyntheticCreationTime(beginningOfTest)
.setCancellationMatchingBillingEvent(Key.create(recurring2)) .setCancellationMatchingBillingEvent(Key.create(recurring2))
.setTargetId(domain.getFullyQualifiedDomainName()) .setTargetId(domain.getFullyQualifiedDomainName())
.build(); .build();
assertBillingEventsForResource(domain, expected, expected2, recurring, recurring2); assertBillingEventsForResource(domain, expected, expected2, recurring, recurring2);
assertCursorAt(clock.nowUtc()); assertCursorAt(beginningOfTest);
} }
@Test @Test
@ -601,16 +616,17 @@ public class ExpandRecurringBillingEventsActionTest
.setParent(historyEntry) .setParent(historyEntry)
.setPeriodYears(1) .setPeriodYears(1)
.setReason(Reason.RENEW) .setReason(Reason.RENEW)
.setSyntheticCreationTime(clock.nowUtc()) .setSyntheticCreationTime(beginningOfTest)
.setCancellationMatchingBillingEvent(Key.create(recurring)) .setCancellationMatchingBillingEvent(Key.create(recurring))
.setTargetId(domain.getFullyQualifiedDomainName()) .setTargetId(domain.getFullyQualifiedDomainName())
.build(); .build();
assertBillingEventsForResource(domain, expected, recurring); assertBillingEventsForResource(domain, expected, recurring);
assertCursorAt(clock.nowUtc()); assertCursorAt(beginningOfTest);
} }
@Test @Test
public void testSuccess_varyingRenewPrices() throws Exception { public void testSuccess_varyingRenewPrices() throws Exception {
DateTime testTime = beginningOfTest.plusYears(1);
persistResource( persistResource(
Registry.get("tld") Registry.get("tld")
.asBuilder() .asBuilder()
@ -619,7 +635,7 @@ public class ExpandRecurringBillingEventsActionTest
START_OF_TIME, Money.of(USD, 8), START_OF_TIME, Money.of(USD, 8),
DateTime.parse("2000-06-01T00:00:00Z"), Money.of(USD, 10))) DateTime.parse("2000-06-01T00:00:00Z"), Money.of(USD, 10)))
.build()); .build());
clock.setTo(clock.nowUtc().plusYears(1)); clock.setTo(testTime);
persistResource(recurring); persistResource(recurring);
action.cursorTimeParam = Optional.of(START_OF_TIME); action.cursorTimeParam = Optional.of(START_OF_TIME);
runMapreduce(); runMapreduce();
@ -635,7 +651,7 @@ public class ExpandRecurringBillingEventsActionTest
.setParent(historyEntry) .setParent(historyEntry)
.setPeriodYears(1) .setPeriodYears(1)
.setReason(Reason.RENEW) .setReason(Reason.RENEW)
.setSyntheticCreationTime(clock.nowUtc()) .setSyntheticCreationTime(testTime)
.setCancellationMatchingBillingEvent(Key.create(recurring)) .setCancellationMatchingBillingEvent(Key.create(recurring))
.setTargetId(domain.getFullyQualifiedDomainName()) .setTargetId(domain.getFullyQualifiedDomainName())
.build(); .build();
@ -645,7 +661,7 @@ public class ExpandRecurringBillingEventsActionTest
.setEventTime(eventDate.plusYears(1)) .setEventTime(eventDate.plusYears(1))
.build(); .build();
assertBillingEventsForResource(domain, recurring, cheaper, expensive); assertBillingEventsForResource(domain, recurring, cheaper, expensive);
assertCursorAt(clock.nowUtc()); assertCursorAt(testTime);
} }
@Test @Test

View file

@ -643,7 +643,6 @@ public class DatastoreHelper {
FluentIterable.from(asList(expected)).transform(BILLING_EVENT_ID_STRIPPER)); FluentIterable.from(asList(expected)).transform(BILLING_EVENT_ID_STRIPPER));
} }
/** Assert that there are no billing events. */ /** Assert that there are no billing events. */
public static void assertNoBillingEvents() { public static void assertNoBillingEvents() {
assertThat(getBillingEvents()).isEmpty(); assertThat(getBillingEvents()).isEmpty();

View file

@ -158,6 +158,12 @@ public abstract class MapreduceTestCase<T> extends ShardableTestCase {
} }
} }
/**
* Executes tasks in the mapreduce queue until all are finished.
*
* <p>If you are mocking a clock in your tests, use the
* {@link #executeTasksUntilEmpty(String, FakeClock)} version instead.
*/
protected void executeTasksUntilEmpty(String queueName) throws Exception { protected void executeTasksUntilEmpty(String queueName) throws Exception {
executeTasksUntilEmpty(queueName, null); executeTasksUntilEmpty(queueName, null);
} }
@ -168,8 +174,8 @@ public abstract class MapreduceTestCase<T> extends ShardableTestCase {
* <p>Incrementing the clock between tasks is important if tasks have transactions inside the * <p>Incrementing the clock between tasks is important if tasks have transactions inside the
* mapper or reducer, which don't have access to the fake clock. * mapper or reducer, which don't have access to the fake clock.
*/ */
protected void protected void executeTasksUntilEmpty(String queueName, @Nullable FakeClock clock)
executeTasksUntilEmpty(String queueName, @Nullable FakeClock clock) throws Exception { throws Exception {
while (true) { while (true) {
ofy().clearSessionCache(); ofy().clearSessionCache();
// We have to re-acquire task list every time, because local implementation returns a copy. // We have to re-acquire task list every time, because local implementation returns a copy.