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.testing.async;
28  
29  import java.util.concurrent.Future;
30  
31  import org.apache.hc.client5.http.async.methods.SimpleHttpRequest;
32  import org.apache.hc.client5.http.async.methods.SimpleHttpResponse;
33  import org.apache.hc.client5.http.async.methods.SimpleRequestBuilder;
34  import org.apache.hc.client5.http.impl.async.CloseableHttpAsyncClient;
35  import org.apache.hc.client5.http.impl.nio.PoolingAsyncClientConnectionManager;
36  import org.apache.hc.client5.http.protocol.HttpClientContext;
37  import org.apache.hc.client5.testing.extension.async.ClientProtocolLevel;
38  import org.apache.hc.client5.testing.extension.async.ServerProtocolLevel;
39  import org.apache.hc.client5.testing.extension.async.TestAsyncClient;
40  import org.apache.hc.core5.http.ContentType;
41  import org.apache.hc.core5.http.EndpointDetails;
42  import org.apache.hc.core5.http.HttpHost;
43  import org.apache.hc.core5.http.HttpResponse;
44  import org.apache.hc.core5.http.HttpStatus;
45  import org.apache.hc.core5.http.URIScheme;
46  import org.apache.hc.core5.http.protocol.HttpContext;
47  import org.apache.hc.core5.http.protocol.HttpCoreContext;
48  import org.apache.hc.core5.net.URIAuthority;
49  import org.junit.jupiter.api.Assertions;
50  import org.junit.jupiter.api.Test;
51  
52  class TestHttp1AsyncStatefulConnManagement extends AbstractIntegrationTestBase {
53  
54      public TestHttp1AsyncStatefulConnManagement() {
55          super(URIScheme.HTTP, ClientProtocolLevel.STANDARD, ServerProtocolLevel.STANDARD);
56      }
57  
58      @Test
59      void testStatefulConnections() throws Exception {
60          configureServer(bootstrap -> bootstrap.register("*", () -> new AbstractSimpleServerExchangeHandler() {
61  
62              @Override
63              protected SimpleHttpResponse handle(
64                      final SimpleHttpRequest request,
65                      final HttpCoreContext context) {
66                  final SimpleHttpResponse response = new SimpleHttpResponse(HttpStatus.SC_OK);
67                  response.setBody("Whatever", ContentType.TEXT_PLAIN);
68                  return response;
69              }
70          }));
71          final HttpHost target = startServer();
72  
73          configureClient(builder -> builder
74                  .setUserTokenHandler((route, context) -> context.getAttribute("user")));
75  
76          final TestAsyncClient client = startClient();
77  
78          final int workerCount = 2;
79          final int requestCount = 5;
80  
81          final HttpContext[] contexts = new HttpContext[workerCount];
82          final HttpWorker[] workers = new HttpWorker[workerCount];
83          for (int i = 0; i < contexts.length; i++) {
84              final HttpClientContext context = HttpClientContext.create();
85              contexts[i] = context;
86              workers[i] = new HttpWorker(
87                      "user" + i,
88                      context, requestCount, target, client);
89          }
90  
91          for (final HttpWorker worker : workers) {
92              worker.start();
93          }
94          for (final HttpWorker worker : workers) {
95              worker.join(TIMEOUT.toMilliseconds());
96          }
97          for (final HttpWorker worker : workers) {
98              final Exception ex = worker.getException();
99              if (ex != null) {
100                 throw ex;
101             }
102             Assertions.assertEquals(requestCount, worker.getCount());
103         }
104 
105         for (final HttpContext context : contexts) {
106             final String state0 = (String) context.getAttribute("r0");
107             Assertions.assertNotNull(state0);
108             for (int r = 1; r < requestCount; r++) {
109                 Assertions.assertEquals(state0, context.getAttribute("r" + r));
110             }
111         }
112 
113     }
114 
115     static class HttpWorker extends Thread {
116 
117         private final String uid;
118         private final HttpClientContext context;
119         private final int requestCount;
120         private final HttpHost target;
121         private final CloseableHttpAsyncClient httpclient;
122 
123         private volatile Exception exception;
124         private volatile int count;
125 
126         public HttpWorker(
127                 final String uid,
128                 final HttpClientContext context,
129                 final int requestCount,
130                 final HttpHost target,
131                 final CloseableHttpAsyncClient httpclient) {
132             super();
133             this.uid = uid;
134             this.context = context;
135             this.requestCount = requestCount;
136             this.target = target;
137             this.httpclient = httpclient;
138             this.count = 0;
139         }
140 
141         public int getCount() {
142             return count;
143         }
144 
145         public Exception getException() {
146             return exception;
147         }
148 
149         @Override
150         public void run() {
151             try {
152                 context.setAttribute("user", uid);
153                 for (int r = 0; r < requestCount; r++) {
154                     final SimpleHttpRequest request = SimpleRequestBuilder.get()
155                             .setHttpHost(target)
156                             .setPath("/")
157                             .build();
158                     final Future<SimpleHttpResponse> future = httpclient.execute(request, null);
159                     future.get();
160 
161                     count++;
162                     final EndpointDetails endpointDetails = context.getEndpointDetails();
163                     final String connuid = Integer.toHexString(System.identityHashCode(endpointDetails));
164                     context.setAttribute("r" + r, connuid);
165                 }
166 
167             } catch (final Exception ex) {
168                 exception = ex;
169             }
170         }
171 
172     }
173 
174     @Test
175     void testRouteSpecificPoolRecylcing() throws Exception {
176         configureServer(bootstrap -> bootstrap.register("*", () -> new AbstractSimpleServerExchangeHandler() {
177 
178             @Override
179             protected SimpleHttpResponse handle(
180                     final SimpleHttpRequest request,
181                     final HttpCoreContext context) {
182                 final SimpleHttpResponse response = new SimpleHttpResponse(HttpStatus.SC_OK);
183                 response.setBody("Whatever", ContentType.TEXT_PLAIN);
184                 return response;
185             }
186         }));
187         final HttpHost target = startServer();
188 
189         // This tests what happens when a maxed connection pool needs
190         // to kill the last idle connection to a route to build a new
191         // one to the same route.
192 
193         configureClient(builder -> builder
194                 .setUserTokenHandler((route, context) -> context.getAttribute("user")));
195 
196         final TestAsyncClient client = startClient();
197 
198         final PoolingAsyncClientConnectionManager connManager = client.getConnectionManager();
199 
200         final int maxConn = 2;
201         // We build a client with 2 max active // connections, and 2 max per route.
202         connManager.setMaxTotal(maxConn);
203         connManager.setDefaultMaxPerRoute(maxConn);
204 
205         // Bottom of the pool : a *keep alive* connection to Route 1.
206         final HttpContext context1 = HttpClientContext.create();
207         context1.setAttribute("user", "stuff");
208 
209         final SimpleHttpRequest request1 = SimpleRequestBuilder.get()
210                 .setHttpHost(target)
211                 .setPath("/")
212                 .build();
213         final Future<SimpleHttpResponse> future1 = client.execute(request1, context1, null);
214         final HttpResponse response1 = future1.get();
215         Assertions.assertNotNull(response1);
216         Assertions.assertEquals(200, response1.getCode());
217 
218         // The ConnPoolByRoute now has 1 free connection, out of 2 max
219         // The ConnPoolByRoute has one RouteSpcfcPool, that has one free connection
220         // for [localhost][stuff]
221 
222         Thread.sleep(100);
223 
224         // Send a very simple HTTP get (it MUST be simple, no auth, no proxy, no 302, no 401, ...)
225         // Send it to another route. Must be a keepalive.
226         final HttpContext context2 = HttpClientContext.create();
227 
228         final SimpleHttpRequest request2 = SimpleRequestBuilder.get()
229                 .setScheme(target.getSchemeName())
230                 .setAuthority(new URIAuthority("127.0.0.1", target.getPort()))
231                 .setPath("/")
232                 .build();
233         final Future<SimpleHttpResponse> future2 = client.execute(request2, context2, null);
234         final HttpResponse response2 = future2.get();
235         Assertions.assertNotNull(response2);
236         Assertions.assertEquals(200, response2.getCode());
237 
238         // ConnPoolByRoute now has 2 free connexions, out of its 2 max.
239         // The [localhost][stuff] RouteSpcfcPool is the same as earlier
240         // And there is a [127.0.0.1][null] pool with 1 free connection
241 
242         Thread.sleep(100);
243 
244         // This will put the ConnPoolByRoute to the targeted state :
245         // [localhost][stuff] will not get reused because this call is [localhost][null]
246         // So the ConnPoolByRoute will need to kill one connection (it is maxed out globally).
247         // The killed conn is the oldest, which means the first HTTPGet ([localhost][stuff]).
248         // When this happens, the RouteSpecificPool becomes empty.
249         final HttpContext context3 = HttpClientContext.create();
250 
251         final SimpleHttpRequest request3 = SimpleRequestBuilder.get()
252                 .setHttpHost(target)
253                 .setPath("/")
254                 .build();
255         final Future<SimpleHttpResponse> future3 = client.execute(request3, context3, null);
256         final HttpResponse response3 = future3.get();
257         Assertions.assertNotNull(response3);
258         Assertions.assertEquals(200, response3.getCode());
259     }
260 
261 }