5050import java .io .InputStreamReader ;
5151import java .net .SocketException ;
5252import java .net .URI ;
53+ import java .util .concurrent .Callable ;
5354import java .nio .charset .StandardCharsets ;
5455import java .nio .file .Files ;
5556import java .nio .file .Path ;
@@ -150,7 +151,7 @@ public RetryTestResource createRetryTest(RetryTestResource retryTestResource) th
150151 HttpContent content =
151152 new ByteArrayContent ("application/json" , jsonString .getBytes (StandardCharsets .UTF_8 ));
152153 HttpRequest req = requestFactory .buildPostRequest (url , content );
153- HttpResponse resp = req . execute ( );
154+ HttpResponse resp = execWithRetries ( req :: execute );
154155 RetryTestResource result = gson .fromJson (resp .parseAsString (), RetryTestResource .class );
155156 resp .disconnect ();
156157 return result ;
@@ -159,14 +160,14 @@ public RetryTestResource createRetryTest(RetryTestResource retryTestResource) th
159160 public void deleteRetryTest (RetryTestResource retryTestResource ) throws IOException {
160161 GenericUrl url = new GenericUrl (baseUri + "/retry_test/" + retryTestResource .id );
161162 HttpRequest req = requestFactory .buildDeleteRequest (url );
162- HttpResponse resp = req . execute ( );
163+ HttpResponse resp = execWithRetries ( req :: execute );
163164 resp .disconnect ();
164165 }
165166
166167 public RetryTestResource getRetryTest (RetryTestResource retryTestResource ) throws IOException {
167168 GenericUrl url = new GenericUrl (baseUri + "/retry_test/" + retryTestResource .id );
168169 HttpRequest req = requestFactory .buildGetRequest (url );
169- HttpResponse resp = req . execute ( );
170+ HttpResponse resp = execWithRetries ( req :: execute );
170171 RetryTestResource result = gson .fromJson (resp .parseAsString (), RetryTestResource .class );
171172 resp .disconnect ();
172173 return result ;
@@ -175,7 +176,7 @@ public RetryTestResource getRetryTest(RetryTestResource retryTestResource) throw
175176 public List <RetryTestResource > listRetryTests () throws IOException {
176177 GenericUrl url = new GenericUrl (baseUri + "/retry_tests" );
177178 HttpRequest req = requestFactory .buildGetRequest (url );
178- HttpResponse resp = req . execute ( );
179+ HttpResponse resp = execWithRetries ( req :: execute );
179180 JsonObject result = gson .fromJson (resp .parseAsString (), JsonObject .class );
180181 JsonArray retryTest = (JsonArray ) result .get ("retry_test" );
181182 ImmutableList .Builder <RetryTestResource > b = ImmutableList .builder ();
@@ -186,6 +187,33 @@ public List<RetryTestResource> listRetryTests() throws IOException {
186187 return b .build ();
187188 }
188189
190+ private <T > T execWithRetries (Callable <T > callable ) throws IOException {
191+ try {
192+ return runWithRetries (
193+ callable ,
194+ RetrySettings .newBuilder ()
195+ .setTotalTimeoutDuration (Duration .ofSeconds (15 ))
196+ .setInitialRetryDelayDuration (Duration .ofMillis (200 ))
197+ .setRetryDelayMultiplier (1.5 )
198+ .setMaxRetryDelayDuration (Duration .ofSeconds (2 ))
199+ .setMaxAttempts (5 )
200+ .build (),
201+ new BasicResultRetryAlgorithm <T >() {
202+ @ Override
203+ public boolean shouldRetry (Throwable previousThrowable , T previousResponse ) {
204+ return previousThrowable instanceof SocketException
205+ || previousThrowable instanceof IOException ;
206+ }
207+ },
208+ NanoClock .getDefaultClock ());
209+ } catch (RetryHelperException e ) {
210+ if (e .getCause () instanceof IOException ) {
211+ throw (IOException ) e .getCause ();
212+ }
213+ throw new IOException (e );
214+ }
215+ }
216+
189217 private boolean startGRPCServer (int gRPCPort ) throws IOException {
190218 GenericUrl url = new GenericUrl (baseUri + "/start_grpc?port=9090" );
191219 HttpRequest req = requestFactory .buildGetRequest (url );
0 commit comments