Skip to content

Commit 2d64f90

Browse files
authored
test(retry): add a test which ensures expected behavior of new retry handlers (#1119)
* test(retry): add a test which ensures expected behavior of new retry handlers In addition to providing the new handlers which are validated to some extent with the retry conformance suite, we also want to ensure we maintain support for "java specific" errors which historically are already handled my the core library. In some ways this also functions as a regression suite of errors we want to keep track of for retry purposes. * chore: run formatter to turn a ~450 line file into a 900 line one!!!!
1 parent 57bec4d commit 2d64f90

File tree

2 files changed

+952
-26
lines changed

2 files changed

+952
-26
lines changed

google-cloud-storage/src/main/java/com/google/cloud/storage/DefaultStorageRetryStrategy.java

Lines changed: 33 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -73,41 +73,48 @@ public RetryResult afterEval(Exception exception, RetryResult retryResult) {
7373

7474
@Override
7575
public RetryResult beforeEval(Exception exception) {
76-
Throwable t = exception;
77-
78-
if (t instanceof BaseServiceException) {
79-
BaseServiceException storageException = (BaseServiceException) t;
80-
Throwable cause = storageException.getCause();
81-
// if the cause of the exception is an IOException lift it before we continue
82-
// evaluation
83-
if (cause instanceof IOException) {
84-
t = cause;
85-
}
86-
}
87-
88-
if (t instanceof BaseServiceException) {
89-
BaseServiceException baseServiceException = (BaseServiceException) t;
90-
int code = baseServiceException.getCode();
91-
String reason = baseServiceException.getReason();
92-
return shouldRetryCode(code, reason);
93-
} else if (t instanceof HttpResponseException) {
94-
int code = ((HttpResponseException) t).getStatusCode();
95-
return shouldRetryCode(code, null);
96-
} else if (t instanceof IOException) {
97-
IOException ioException = (IOException) t;
98-
return BaseServiceException.isRetryable(idempotent, ioException)
99-
? RetryResult.RETRY
100-
: RetryResult.NO_RETRY;
76+
if (exception instanceof BaseServiceException) {
77+
BaseServiceException baseServiceException = (BaseServiceException) exception;
78+
return deepShouldRetry(baseServiceException);
79+
} else if (exception instanceof HttpResponseException) {
80+
int code = ((HttpResponseException) exception).getStatusCode();
81+
return shouldRetryCodeReason(code, null);
82+
} else if (exception instanceof IOException) {
83+
IOException ioException = (IOException) exception;
84+
return shouldRetryIOException(ioException);
10185
}
10286
return RetryResult.CONTINUE_EVALUATION;
10387
}
10488

105-
private RetryResult shouldRetryCode(Integer code, String reason) {
89+
private RetryResult shouldRetryCodeReason(Integer code, String reason) {
10690
if (BaseServiceException.isRetryable(code, reason, idempotent, retryableErrors)) {
10791
return RetryResult.RETRY;
10892
} else {
10993
return RetryResult.NO_RETRY;
11094
}
11195
}
96+
97+
private RetryResult shouldRetryIOException(IOException ioException) {
98+
if (BaseServiceException.isRetryable(idempotent, ioException)) {
99+
return RetryResult.RETRY;
100+
} else {
101+
return RetryResult.NO_RETRY;
102+
}
103+
}
104+
105+
private RetryResult deepShouldRetry(BaseServiceException baseServiceException) {
106+
if (baseServiceException.getCode() == BaseServiceException.UNKNOWN_CODE
107+
&& baseServiceException.getReason() == null) {
108+
final Throwable cause = baseServiceException.getCause();
109+
if (cause instanceof IOException) {
110+
IOException ioException = (IOException) cause;
111+
return shouldRetryIOException(ioException);
112+
}
113+
}
114+
115+
int code = baseServiceException.getCode();
116+
String reason = baseServiceException.getReason();
117+
return shouldRetryCodeReason(code, reason);
118+
}
112119
}
113120
}

0 commit comments

Comments
 (0)