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.execchain;
28  
29  import org.apache.http.HttpEntity;
30  import org.apache.http.HttpHost;
31  import org.apache.http.HttpResponse;
32  import org.apache.http.client.ServiceUnavailableRetryStrategy;
33  import org.apache.http.client.methods.CloseableHttpResponse;
34  import org.apache.http.client.methods.HttpExecutionAware;
35  import org.apache.http.client.methods.HttpGet;
36  import org.apache.http.client.methods.HttpPut;
37  import org.apache.http.client.methods.HttpRequestWrapper;
38  import org.apache.http.client.protocol.HttpClientContext;
39  import org.apache.http.conn.routing.HttpRoute;
40  import org.apache.http.protocol.HttpContext;
41  import org.junit.Before;
42  import org.junit.Test;
43  import org.mockito.Mock;
44  import org.mockito.Mockito;
45  import org.mockito.MockitoAnnotations;
46  
47  @SuppressWarnings({"boxing","static-access"}) // test code
48  public class TestServiceUnavailableRetryExec {
49  
50      @Mock
51      private ClientExecChain requestExecutor;
52      @Mock
53      private ServiceUnavailableRetryStrategy retryStrategy;
54      @Mock
55      private HttpExecutionAware execAware;
56  
57      private ServiceUnavailableRetryExec retryExec;
58      private HttpHost target;
59  
60      @Before
61      public void setup() throws Exception {
62          MockitoAnnotations.initMocks(this);
63          retryExec = new ServiceUnavailableRetryExec(requestExecutor, retryStrategy);
64          target = new HttpHost("localhost", 80);
65      }
66  
67      @Test
68      public void testFundamentals() throws Exception {
69          final HttpRoute route = new HttpRoute(target);
70          final HttpGet get = new HttpGet("/test");
71          final HttpRequestWrapper request = HttpRequestWrapper.wrap(get);
72          final HttpClientContext context = HttpClientContext.create();
73  
74          final CloseableHttpResponse response = Mockito.mock(CloseableHttpResponse.class);
75  
76          Mockito.when(requestExecutor.execute(
77                  Mockito.eq(route),
78                  Mockito.same(request),
79                  Mockito.<HttpClientContext>any(),
80                  Mockito.<HttpExecutionAware>any())).thenReturn(response);
81          Mockito.when(retryStrategy.retryRequest(
82                  Mockito.<HttpResponse>any(),
83                  Mockito.anyInt(),
84                  Mockito.<HttpContext>any())).thenReturn(Boolean.TRUE, Boolean.FALSE);
85          Mockito.when(retryStrategy.getRetryInterval()).thenReturn(0L);
86  
87          retryExec.execute(route, request, context, execAware);
88  
89          Mockito.verify(requestExecutor, Mockito.times(2)).execute(
90                  Mockito.eq(route),
91                  Mockito.same(request),
92                  Mockito.same(context),
93                  Mockito.same(execAware));
94          Mockito.verify(response, Mockito.times(1)).close();
95      }
96  
97      @Test(expected = RuntimeException.class)
98      public void testStrategyRuntimeException() throws Exception {
99          final HttpRoute route = new HttpRoute(target);
100         final HttpRequestWrapper request = HttpRequestWrapper.wrap(new HttpGet("/test"));
101         final HttpClientContext context = HttpClientContext.create();
102 
103         final CloseableHttpResponse response = Mockito.mock(CloseableHttpResponse.class);
104         Mockito.when(requestExecutor.execute(
105                 Mockito.eq(route),
106                 Mockito.<HttpRequestWrapper>any(),
107                 Mockito.<HttpClientContext>any(),
108                 Mockito.<HttpExecutionAware>any())).thenReturn(response);
109         Mockito.doThrow(new RuntimeException("Ooopsie")).when(retryStrategy).retryRequest(
110                 Mockito.<HttpResponse>any(),
111                 Mockito.anyInt(),
112                 Mockito.<HttpContext>any());
113         try {
114             retryExec.execute(route, request, context, execAware);
115         } catch (final Exception ex) {
116             Mockito.verify(response).close();
117             throw ex;
118         }
119     }
120 
121     @Test
122     public void testNonRepeatableEntityResponseReturnedImmediately() throws Exception {
123         final HttpRoute route = new HttpRoute(target);
124 
125         final HttpEntity entity = Mockito.mock(HttpEntity.class);
126         Mockito.when(entity.isRepeatable()).thenReturn(Boolean.FALSE);
127 
128         final HttpPut put = new HttpPut("/test");
129         put.setEntity(entity);
130 
131         final HttpRequestWrapper request = HttpRequestWrapper.wrap(put);
132         final HttpClientContext context = HttpClientContext.create();
133         final CloseableHttpResponse response = Mockito.mock(CloseableHttpResponse.class);
134 
135         Mockito.when(requestExecutor.execute(
136                 Mockito.eq(route),
137                 Mockito.<HttpRequestWrapper>any(),
138                 Mockito.<HttpClientContext>any(),
139                 Mockito.<HttpExecutionAware>any())).thenReturn(response);
140 
141         Mockito.when(retryStrategy.retryRequest(
142                 Mockito.<HttpResponse>any(),
143                 Mockito.anyInt(),
144                 Mockito.<HttpContext>any())).thenReturn(Boolean.TRUE, Boolean.FALSE);
145 
146         retryExec.execute(route, request, context, execAware);
147 
148         Mockito.verify(response, Mockito.times(0)).close();
149     }
150 }