View Javadoc
1   /*
2    * ====================================================================
3    * Licensed to the Apache Software Foundation (ASF) under one
4    * or more contributor license agreements.  See the NOTICE file
5    * distributed with this work for additional information
6    * regarding copyright ownership.  The ASF licenses this file
7    * to you under the Apache License, Version 2.0 (the
8    * "License"); you may not use this file except in compliance
9    * with the License.  You may obtain a copy of the License at
10   *
11   *   http://www.apache.org/licenses/LICENSE-2.0
12   *
13   * Unless required by applicable law or agreed to in writing,
14   * software distributed under the License is distributed on an
15   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
16   * KIND, either express or implied.  See the License for the
17   * specific language governing permissions and limitations
18   * under the License.
19   * ====================================================================
20   *
21   * This software consists of voluntary contributions made by many
22   * individuals on behalf of the Apache Software Foundation.  For more
23   * information on the Apache Software Foundation, please see
24   * <http://www.apache.org/>.
25   *
26   */
27  package org.apache.hc.client5.http.impl;
28  
29  import java.io.IOException;
30  import java.net.ConnectException;
31  import java.net.NoRouteToHostException;
32  import java.net.SocketTimeoutException;
33  import java.net.UnknownHostException;
34  import java.time.Instant;
35  import java.time.temporal.ChronoUnit;
36  
37  import javax.net.ssl.SSLException;
38  
39  import org.apache.hc.client5.http.classic.methods.HttpGet;
40  import org.apache.hc.client5.http.utils.DateUtils;
41  import org.apache.hc.core5.http.ConnectionClosedException;
42  import org.apache.hc.core5.http.HttpHeaders;
43  import org.apache.hc.core5.http.HttpResponse;
44  import org.apache.hc.core5.http.message.BasicHttpResponse;
45  import org.apache.hc.core5.util.TimeValue;
46  import org.junit.jupiter.api.Assertions;
47  import org.junit.jupiter.api.BeforeEach;
48  import org.junit.jupiter.api.Test;
49  
50  public class TestDefaultHttpRequestRetryStrategy {
51  
52      private DefaultHttpRequestRetryStrategy retryStrategy;
53  
54      @BeforeEach
55      public void setup() {
56          this.retryStrategy = new DefaultHttpRequestRetryStrategy(3, TimeValue.ofMilliseconds(1234L));
57      }
58  
59      @Test
60      public void testBasics() throws Exception {
61          final HttpResponse response1 = new BasicHttpResponse(503, "Oopsie");
62          Assertions.assertTrue(this.retryStrategy.retryRequest(response1, 1, null));
63          Assertions.assertTrue(this.retryStrategy.retryRequest(response1, 2, null));
64          Assertions.assertTrue(this.retryStrategy.retryRequest(response1, 3, null));
65          Assertions.assertFalse(this.retryStrategy.retryRequest(response1, 4, null));
66          final HttpResponse response2 = new BasicHttpResponse(500, "Big Time Oopsie");
67          Assertions.assertFalse(this.retryStrategy.retryRequest(response2, 1, null));
68          final HttpResponse response3 = new BasicHttpResponse(429, "Oopsie");
69          Assertions.assertTrue(this.retryStrategy.retryRequest(response3, 1, null));
70          Assertions.assertTrue(this.retryStrategy.retryRequest(response3, 2, null));
71          Assertions.assertTrue(this.retryStrategy.retryRequest(response3, 3, null));
72          Assertions.assertFalse(this.retryStrategy.retryRequest(response3, 4, null));
73  
74          Assertions.assertEquals(TimeValue.ofMilliseconds(1234L), this.retryStrategy.getRetryInterval(response1, 1, null));
75      }
76  
77      @Test
78      public void testRetryAfterHeaderAsLong() throws Exception {
79          final HttpResponse response = new BasicHttpResponse(503, "Oopsie");
80          response.setHeader(HttpHeaders.RETRY_AFTER, "321");
81  
82          Assertions.assertEquals(TimeValue.ofSeconds(321L), this.retryStrategy.getRetryInterval(response, 3, null));
83      }
84  
85      @Test
86      public void testRetryAfterHeaderAsDate() throws Exception {
87          this.retryStrategy = new DefaultHttpRequestRetryStrategy(3, TimeValue.ZERO_MILLISECONDS);
88          final HttpResponse response = new BasicHttpResponse(503, "Oopsie");
89          response.setHeader(HttpHeaders.RETRY_AFTER, DateUtils.formatStandardDate(Instant.now().plus(100, ChronoUnit.SECONDS)));
90  
91          Assertions.assertTrue(this.retryStrategy.getRetryInterval(response, 3, null).compareTo(TimeValue.ZERO_MILLISECONDS) > 0);
92      }
93  
94      @Test
95      public void testRetryAfterHeaderAsPastDate() throws Exception {
96          final HttpResponse response = new BasicHttpResponse(503, "Oopsie");
97          response.setHeader(HttpHeaders.RETRY_AFTER, DateUtils.formatStandardDate(Instant.now().minus(100, ChronoUnit.SECONDS)));
98  
99          Assertions.assertEquals(TimeValue.ofMilliseconds(1234L), this.retryStrategy.getRetryInterval(response, 3, null));
100     }
101 
102     @Test
103     public void testInvalidRetryAfterHeader() throws Exception {
104         final HttpResponse response = new BasicHttpResponse(503, "Oopsie");
105         response.setHeader(HttpHeaders.RETRY_AFTER, "Stuff");
106 
107         Assertions.assertEquals(TimeValue.ofMilliseconds(1234L), retryStrategy.getRetryInterval(response, 3, null));
108     }
109 
110     @Test
111     public void noRetryOnConnectTimeout() throws Exception {
112         final HttpGet request = new HttpGet("/");
113 
114         Assertions.assertFalse(retryStrategy.retryRequest(request, new SocketTimeoutException(), 1, null));
115     }
116 
117     @Test
118     public void noRetryOnConnect() throws Exception {
119         final HttpGet request = new HttpGet("/");
120 
121         Assertions.assertFalse(retryStrategy.retryRequest(request, new ConnectException(), 1, null));
122     }
123 
124     @Test
125     public void noRetryOnConnectionClosed() throws Exception {
126         final HttpGet request = new HttpGet("/");
127 
128         Assertions.assertFalse(retryStrategy.retryRequest(request, new ConnectionClosedException(), 1, null));
129     }
130 
131     @Test
132     public void noRetryForNoRouteToHostException() {
133         final HttpGet request = new HttpGet("/");
134 
135         Assertions.assertFalse(retryStrategy.retryRequest(request, new NoRouteToHostException(), 1, null));
136     }
137 
138     @Test
139     public void noRetryOnSSLFailure() throws Exception {
140         final HttpGet request = new HttpGet("/");
141 
142         Assertions.assertFalse(retryStrategy.retryRequest(request, new SSLException("encryption failed"), 1, null));
143     }
144 
145     @Test
146     public void noRetryOnUnknownHost() throws Exception {
147         final HttpGet request = new HttpGet("/");
148 
149         Assertions.assertFalse(retryStrategy.retryRequest(request, new UnknownHostException(), 1, null));
150     }
151 
152     @Test
153     public void noRetryOnAbortedRequests() throws Exception {
154         final HttpGet request = new HttpGet("/");
155         request.cancel();
156 
157         Assertions.assertFalse(retryStrategy.retryRequest(request, new IOException(), 1, null));
158     }
159 
160     @Test
161     public void retryOnNonAbortedRequests() throws Exception {
162         final HttpGet request = new HttpGet("/");
163 
164         Assertions.assertTrue(retryStrategy.retryRequest(request, new IOException(), 1, null));
165     }
166 
167 }