Simplify the CloudDnsWriter callWithRetry functional

-------------
Created by MOE: https://github.com/google/moe
MOE_MIGRATED_REVID=176512218
This commit is contained in:
guyben 2017-11-21 08:33:09 -08:00 committed by jianglai
parent 3c43ece5be
commit 6f659659ff
2 changed files with 31 additions and 37 deletions

View file

@ -50,7 +50,6 @@ import java.util.HashSet;
import java.util.List; import java.util.List;
import java.util.Optional; import java.util.Optional;
import java.util.Set; import java.util.Set;
import java.util.concurrent.Callable;
import javax.inject.Inject; import javax.inject.Inject;
import javax.inject.Named; import javax.inject.Named;
import org.joda.time.Duration; import org.joda.time.Duration;
@ -272,18 +271,18 @@ public class CloudDnsWriter extends BaseDnsWriter {
*/ */
@Override @Override
protected void commitUnchecked() { protected void commitUnchecked() {
retrier.callWithRetry( ImmutableMap<String, ImmutableSet<ResourceRecordSet>> desiredRecordsCopy =
getMutateZoneCallback(ImmutableMap.copyOf(desiredRecords)), ZoneStateException.class); ImmutableMap.copyOf(desiredRecords);
retrier.callWithRetry(() -> mutateZone(desiredRecordsCopy), ZoneStateException.class);
logger.info("Wrote to Cloud DNS"); logger.info("Wrote to Cloud DNS");
} }
/** /**
* Get a callback to mutate the zone with the provided {@code desiredRecords}. * Mutate the zone with the provided {@code desiredRecords}.
*/ */
@VisibleForTesting @VisibleForTesting
Callable<Void> getMutateZoneCallback( void mutateZone(ImmutableMap<String, ImmutableSet<ResourceRecordSet>> desiredRecords)
final ImmutableMap<String, ImmutableSet<ResourceRecordSet>> desiredRecords) { throws IOException {
return () -> {
// Fetch all existing records for names that this writer is trying to modify // Fetch all existing records for names that this writer is trying to modify
Builder<ResourceRecordSet> existingRecords = new Builder<>(); Builder<ResourceRecordSet> existingRecords = new Builder<>();
for (String domainName : desiredRecords.keySet()) { for (String domainName : desiredRecords.keySet()) {
@ -311,8 +310,6 @@ public class CloudDnsWriter extends BaseDnsWriter {
// Delete all existing records and add back the desired records // Delete all existing records and add back the desired records
updateResourceRecords(flattenedDesiredRecords.build(), existingRecords.build()); updateResourceRecords(flattenedDesiredRecords.build(), existingRecords.build());
return null;
};
} }
/** /**

View file

@ -22,6 +22,7 @@ import static google.registry.testing.DatastoreHelper.newDomainResource;
import static google.registry.testing.DatastoreHelper.newHostResource; import static google.registry.testing.DatastoreHelper.newHostResource;
import static google.registry.testing.DatastoreHelper.persistResource; import static google.registry.testing.DatastoreHelper.persistResource;
import static org.mockito.Matchers.anyString; import static org.mockito.Matchers.anyString;
import static org.mockito.Mockito.doThrow;
import static org.mockito.Mockito.spy; import static org.mockito.Mockito.spy;
import static org.mockito.Mockito.times; import static org.mockito.Mockito.times;
import static org.mockito.Mockito.verify; import static org.mockito.Mockito.verify;
@ -51,7 +52,6 @@ import java.io.IOException;
import java.net.Inet4Address; import java.net.Inet4Address;
import java.net.Inet6Address; import java.net.Inet6Address;
import java.net.InetAddress; import java.net.InetAddress;
import java.util.concurrent.Callable;
import org.joda.time.Duration; import org.joda.time.Duration;
import org.junit.Before; import org.junit.Before;
import org.junit.Rule; import org.junit.Rule;
@ -82,7 +82,6 @@ public class CloudDnsWriterTest {
@Mock private Dns.ResourceRecordSets.List listResourceRecordSetsRequest; @Mock private Dns.ResourceRecordSets.List listResourceRecordSetsRequest;
@Mock private Dns.Changes changes; @Mock private Dns.Changes changes;
@Mock private Dns.Changes.Create createChangeRequest; @Mock private Dns.Changes.Create createChangeRequest;
@Mock private Callable<Void> mutateZoneCallable;
@Captor ArgumentCaptor<String> recordNameCaptor; @Captor ArgumentCaptor<String> recordNameCaptor;
@Captor ArgumentCaptor<String> zoneNameCaptor; @Captor ArgumentCaptor<String> zoneNameCaptor;
@Captor ArgumentCaptor<Change> changeCaptor; @Captor ArgumentCaptor<Change> changeCaptor;
@ -406,13 +405,11 @@ public class CloudDnsWriterTest {
@SuppressWarnings("unchecked") @SuppressWarnings("unchecked")
public void retryMutateZoneOnError() throws Exception { public void retryMutateZoneOnError() throws Exception {
CloudDnsWriter spyWriter = spy(writer); CloudDnsWriter spyWriter = spy(writer);
when(mutateZoneCallable.call()).thenThrow(ZoneStateException.class).thenReturn(null); // First call - throw. Second call - do nothing.
when(spyWriter.getMutateZoneCallback( doThrow(ZoneStateException.class).doNothing().when(spyWriter).mutateZone(Matchers.any());
Matchers.any()))
.thenReturn(mutateZoneCallable);
spyWriter.commit(); spyWriter.commit();
verify(mutateZoneCallable, times(2)).call(); verify(spyWriter, times(2)).mutateZone(Matchers.any());
} }
@Test @Test