|
23 | 23 | import com.google.api.client.testing.http.MockHttpTransport;
|
24 | 24 | import com.google.api.client.testing.http.MockLowLevelHttpRequest;
|
25 | 25 | import com.google.api.client.testing.http.MockLowLevelHttpResponse;
|
| 26 | +import com.google.api.client.util.ExponentialBackOff; |
26 | 27 | import java.io.ByteArrayInputStream;
|
27 | 28 | import java.io.ByteArrayOutputStream;
|
28 | 29 | import java.io.IOException;
|
29 | 30 | import java.io.ObjectInputStream;
|
30 | 31 | import java.io.ObjectOutput;
|
31 | 32 | import java.io.ObjectOutputStream;
|
32 | 33 | import junit.framework.TestCase;
|
| 34 | +import org.junit.Assert; |
33 | 35 | import org.junit.function.ThrowingRunnable;
|
34 | 36 |
|
35 | 37 | /**
|
@@ -208,6 +210,8 @@ public void run() throws Throwable {
|
208 | 210 | + SIMPLE_GENERIC_URL
|
209 | 211 | + LINE_SEPARATOR
|
210 | 212 | + "Unable to find resource");
|
| 213 | + // no retries expected |
| 214 | + assertEquals(1, responseException.getAttemptCount()); |
211 | 215 | }
|
212 | 216 |
|
213 | 217 | public void testInvalidCharset() throws Exception {
|
@@ -245,6 +249,50 @@ public void run() throws Throwable {
|
245 | 249 | .isEqualTo("404 Not Found\nGET " + SIMPLE_GENERIC_URL);
|
246 | 250 | }
|
247 | 251 |
|
| 252 | + public void testAttemptCountWithBackOff() throws Exception { |
| 253 | + HttpTransport fakeTransport = |
| 254 | + new MockHttpTransport() { |
| 255 | + @Override |
| 256 | + public LowLevelHttpRequest buildRequest(String method, String url) throws IOException { |
| 257 | + return new MockLowLevelHttpRequest() { |
| 258 | + @Override |
| 259 | + public LowLevelHttpResponse execute() throws IOException { |
| 260 | + MockLowLevelHttpResponse result = new MockLowLevelHttpResponse(); |
| 261 | + result.setStatusCode(HttpStatusCodes.STATUS_CODE_SERVER_ERROR); |
| 262 | + result.setReasonPhrase("Error"); |
| 263 | + result.setContent("Unknown Error"); |
| 264 | + return result; |
| 265 | + } |
| 266 | + }; |
| 267 | + } |
| 268 | + }; |
| 269 | + ExponentialBackOff backoff = new ExponentialBackOff.Builder().build(); |
| 270 | + final HttpRequest request = |
| 271 | + fakeTransport.createRequestFactory().buildGetRequest(new GenericUrl("http://not/used")); |
| 272 | + request.setUnsuccessfulResponseHandler( |
| 273 | + new HttpBackOffUnsuccessfulResponseHandler(backoff) |
| 274 | + .setBackOffRequired( |
| 275 | + new HttpBackOffUnsuccessfulResponseHandler.BackOffRequired() { |
| 276 | + public boolean isRequired(HttpResponse response) { |
| 277 | + return true; |
| 278 | + } |
| 279 | + })); |
| 280 | + request.setNumberOfRetries(1); |
| 281 | + HttpResponseException responseException = |
| 282 | + assertThrows( |
| 283 | + HttpResponseException.class, |
| 284 | + new ThrowingRunnable() { |
| 285 | + @Override |
| 286 | + public void run() throws Throwable { |
| 287 | + request.execute(); |
| 288 | + } |
| 289 | + }); |
| 290 | + |
| 291 | + Assert.assertEquals(500, responseException.getStatusCode()); |
| 292 | + // original request and 1 retry - total 2 |
| 293 | + assertEquals(2, responseException.getAttemptCount()); |
| 294 | + } |
| 295 | + |
248 | 296 | public void testUnsupportedCharset() throws Exception {
|
249 | 297 | HttpTransport transport =
|
250 | 298 | new MockHttpTransport() {
|
|
0 commit comments