mirror of
https://github.com/google/nomulus.git
synced 2025-05-13 07:57:13 +02:00
Add retry logic for SheetSynchronizer
------------- Created by MOE: https://github.com/google/moe MOE_MIGRATED_REVID=138211996
This commit is contained in:
parent
ec73b5ec1c
commit
b7a98451e6
4 changed files with 87 additions and 27 deletions
|
@ -18,6 +18,7 @@ import static org.mockito.Matchers.any;
|
|||
import static org.mockito.Matchers.eq;
|
||||
import static org.mockito.Mockito.atLeastOnce;
|
||||
import static org.mockito.Mockito.mock;
|
||||
import static org.mockito.Mockito.times;
|
||||
import static org.mockito.Mockito.verify;
|
||||
import static org.mockito.Mockito.verifyNoMoreInteractions;
|
||||
import static org.mockito.Mockito.when;
|
||||
|
@ -30,7 +31,11 @@ import com.google.gdata.data.spreadsheet.ListEntry;
|
|||
import com.google.gdata.data.spreadsheet.ListFeed;
|
||||
import com.google.gdata.data.spreadsheet.SpreadsheetEntry;
|
||||
import com.google.gdata.data.spreadsheet.WorksheetEntry;
|
||||
import google.registry.testing.FakeClock;
|
||||
import google.registry.testing.FakeSleeper;
|
||||
import google.registry.util.Retrier;
|
||||
import java.net.URL;
|
||||
import org.joda.time.DateTime;
|
||||
import org.junit.After;
|
||||
import org.junit.Before;
|
||||
import org.junit.Test;
|
||||
|
@ -41,32 +46,34 @@ import org.junit.runners.JUnit4;
|
|||
@RunWith(JUnit4.class)
|
||||
public class SheetSynchronizerTest {
|
||||
|
||||
private static final int MAX_RETRIES = 3;
|
||||
|
||||
private final SpreadsheetService spreadsheetService = mock(SpreadsheetService.class);
|
||||
private final SpreadsheetEntry spreadsheet = mock(SpreadsheetEntry.class);
|
||||
private final WorksheetEntry worksheet = mock(WorksheetEntry.class);
|
||||
private final ListFeed listFeed = mock(ListFeed.class);
|
||||
private final SheetSynchronizer sheetSynchronizer = new SheetSynchronizer();
|
||||
private final FakeSleeper sleeper =
|
||||
new FakeSleeper(new FakeClock(DateTime.parse("2000-01-01TZ")));
|
||||
|
||||
@Before
|
||||
public void before() throws Exception {
|
||||
sheetSynchronizer.spreadsheetService = spreadsheetService;
|
||||
sheetSynchronizer.retrier = new Retrier(sleeper, MAX_RETRIES);
|
||||
when(spreadsheetService.getEntry(any(URL.class), eq(SpreadsheetEntry.class)))
|
||||
.thenReturn(spreadsheet);
|
||||
when(spreadsheet.getWorksheets())
|
||||
.thenReturn(ImmutableList.of(worksheet));
|
||||
when(worksheet.getListFeedUrl())
|
||||
.thenReturn(new URL("http://example.com/spreadsheet"));
|
||||
when(spreadsheetService.getFeed(any(URL.class), eq(ListFeed.class)))
|
||||
.thenReturn(listFeed);
|
||||
when(worksheet.update())
|
||||
.thenReturn(worksheet);
|
||||
when(spreadsheet.getWorksheets()).thenReturn(ImmutableList.of(worksheet));
|
||||
when(worksheet.getListFeedUrl()).thenReturn(new URL("http://example.com/spreadsheet"));
|
||||
when(spreadsheetService.getFeed(any(URL.class), eq(ListFeed.class))).thenReturn(listFeed);
|
||||
when(worksheet.update()).thenReturn(worksheet);
|
||||
}
|
||||
|
||||
@After
|
||||
public void after() throws Exception {
|
||||
verify(spreadsheetService).getEntry(
|
||||
new URL("https://spreadsheets.google.com/feeds/spreadsheets/foobar"),
|
||||
SpreadsheetEntry.class);
|
||||
verify(spreadsheetService)
|
||||
.getEntry(
|
||||
new URL("https://spreadsheets.google.com/feeds/spreadsheets/foobar"),
|
||||
SpreadsheetEntry.class);
|
||||
verify(spreadsheet).getWorksheets();
|
||||
verify(worksheet).getListFeedUrl();
|
||||
verify(spreadsheetService).getFeed(new URL("http://example.com/spreadsheet"), ListFeed.class);
|
||||
|
@ -86,8 +93,7 @@ public class SheetSynchronizerTest {
|
|||
public void testSynchronize_bothContainSameRow_doNothing() throws Exception {
|
||||
ListEntry entry = makeListEntry(ImmutableMap.of("key", "value"));
|
||||
when(listFeed.getEntries()).thenReturn(ImmutableList.of(entry));
|
||||
sheetSynchronizer.synchronize("foobar", ImmutableList.of(
|
||||
ImmutableMap.of("key", "value")));
|
||||
sheetSynchronizer.synchronize("foobar", ImmutableList.of(ImmutableMap.of("key", "value")));
|
||||
verify(worksheet).setRowCount(2);
|
||||
verify(worksheet).update();
|
||||
verify(entry, atLeastOnce()).getCustomElements();
|
||||
|
@ -98,8 +104,7 @@ public class SheetSynchronizerTest {
|
|||
public void testSynchronize_cellIsDifferent_updateRow() throws Exception {
|
||||
ListEntry entry = makeListEntry(ImmutableMap.of("key", "value"));
|
||||
when(listFeed.getEntries()).thenReturn(ImmutableList.of(entry));
|
||||
sheetSynchronizer.synchronize("foobar", ImmutableList.of(
|
||||
ImmutableMap.of("key", "new value")));
|
||||
sheetSynchronizer.synchronize("foobar", ImmutableList.of(ImmutableMap.of("key", "new value")));
|
||||
verify(entry.getCustomElements()).setValueLocal("key", "new value");
|
||||
verify(entry).update();
|
||||
verify(worksheet).setRowCount(2);
|
||||
|
@ -108,13 +113,29 @@ public class SheetSynchronizerTest {
|
|||
verifyNoMoreInteractions(entry);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testSynchronize_cellIsDifferent_updateRow_retriesOnException() throws Exception {
|
||||
ListEntry entry = makeListEntry(ImmutableMap.of("key", "value"));
|
||||
when(listFeed.getEntries()).thenReturn(ImmutableList.of(entry));
|
||||
when(entry.update())
|
||||
.thenThrow(new RuntimeException())
|
||||
.thenThrow(new RuntimeException())
|
||||
.thenReturn(entry);
|
||||
sheetSynchronizer.synchronize("foobar", ImmutableList.of(ImmutableMap.of("key", "new value")));
|
||||
verify(entry.getCustomElements()).setValueLocal("key", "new value");
|
||||
verify(entry, times(3)).update();
|
||||
verify(worksheet).setRowCount(2);
|
||||
verify(worksheet).update();
|
||||
verify(entry, atLeastOnce()).getCustomElements();
|
||||
verifyNoMoreInteractions(entry);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testSynchronize_spreadsheetMissingRow_insertRow() throws Exception {
|
||||
ListEntry entry = makeListEntry(ImmutableMap.<String, String>of());
|
||||
when(listFeed.getEntries()).thenReturn(ImmutableList.<ListEntry>of());
|
||||
when(listFeed.createEntry()).thenReturn(entry);
|
||||
sheetSynchronizer.synchronize("foobar", ImmutableList.of(
|
||||
ImmutableMap.of("key", "value")));
|
||||
sheetSynchronizer.synchronize("foobar", ImmutableList.of(ImmutableMap.of("key", "value")));
|
||||
verify(entry.getCustomElements()).setValueLocal("key", "value");
|
||||
verify(listFeed).insert(entry);
|
||||
verify(worksheet).setRowCount(2);
|
||||
|
@ -124,6 +145,26 @@ public class SheetSynchronizerTest {
|
|||
verifyNoMoreInteractions(entry);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testSynchronize_spreadsheetMissingRow_insertRow_retriesOnException()
|
||||
throws Exception {
|
||||
ListEntry entry = makeListEntry(ImmutableMap.<String, String>of());
|
||||
when(listFeed.getEntries()).thenReturn(ImmutableList.<ListEntry>of());
|
||||
when(listFeed.createEntry()).thenReturn(entry);
|
||||
when(listFeed.insert(entry))
|
||||
.thenThrow(new RuntimeException())
|
||||
.thenThrow(new RuntimeException())
|
||||
.thenReturn(entry);
|
||||
sheetSynchronizer.synchronize("foobar", ImmutableList.of(ImmutableMap.of("key", "value")));
|
||||
verify(entry.getCustomElements()).setValueLocal("key", "value");
|
||||
verify(listFeed, times(3)).insert(entry);
|
||||
verify(worksheet).setRowCount(2);
|
||||
verify(worksheet).update();
|
||||
verify(listFeed).createEntry();
|
||||
verify(entry, atLeastOnce()).getCustomElements();
|
||||
verifyNoMoreInteractions(entry);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testSynchronize_spreadsheetRowNoLongerInData_deleteRow() throws Exception {
|
||||
ListEntry entry = makeListEntry(ImmutableMap.of("key", "value"));
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue