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