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.http.impl.client;
28  
29  import static org.junit.Assert.assertFalse;
30  import static org.junit.Assert.assertTrue;
31  
32  import java.net.ConnectException;
33  import java.net.SocketTimeoutException;
34  
35  import org.apache.http.HttpResponse;
36  import org.apache.http.HttpStatus;
37  import org.apache.http.HttpVersion;
38  import org.apache.http.client.ConnectionBackoffStrategy;
39  import org.apache.http.conn.ConnectionPoolTimeoutException;
40  import org.apache.http.message.BasicHttpResponse;
41  import org.junit.Before;
42  import org.junit.Test;
43  
44  
45  public class TestDefaultBackoffStrategy {
46  
47      private DefaultBackoffStrategy impl;
48  
49      @Before
50      public void setUp() {
51          impl = new DefaultBackoffStrategy();
52      }
53  
54      @Test
55      public void isABackoffStrategy() {
56          assertTrue(impl instanceof ConnectionBackoffStrategy);
57      }
58  
59      @Test
60      public void backsOffForSocketTimeouts() {
61          assertTrue(impl.shouldBackoff(new SocketTimeoutException()));
62      }
63  
64      @Test
65      public void backsOffForConnectionTimeouts() {
66          assertTrue(impl.shouldBackoff(new ConnectException()));
67      }
68  
69      @Test
70      public void doesNotBackOffForConnectionManagerTimeout() {
71          assertFalse(impl.shouldBackoff(new ConnectionPoolTimeoutException()));
72      }
73  
74      @Test
75      public void backsOffForServiceUnavailable() {
76          final HttpResponse resp = new BasicHttpResponse(HttpVersion.HTTP_1_1,
77                  HttpStatus.SC_SERVICE_UNAVAILABLE, "Service Unavailable");
78          assertTrue(impl.shouldBackoff(resp));
79      }
80  
81      @Test
82      public void backsOffForTooManyRequests() {
83          final HttpResponse resp = new BasicHttpResponse(HttpVersion.HTTP_1_1,
84                  429, "Too Many Requests");
85          assertTrue(impl.shouldBackoff(resp));
86      }
87  
88      @Test
89      public void doesNotBackOffForNon429And503StatusCodes() {
90          for(int i = 100; i <= 599; i++) {
91              if (i== 429|| i == HttpStatus.SC_SERVICE_UNAVAILABLE) {
92                  continue;
93              }
94              final HttpResponse resp = new BasicHttpResponse(HttpVersion.HTTP_1_1,
95                      i, "Foo");
96              assertFalse(impl.shouldBackoff(resp));
97          }
98      }
99  }