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.cache;
28  
29  import static org.mockito.Mockito.mock;
30  import static org.mockito.Mockito.verify;
31  import static org.mockito.Mockito.when;
32  
33  import org.apache.http.HttpHost;
34  import org.apache.http.client.methods.HttpRequestWrapper;
35  import org.apache.http.client.protocol.HttpClientContext;
36  import org.apache.http.conn.routing.HttpRoute;
37  import org.apache.http.impl.execchain.ClientExecChain;
38  import org.apache.http.message.BasicHttpRequest;
39  import org.junit.Before;
40  import org.junit.Test;
41  
42  import java.util.concurrent.ScheduledExecutorService;
43  import java.util.concurrent.TimeUnit;
44  
45  public class TestExponentialBackingOffSchedulingStrategy {
46  
47      private ScheduledExecutorService mockExecutor;
48      private ExponentialBackOffSchedulingStrategy impl;
49  
50      @Before
51      public void setUp() {
52          mockExecutor = mock(ScheduledExecutorService.class);
53  
54          impl = new ExponentialBackOffSchedulingStrategy(
55                  mockExecutor,
56                  ExponentialBackOffSchedulingStrategy.DEFAULT_BACK_OFF_RATE,
57                  ExponentialBackOffSchedulingStrategy.DEFAULT_INITIAL_EXPIRY_IN_MILLIS,
58                  ExponentialBackOffSchedulingStrategy.DEFAULT_MAX_EXPIRY_IN_MILLIS
59          );
60      }
61  
62      @Test
63      public void testScheduleWithoutPreviousError() {
64          final AsynchronousValidationRequest request = createAsynchronousValidationRequest(withErrorCount(0));
65  
66          expectRequestScheduledWithoutDelay(request);
67  
68          impl.schedule(request);
69  
70          verify(mockExecutor).schedule(request, 0, TimeUnit.MILLISECONDS);
71      }
72  
73      @Test
74      public void testScheduleWithOneFailedAttempt() {
75          final AsynchronousValidationRequest request = createAsynchronousValidationRequest(withErrorCount(1));
76  
77          expectRequestScheduledWithDelay(request, TimeUnit.SECONDS.toMillis(6));
78  
79          impl.schedule(request);
80  
81          verify(mockExecutor).schedule(request, 6000, TimeUnit.MILLISECONDS);
82      }
83  
84      @Test
85      public void testScheduleWithTwoFailedAttempts() {
86          final AsynchronousValidationRequest request = createAsynchronousValidationRequest(withErrorCount(2));
87  
88          expectRequestScheduledWithDelay(request, TimeUnit.SECONDS.toMillis(60));
89  
90          impl.schedule(request);
91  
92          verify(mockExecutor).schedule(request, 60000, TimeUnit.MILLISECONDS);
93      }
94  
95      @Test
96      public void testScheduleWithThreeFailedAttempts() {
97          final AsynchronousValidationRequest request = createAsynchronousValidationRequest(withErrorCount(3));
98  
99          expectRequestScheduledWithDelay(request, TimeUnit.SECONDS.toMillis(600));
100 
101         impl.schedule(request);
102 
103         verify(mockExecutor).schedule(request, 600000, TimeUnit.MILLISECONDS);
104     }
105 
106     @Test
107     public void testScheduleWithFourFailedAttempts() {
108         final AsynchronousValidationRequest request = createAsynchronousValidationRequest(withErrorCount(4));
109 
110         expectRequestScheduledWithDelay(request, TimeUnit.SECONDS.toMillis(6000));
111 
112         impl.schedule(request);
113 
114         verify(mockExecutor).schedule(request, 6000000, TimeUnit.MILLISECONDS);
115     }
116 
117     @Test
118     public void testScheduleWithFiveFailedAttempts() {
119         final AsynchronousValidationRequest request = createAsynchronousValidationRequest(withErrorCount(5));
120 
121         expectRequestScheduledWithDelay(request, TimeUnit.SECONDS.toMillis(60000));
122 
123         impl.schedule(request);
124 
125         verify(mockExecutor).schedule(request, 60000000, TimeUnit.MILLISECONDS);
126     }
127 
128     @Test
129     public void testScheduleWithSixFailedAttempts() {
130         final AsynchronousValidationRequest request = createAsynchronousValidationRequest(withErrorCount(6));
131 
132         expectRequestScheduledWithDelay(request, TimeUnit.SECONDS.toMillis(86400));
133 
134         impl.schedule(request);
135 
136         verify(mockExecutor).schedule(request, 86400000, TimeUnit.MILLISECONDS);
137     }
138 
139     @Test
140     public void testScheduleWithMaxNumberOfFailedAttempts() {
141         final AsynchronousValidationRequest request = createAsynchronousValidationRequest(withErrorCount(Integer.MAX_VALUE));
142 
143         expectRequestScheduledWithDelay(request, TimeUnit.SECONDS.toMillis(86400));
144 
145         impl.schedule(request);
146 
147         verify(mockExecutor).schedule(request, 86400000, TimeUnit.MILLISECONDS);
148     }
149 
150     private void expectRequestScheduledWithoutDelay(final AsynchronousValidationRequest request) {
151         expectRequestScheduledWithDelay(request, 0);
152     }
153 
154     private void expectRequestScheduledWithDelay(final AsynchronousValidationRequest request, final long delayInMillis) {
155         when(mockExecutor.schedule(request, delayInMillis, TimeUnit.MILLISECONDS)).thenReturn(null);
156     }
157 
158     private AsynchronousValidationRequest createAsynchronousValidationRequest(final int errorCount) {
159         final ClientExecChain clientExecChain = mock(ClientExecChain.class);
160         final CachingExec cachingHttpClient = new CachingExec(clientExecChain);
161         final AsynchronousValidator mockValidator = new AsynchronousValidator(impl);
162         final HttpRoute httpRoute = new HttpRoute(new HttpHost("foo.example.com", 80));
163         final HttpRequestWrapper httpRequestWrapper = HttpRequestWrapper.wrap(new BasicHttpRequest("GET", "/"));
164         final HttpClientContext httpClientContext = new HttpClientContext();
165         return new AsynchronousValidationRequest(mockValidator, cachingHttpClient, httpRoute, httpRequestWrapper,
166                 httpClientContext, null, null, "identifier", errorCount);
167     }
168 
169     private static int withErrorCount(final int errorCount) {
170         return errorCount;
171     }
172 }