|
41 | 41 | import com.google.api.client.http.HttpHeaders; |
42 | 42 | import com.google.api.client.http.HttpResponseException; |
43 | 43 | import com.google.api.gax.paging.Page; |
| 44 | +import com.google.api.gax.retrying.ResultRetryAlgorithm; |
| 45 | +import com.google.api.gax.retrying.TimedAttemptSettings; |
44 | 46 | import com.google.api.services.bigquery.model.ErrorProto; |
45 | 47 | import com.google.api.services.bigquery.model.GetQueryResultsResponse; |
46 | 48 | import com.google.api.services.bigquery.model.JobConfigurationQuery; |
|
76 | 78 | import java.util.Collections; |
77 | 79 | import java.util.List; |
78 | 80 | import java.util.Map; |
| 81 | +import java.util.concurrent.atomic.AtomicReference; |
79 | 82 | import org.junit.jupiter.api.Assertions; |
80 | 83 | import org.junit.jupiter.api.BeforeEach; |
81 | 84 | import org.junit.jupiter.api.Test; |
@@ -941,31 +944,80 @@ void testGetTable() throws IOException { |
941 | 944 |
|
942 | 945 | @Test |
943 | 946 | void testGetTableFailureShouldRetryServerErrors() throws IOException { |
944 | | - GoogleJsonError error = new GoogleJsonError(); |
945 | | - error.setMessage("Visibility check was unavailable. Please retry the request"); |
946 | | - error.setCode(503); |
947 | | - GoogleJsonError.ErrorInfo errorInfo = new GoogleJsonError.ErrorInfo(); |
948 | | - errorInfo.setReason("backendError"); |
949 | | - error.setErrors(ImmutableList.of(errorInfo)); |
| 947 | + when(bigqueryRpcMock.getTableSkipExceptionTranslation( |
| 948 | + PROJECT, DATASET, TABLE, EMPTY_RPC_OPTIONS)) |
| 949 | + .thenThrow(serviceUnavailableException()) |
| 950 | + .thenReturn(TABLE_INFO_WITH_PROJECT.toPb()); |
| 951 | + |
| 952 | + bigquery = |
| 953 | + options.toBuilder() |
| 954 | + .setRetrySettings(ServiceOptions.getDefaultRetrySettings()) |
| 955 | + .build() |
| 956 | + .getService(); |
| 957 | + |
| 958 | + Table table = bigquery.getTable(DATASET, TABLE); |
| 959 | + |
| 960 | + assertEquals(new Table(bigquery, new TableInfo.BuilderImpl(TABLE_INFO_WITH_PROJECT)), table); |
| 961 | + verify(bigqueryRpcMock, times(2)) |
| 962 | + .getTableSkipExceptionTranslation(PROJECT, DATASET, TABLE, EMPTY_RPC_OPTIONS); |
| 963 | + } |
| 964 | + |
| 965 | + @Test |
| 966 | + void testGetTableFailureUsesCustomRetryAlgorithm() throws IOException { |
| 967 | + AtomicReference<Throwable> retryThrowable = new AtomicReference<>(); |
| 968 | + ResultRetryAlgorithm<Object> retryAlgorithm = |
| 969 | + new ResultRetryAlgorithm<Object>() { |
| 970 | + @Override |
| 971 | + public TimedAttemptSettings createNextAttempt( |
| 972 | + Throwable previousThrowable, |
| 973 | + Object previousResponse, |
| 974 | + TimedAttemptSettings previousSettings) { |
| 975 | + if (previousThrowable != null) { |
| 976 | + retryThrowable.set(previousThrowable); |
| 977 | + } |
| 978 | + return null; |
| 979 | + } |
| 980 | + |
| 981 | + @Override |
| 982 | + public boolean shouldRetry(Throwable previousThrowable, Object previousResponse) { |
| 983 | + if (previousThrowable != null) { |
| 984 | + retryThrowable.set(previousThrowable); |
| 985 | + } |
| 986 | + return previousThrowable instanceof HttpResponseException; |
| 987 | + } |
| 988 | + }; |
950 | 989 |
|
951 | 990 | when(bigqueryRpcMock.getTableSkipExceptionTranslation( |
952 | 991 | PROJECT, DATASET, TABLE, EMPTY_RPC_OPTIONS)) |
953 | | - .thenThrow(new GoogleJsonResponseException(serverErrorResponse(), error)) |
| 992 | + .thenThrow(serviceUnavailableException()) |
954 | 993 | .thenReturn(TABLE_INFO_WITH_PROJECT.toPb()); |
955 | 994 |
|
956 | 995 | bigquery = |
957 | 996 | options.toBuilder() |
958 | 997 | .setRetrySettings(ServiceOptions.getDefaultRetrySettings()) |
| 998 | + .setResultRetryAlgorithm(retryAlgorithm) |
959 | 999 | .build() |
960 | 1000 | .getService(); |
961 | 1001 |
|
| 1002 | + assertSame(retryAlgorithm, bigquery.getOptions().getResultRetryAlgorithm()); |
962 | 1003 | Table table = bigquery.getTable(DATASET, TABLE); |
963 | 1004 |
|
964 | 1005 | assertEquals(new Table(bigquery, new TableInfo.BuilderImpl(TABLE_INFO_WITH_PROJECT)), table); |
| 1006 | + assertThat(retryThrowable.get()).isInstanceOf(HttpResponseException.class); |
965 | 1007 | verify(bigqueryRpcMock, times(2)) |
966 | 1008 | .getTableSkipExceptionTranslation(PROJECT, DATASET, TABLE, EMPTY_RPC_OPTIONS); |
967 | 1009 | } |
968 | 1010 |
|
| 1011 | + private static GoogleJsonResponseException serviceUnavailableException() { |
| 1012 | + GoogleJsonError error = new GoogleJsonError(); |
| 1013 | + error.setMessage("Visibility check was unavailable. Please retry the request"); |
| 1014 | + error.setCode(503); |
| 1015 | + GoogleJsonError.ErrorInfo errorInfo = new GoogleJsonError.ErrorInfo(); |
| 1016 | + errorInfo.setReason("backendError"); |
| 1017 | + error.setErrors(ImmutableList.of(errorInfo)); |
| 1018 | + return new GoogleJsonResponseException(serverErrorResponse(), error); |
| 1019 | + } |
| 1020 | + |
969 | 1021 | private static HttpResponseException.Builder serverErrorResponse() { |
970 | 1022 | return new HttpResponseException.Builder(503, "Service Unavailable", new HttpHeaders()); |
971 | 1023 | } |
|
0 commit comments