1
0
mirror of https://github.com/google/nomulus synced 2026-04-21 00:30:43 +00:00

Add retries to DriveConnection.listFiles() on GoogleJsonResponseExceptions (#2541)

* Add retries to DriveConnection.listFiles() on GoogleJsonResponseExceptions

* Remove unused import

* Remove unread variable

* Add comment inputs

* fix formatting

* Remove period from error message.
This commit is contained in:
Juan Celhay
2024-09-03 10:27:00 -04:00
committed by GitHub
parent ab5f6cc229
commit bac4e22bff
2 changed files with 79 additions and 5 deletions

View File

@@ -25,7 +25,11 @@ import static org.mockito.Mockito.times;
import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.when;
import com.google.api.client.googleapis.json.GoogleJsonError;
import com.google.api.client.googleapis.json.GoogleJsonResponseException;
import com.google.api.client.http.ByteArrayContent;
import com.google.api.client.http.HttpHeaders;
import com.google.api.client.http.HttpResponseException;
import com.google.api.services.drive.Drive;
import com.google.api.services.drive.Drive.Files;
import com.google.api.services.drive.model.File;
@@ -148,6 +152,50 @@ class DriveConnectionTest {
verify(filesList, times(2)).getPageToken();
}
@Test
void testListFiles_succeedsRetriedGoogleJsonResponseException() throws Exception {
GoogleJsonResponseException googleJsonResponseException =
new GoogleJsonResponseException(
new HttpResponseException.Builder(503, "Service Unavailable.", new HttpHeaders()),
new GoogleJsonError());
File testFile = new File().setId("testFile");
File testFile1 = new File().setId("testFile1");
when(filesList.execute())
.thenThrow(googleJsonResponseException)
.thenReturn(new FileList().setFiles(ImmutableList.of(testFile)).setNextPageToken("next"))
.thenThrow(googleJsonResponseException)
.thenReturn(new FileList().setFiles(ImmutableList.of(testFile1)));
assertThat(driveConnection.listFiles("driveFolderId"))
.containsExactly(testFile.getId(), testFile1.getId());
verify(filesList).setPageToken("next");
verify(filesList, times(1)).setQ("'driveFolderId' in parents");
verify(filesList, times(4)).getPageToken();
}
@Test
void testListFiles_throwsException_afterMaxRetriedGoogleJsonResponseException() throws Exception {
GoogleJsonResponseException googleJsonResponseException =
new GoogleJsonResponseException(
new HttpResponseException.Builder(503, "Service Unavailable.", new HttpHeaders()),
new GoogleJsonError());
when(filesList.execute()).thenThrow(googleJsonResponseException);
Exception thrown =
assertThrows(
Exception.class, () -> driveConnection.listFiles("driveFolderId", "sampleQuery"));
assertThat(thrown.getCause()).isEqualTo(googleJsonResponseException);
assertThat(thrown.getMessage())
.isEqualTo(
"Max failures reached while attempting to list Drive files in folder "
+ "driveFolderId with query sampleQuery; failing permanently.");
verify(filesList, times(0)).setPageToken(null);
verify(filesList, times(1)).setQ("'driveFolderId' in parents and sampleQuery");
verify(filesList, times(10)).getPageToken();
}
@Test
void testCreateOrUpdateFile_succeedsForNewFile() throws Exception {
when(files.create(