From 24df54f6e37ed1f287bbd111d4d64bdd19b246d7 Mon Sep 17 00:00:00 2001 From: Jeff Ching Date: Tue, 11 Dec 2018 14:41:39 -0800 Subject: [PATCH] Remove usage of deprecated Backoff from google-http-java-client --- .../client/googleapis/batch/BatchRequest.java | 19 ------ .../batch/BatchUnparsedResponse.java | 14 +--- .../googleapis/batch/BatchRequestTest.java | 65 ++++--------------- 3 files changed, 14 insertions(+), 84 deletions(-) diff --git a/google-api-client/src/main/java/com/google/api/client/googleapis/batch/BatchRequest.java b/google-api-client/src/main/java/com/google/api/client/googleapis/batch/BatchRequest.java index 8471af831..5095add2b 100644 --- a/google-api-client/src/main/java/com/google/api/client/googleapis/batch/BatchRequest.java +++ b/google-api-client/src/main/java/com/google/api/client/googleapis/batch/BatchRequest.java @@ -14,7 +14,6 @@ package com.google.api.client.googleapis.batch; -import com.google.api.client.http.BackOffPolicy; import com.google.api.client.http.GenericUrl; import com.google.api.client.http.HttpBackOffUnsuccessfulResponseHandler; import com.google.api.client.http.HttpExecuteInterceptor; @@ -93,7 +92,6 @@ public void onFailure(GoogleJsonErrorContainer e, HttpHeaders responseHeaders) { * @since 1.9 * @author rmistry@google.com (Ravi Mistry) */ -@SuppressWarnings("deprecation") public final class BatchRequest { /** The URL where batch requests are sent. */ @@ -220,12 +218,6 @@ public void execute() throws IOException { HttpExecuteInterceptor originalInterceptor = batchRequest.getInterceptor(); batchRequest.setInterceptor(new BatchInterceptor(originalInterceptor)); int retriesRemaining = batchRequest.getNumberOfRetries(); - BackOffPolicy backOffPolicy = batchRequest.getBackOffPolicy(); - - if (backOffPolicy != null) { - // Reset the BackOffPolicy at the start of each execute. - backOffPolicy.reset(); - } do { retryAllowed = retriesRemaining > 0; @@ -259,17 +251,6 @@ public void execute() throws IOException { List> unsuccessfulRequestInfos = batchResponse.unsuccessfulRequestInfos; if (!unsuccessfulRequestInfos.isEmpty()) { requestInfos = unsuccessfulRequestInfos; - // backOff if required. - if (batchResponse.backOffRequired && backOffPolicy != null) { - long backOffTime = backOffPolicy.getNextBackOffMillis(); - if (backOffTime != BackOffPolicy.STOP) { - try { - sleeper.sleep(backOffTime); - } catch (InterruptedException exception) { - // ignore - } - } - } } else { break; } diff --git a/google-api-client/src/main/java/com/google/api/client/googleapis/batch/BatchUnparsedResponse.java b/google-api-client/src/main/java/com/google/api/client/googleapis/batch/BatchUnparsedResponse.java index aad711a1c..e6ec26323 100644 --- a/google-api-client/src/main/java/com/google/api/client/googleapis/batch/BatchUnparsedResponse.java +++ b/google-api-client/src/main/java/com/google/api/client/googleapis/batch/BatchUnparsedResponse.java @@ -15,7 +15,6 @@ package com.google.api.client.googleapis.batch; import com.google.api.client.googleapis.batch.BatchRequest.RequestInfo; -import com.google.api.client.http.BackOffPolicy; import com.google.api.client.http.GenericUrl; import com.google.api.client.http.HttpContent; import com.google.api.client.http.HttpHeaders; @@ -42,7 +41,6 @@ * * @author rmistry@google.com (Ravi Mistry) */ -@SuppressWarnings("deprecation") final class BatchUnparsedResponse { /** The boundary used in the batch response to separate individual responses. */ @@ -60,9 +58,6 @@ final class BatchUnparsedResponse { /** List of unsuccessful HTTP requests that can be retried. */ List> unsuccessfulRequestInfos = new ArrayList>(); - /** Indicates if back off is required before the next retry. */ - boolean backOffRequired; - /** The content Id the response is currently at. */ private int contentId = 0; @@ -183,10 +178,6 @@ private void parseAndCallback( HttpHeaders responseHeaders = response.getHeaders(); HttpUnsuccessfulResponseHandler unsuccessfulResponseHandler = requestInfo.request.getUnsuccessfulResponseHandler(); - BackOffPolicy backOffPolicy = requestInfo.request.getBackOffPolicy(); - - // Reset backOff flag. - backOffRequired = false; if (HttpStatusCodes.isSuccess(statusCode)) { if (callback == null) { @@ -207,12 +198,9 @@ private void parseAndCallback( if (!errorHandled) { if (requestInfo.request.handleRedirect(response.getStatusCode(), response.getHeaders())) { redirectRequest = true; - } else if (retrySupported && backOffPolicy != null - && backOffPolicy.isBackOffRequired(response.getStatusCode())) { - backOffRequired = true; } } - if (retrySupported && (errorHandled || backOffRequired || redirectRequest)) { + if (retrySupported && (errorHandled || redirectRequest)) { unsuccessfulRequestInfos.add(requestInfo); } else { if (callback == null) { diff --git a/google-api-client/src/test/java/com/google/api/client/googleapis/batch/BatchRequestTest.java b/google-api-client/src/test/java/com/google/api/client/googleapis/batch/BatchRequestTest.java index 8af46b269..371cec10e 100644 --- a/google-api-client/src/test/java/com/google/api/client/googleapis/batch/BatchRequestTest.java +++ b/google-api-client/src/test/java/com/google/api/client/googleapis/batch/BatchRequestTest.java @@ -48,7 +48,6 @@ * * @author rmistry@google.com (Ravi Mistry) */ -@SuppressWarnings("deprecation") public class BatchRequestTest extends TestCase { private static final String ROOT_URL = "http://www.test.com/"; @@ -301,19 +300,16 @@ private static class MockTransport extends MockHttpTransport { final boolean testAuthenticationError; boolean returnSuccessAuthenticatedContent; boolean returnErrorAuthenticatedContent; - final boolean testExponentialBackOff; final boolean testRedirect; final boolean testBinary; final boolean testMissingLength; int actualCalls; int callsBeforeSuccess; - MockTransport(boolean testServerError, boolean testAuthenticationError, - boolean testExponentialBackOff, boolean testRedirect, boolean testBinary, + MockTransport(boolean testServerError, boolean testAuthenticationError, boolean testRedirect, boolean testBinary, boolean testMissingLength) { this.testServerError = testServerError; this.testAuthenticationError = testAuthenticationError; - this.testExponentialBackOff = testExponentialBackOff; this.testRedirect = testRedirect; this.testBinary = testBinary; this.testMissingLength = testMissingLength; @@ -355,8 +351,7 @@ public LowLevelHttpResponse execute() throws IOException { + "\"code\": " + ERROR_CODE + ", \"message\": \"" + ERROR_MSG + "\"}}"); ByteArrayOutputStream outputStream = new ByteArrayOutputStream(); Writer responseContent = new OutputStreamWriter(outputStream, "ISO-8859-1"); - if (returnSuccessAuthenticatedContent || (testExponentialBackOff && actualCalls > 1) - || (testRedirect && actualCalls > 1)) { + if (returnSuccessAuthenticatedContent || (testRedirect && actualCalls > 1)) { if (returnSuccessAuthenticatedContent || actualCalls == callsBeforeSuccess) { responseContent.append("--" + RESPONSE_BOUNDARY + "\n") .append("Content-Type: application/http\n") @@ -483,13 +478,11 @@ public void intercept(HttpRequest request) { private BatchRequest getBatchPopulatedWithRequests(boolean testServerError, boolean testAuthenticationError, boolean returnSuccessAuthenticatedContent, - boolean testExponentialBackOff, boolean testRedirect, boolean testBinary, boolean testMissingLength) throws Exception { transport = new MockTransport(testServerError, testAuthenticationError, - testExponentialBackOff, testRedirect, testBinary, testMissingLength); @@ -514,9 +507,6 @@ private BatchRequest getBatchPopulatedWithRequests(boolean testServerError, request2.setUnsuccessfulResponseHandler( new MockUnsuccessfulResponseHandler(transport, returnSuccessAuthenticatedContent)); } - if (testExponentialBackOff) { - request2.setBackOffPolicy(new MockExponentialBackOffPolicy()); - } if (testBinary) { batchRequest.queue(request1, MockData.Class1.class, ErrorOutput.ErrorBody.class, @@ -532,7 +522,7 @@ private BatchRequest getBatchPopulatedWithRequests(boolean testServerError, public void testQueueDatastructures() throws Exception { BatchRequest batchRequest = - getBatchPopulatedWithRequests(false, false, false, false, false, false, false); + getBatchPopulatedWithRequests(false, false, false, false, false, false); List> requestInfos = batchRequest.requestInfos; // Assert that the expected objects are queued. @@ -552,7 +542,7 @@ public void testQueueDatastructures() throws Exception { public void testExecute() throws Exception { BatchRequest batchRequest = - getBatchPopulatedWithRequests(false, false, false, false, false, false, false); + getBatchPopulatedWithRequests(false, false, false, false, false, false); batchRequest.execute(); // Assert callbacks have been invoked. assertEquals(1, callback1.successCalls); @@ -564,7 +554,7 @@ public void testExecute() throws Exception { public void testExecuteWithError() throws Exception { BatchRequest batchRequest = - getBatchPopulatedWithRequests(true, false, false, false, false, false, false); + getBatchPopulatedWithRequests(true, false, false, false, false, false); batchRequest.execute(); // Assert callbacks have been invoked. assertEquals(1, callback1.successCalls); @@ -593,7 +583,7 @@ public void testExecuteWithVoidCallbackError() throws Exception { } public void subTestExecuteWithVoidCallback(boolean testServerError) throws Exception { - MockTransport transport = new MockTransport(testServerError, false, false, false, false, false); + MockTransport transport = new MockTransport(testServerError, false,false, false, false); MockGoogleClient client = new MockGoogleClient.Builder( transport, ROOT_URL, SERVICE_PATH, null, null).setApplicationName("Test Application") .build(); @@ -617,7 +607,7 @@ public void subTestExecuteWithVoidCallback(boolean testServerError) throws Excep public void testExecuteWithAuthenticationErrorThenSuccessCallback() throws Exception { BatchRequest batchRequest = - getBatchPopulatedWithRequests(false, true, true, false, false, false, false); + getBatchPopulatedWithRequests(false, true, true, false, false, false); batchRequest.execute(); // Assert callbacks have been invoked. assertEquals(1, callback1.successCalls); @@ -631,7 +621,7 @@ public void testExecuteWithAuthenticationErrorThenSuccessCallback() throws Excep public void testExecuteWithAuthenticationErrorThenErrorCallback() throws Exception { BatchRequest batchRequest = - getBatchPopulatedWithRequests(false, true, false, false, false, false, false); + getBatchPopulatedWithRequests(false, true, false, false, false, false); batchRequest.execute(); // Assert callbacks have been invoked. assertEquals(1, callback1.successCalls); @@ -643,38 +633,9 @@ public void testExecuteWithAuthenticationErrorThenErrorCallback() throws Excepti assertTrue(batchRequest.requestInfos.isEmpty()); } - public void testExecuteWithExponentialBackoffThenSuccessCallback() throws Exception { - BatchRequest batchRequest = - getBatchPopulatedWithRequests(true, false, false, true, false, false, false); - transport.callsBeforeSuccess = 2; - batchRequest.execute(); - // Assert callbacks have been invoked. - assertEquals(1, callback1.successCalls); - assertEquals(1, callback2.successCalls); - // Assert transport called expected number of times. - assertEquals(2, transport.actualCalls); - // Assert requestInfos is empty after execute. - assertTrue(batchRequest.requestInfos.isEmpty()); - } - - public void testExecuteWithExponentialBackoffThenErrorCallback() throws Exception { - BatchRequest batchRequest = - getBatchPopulatedWithRequests(true, false, false, true, false, false, false); - transport.callsBeforeSuccess = 20; - batchRequest.execute(); - // Assert callbacks have been invoked. - assertEquals(1, callback1.successCalls); - assertEquals(0, callback2.successCalls); - assertEquals(1, callback2.failureCalls); - // Assert transport called expected number of times. - assertEquals(11, transport.actualCalls); - // Assert requestInfos is empty after execute. - assertTrue(batchRequest.requestInfos.isEmpty()); - } - public void testInterceptor() throws Exception { BatchRequest batchRequest = - getBatchPopulatedWithRequests(true, false, false, true, false, false, false); + getBatchPopulatedWithRequests(true, false, false, false, false, false); batchRequest.execute(); // Assert the top-level request initializer is called. assertTrue(credential.initializerCalled); @@ -683,7 +644,7 @@ public void testInterceptor() throws Exception { public void testRedirect() throws Exception { BatchRequest batchRequest = - getBatchPopulatedWithRequests(false, false, false, false, true, false, false); + getBatchPopulatedWithRequests(false, false, false, true, false, false); transport.callsBeforeSuccess = 2; batchRequest.execute(); // Assert transport called expected number of times. @@ -820,7 +781,7 @@ public boolean retrySupported() { public void testProtoExecute() throws Exception { BatchRequest batchRequest = - getBatchPopulatedWithRequests(false, false, false, false, false, true, false); + getBatchPopulatedWithRequests(false, false, false, false, true, false); batchRequest.execute(); // Assert callbacks have been invoked. assertEquals(1, callback1.successCalls); @@ -832,7 +793,7 @@ public void testProtoExecute() throws Exception { public void testProtoExecuteWithError() throws Exception { BatchRequest batchRequest = - getBatchPopulatedWithRequests(true, false, false, false, false, true, false); + getBatchPopulatedWithRequests(true, false, false, false, true, false); batchRequest.execute(); // Assert callbacks have been invoked. assertEquals(1, callback1.successCalls); @@ -846,7 +807,7 @@ public void testProtoExecuteWithError() throws Exception { public void testProtoExecuteWithoutLength() throws Exception { BatchRequest batchRequest = - getBatchPopulatedWithRequests(false, false, false, false, false, true, true); + getBatchPopulatedWithRequests(false, false, false, false, true, true); batchRequest.execute(); // Assert callbacks have been invoked. assertEquals(1, callback1.successCalls);