18
18
package org .openqa .selenium .remote .http ;
19
19
20
20
import static java .net .HttpURLConnection .HTTP_CLIENT_TIMEOUT ;
21
- import static java .net .HttpURLConnection .HTTP_GATEWAY_TIMEOUT ;
22
21
import static java .net .HttpURLConnection .HTTP_INTERNAL_ERROR ;
23
22
import static java .net .HttpURLConnection .HTTP_OK ;
24
23
import static java .net .HttpURLConnection .HTTP_UNAVAILABLE ;
@@ -56,7 +55,8 @@ public void setUp() throws MalformedURLException {
56
55
ClientConfig .defaultConfig ()
57
56
.baseUrl (URI .create ("http://localhost:2345" ).toURL ())
58
57
.withRetries ()
59
- .readTimeout (Duration .ofSeconds (1 ));
58
+ .readTimeout (Duration .ofSeconds (1 ))
59
+ .connectionTimeout (Duration .ofSeconds (1 ));
60
60
client = HttpClient .Factory .createDefault ().createClient (config );
61
61
}
62
62
@@ -82,8 +82,8 @@ void canReturnAppropriateFallbackResponse() {
82
82
throw new TimeoutException ();
83
83
});
84
84
85
- Assertions .assertEquals (
86
- HTTP_GATEWAY_TIMEOUT , handler1 .execute (new HttpRequest (GET , "/" )). getStatus ( ));
85
+ Assertions .assertThrows (
86
+ TimeoutException . class , () -> handler1 .execute (new HttpRequest (GET , "/" )));
87
87
88
88
HttpHandler handler2 =
89
89
new RetryRequest ()
@@ -96,12 +96,11 @@ void canReturnAppropriateFallbackResponse() {
96
96
@ Test
97
97
void canReturnAppropriateFallbackResponseWithMultipleThreads ()
98
98
throws InterruptedException , ExecutionException {
99
- HttpHandler handler1 =
100
- new RetryRequest ()
101
- .andFinally (
102
- (HttpRequest request ) -> {
103
- throw new TimeoutException ();
104
- });
99
+ AppServer server = new NettyAppServer (req -> new HttpResponse ());
100
+
101
+ URI uri = URI .create (server .whereIs ("/" ));
102
+ HttpRequest connectionTimeoutRequest =
103
+ new HttpRequest (GET , String .format (REQUEST_PATH , uri .getHost (), uri .getPort ()));
105
104
106
105
HttpHandler handler2 =
107
106
new RetryRequest ()
@@ -110,12 +109,12 @@ void canReturnAppropriateFallbackResponseWithMultipleThreads()
110
109
ExecutorService executorService = Executors .newFixedThreadPool (2 );
111
110
List <Callable <HttpResponse >> tasks = new ArrayList <>();
112
111
113
- tasks .add (() -> handler1 .execute (new HttpRequest ( GET , "/" ) ));
112
+ tasks .add (() -> client .execute (connectionTimeoutRequest ));
114
113
tasks .add (() -> handler2 .execute (new HttpRequest (GET , "/" )));
115
114
116
115
List <Future <HttpResponse >> results = executorService .invokeAll (tasks );
117
116
118
- Assertions .assertEquals (HTTP_GATEWAY_TIMEOUT , results .get (0 ).get ().getStatus ());
117
+ Assertions .assertEquals (HTTP_CLIENT_TIMEOUT , results .get (0 ).get ().getStatus ());
119
118
120
119
Assertions .assertEquals (HTTP_UNAVAILABLE , results .get (1 ).get ().getStatus ());
121
120
}
@@ -265,70 +264,6 @@ void shouldGetTheErrorResponseOnServerUnavailableError() {
265
264
server .stop ();
266
265
}
267
266
268
- @ Test
269
- void shouldBeAbleToRetryARequestOnTimeout () {
270
- AtomicInteger count = new AtomicInteger (0 );
271
- AppServer server =
272
- new NettyAppServer (
273
- req -> {
274
- count .incrementAndGet ();
275
- if (count .get () <= 3 ) {
276
- try {
277
- Thread .sleep (2000 );
278
- } catch (InterruptedException e ) {
279
- e .printStackTrace ();
280
- }
281
- }
282
- return new HttpResponse ();
283
- });
284
- server .start ();
285
-
286
- URI uri = URI .create (server .whereIs ("/" ));
287
- HttpRequest request =
288
- new HttpRequest (GET , String .format (REQUEST_PATH , uri .getHost (), uri .getPort ()));
289
-
290
- HttpResponse response = client .execute (request );
291
- assertThat (response ).extracting (HttpResponse ::getStatus ).isEqualTo (HTTP_OK );
292
- assertThat (count .get ()).isEqualTo (4 );
293
-
294
- server .stop ();
295
- }
296
-
297
- @ Test
298
- void shouldBeAbleToGetErrorResponseOnRequestTimeout () {
299
- AtomicInteger count = new AtomicInteger (0 );
300
- AppServer server =
301
- new NettyAppServer (
302
- req -> {
303
- count .incrementAndGet ();
304
- throw new TimeoutException ();
305
- });
306
- server .start ();
307
-
308
- URI uri = URI .create (server .whereIs ("/" ));
309
- HttpRequest request =
310
- new HttpRequest (GET , String .format (REQUEST_PATH , uri .getHost (), uri .getPort ()));
311
-
312
- HttpResponse response = client .execute (request );
313
-
314
- // The NettyAppServer passes the request through ErrorFilter.
315
- // This maps the timeout exception to HTTP response code 500 and HTTP response body containing
316
- // "timeout".
317
- // RetryRequest retries if it gets a TimeoutException only.
318
- // Parsing and inspecting the response body each time if HTTP response code 500 is not
319
- // efficient.
320
- // A potential solution can be updating the ErrorCodec to reflect the appropriate HTTP code
321
- // (this is a breaking change).
322
- // RetryRequest can then inspect just the HTTP response status code and retry.
323
-
324
- assertThat (response ).extracting (HttpResponse ::getStatus ).isEqualTo (HTTP_INTERNAL_ERROR );
325
-
326
- // This should ideally be more than the number of retries configured i.e. greater than 3
327
- assertThat (count .get ()).isEqualTo (1 );
328
-
329
- server .stop ();
330
- }
331
-
332
267
@ Test
333
268
void shouldBeAbleToRetryARequestOnConnectionFailure () {
334
269
AppServer server = new NettyAppServer (req -> new HttpResponse ());
0 commit comments