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 static org.hamcrest.MatcherAssert.assertThat;
30  
31  import java.util.Random;
32  import java.util.concurrent.Executors;
33  import java.util.concurrent.Future;
34  import java.util.concurrent.ScheduledExecutorService;
35  import java.util.concurrent.TimeUnit;
36  import java.util.concurrent.TimeoutException;
37  
38  import org.apache.hc.client5.http.async.methods.SimpleHttpResponse;
39  import org.apache.hc.client5.http.async.methods.SimpleRequestBuilder;
40  import org.apache.hc.client5.http.impl.async.CloseableHttpAsyncClient;
41  import org.apache.hc.client5.http.impl.async.HttpAsyncClients;
42  import org.apache.hc.client5.http.impl.nio.PoolingAsyncClientConnectionManager;
43  import org.apache.hc.client5.testing.extension.async.ClientProtocolLevel;
44  import org.apache.hc.client5.testing.extension.async.ServerProtocolLevel;
45  import org.apache.hc.client5.testing.extension.async.TestAsyncClient;
46  import org.apache.hc.core5.http.HeaderElements;
47  import org.apache.hc.core5.http.HttpHeaders;
48  import org.apache.hc.core5.http.HttpHost;
49  import org.apache.hc.core5.http.URIScheme;
50  import org.hamcrest.CoreMatchers;
51  import org.junit.jupiter.api.Test;
52  import org.junit.jupiter.params.ParameterizedTest;
53  import org.junit.jupiter.params.provider.ValueSource;
54  
55  abstract  class TestHttp1Async extends AbstractHttpAsyncFundamentalsTest {
56  
57      public TestHttp1Async(final URIScheme scheme) {
58          super(scheme, ClientProtocolLevel.STANDARD, ServerProtocolLevel.STANDARD);
59      }
60  
61      @ParameterizedTest(name = "{displayName}; concurrent connections: {0}")
62      @ValueSource(ints = {5, 1, 20})
63      public void testSequentialGetRequestsCloseConnection(final int concurrentConns) throws Exception {
64          configureServer(bootstrap -> bootstrap.register("/random/*", AsyncRandomHandler::new));
65          final HttpHost target = startServer();
66  
67          final TestAsyncClient client = startClient();
68  
69          final PoolingAsyncClientConnectionManager connManager = client.getConnectionManager();
70          connManager.setDefaultMaxPerRoute(concurrentConns);
71          connManager.setMaxTotal(100);
72          for (int i = 0; i < 3; i++) {
73              final Future<SimpleHttpResponse> future = client.execute(
74                      SimpleRequestBuilder.get()
75                              .setHttpHost(target)
76                              .setPath("/random/2048")
77                              .addHeader(HttpHeaders.CONNECTION, HeaderElements.CLOSE)
78                              .build(), null);
79              final SimpleHttpResponse response = future.get();
80              assertThat(response, CoreMatchers.notNullValue());
81              assertThat(response.getCode(), CoreMatchers.equalTo(200));
82              final String body = response.getBodyText();
83              assertThat(body, CoreMatchers.notNullValue());
84              assertThat(body.length(), CoreMatchers.equalTo(2048));
85          }
86      }
87  
88      @Test
89      void testSharedPool() throws Exception {
90          configureServer(bootstrap -> bootstrap.register("/random/*", AsyncRandomHandler::new));
91          final HttpHost target = startServer();
92  
93          final TestAsyncClient client = startClient();
94  
95          final PoolingAsyncClientConnectionManager connManager = client.getConnectionManager();
96          final Future<SimpleHttpResponse> future1 = client.execute(
97                  SimpleRequestBuilder.get()
98                          .setHttpHost(target)
99                          .setPath("/random/2048")
100                         .build(), null);
101         final SimpleHttpResponse response1 = future1.get();
102         assertThat(response1, CoreMatchers.notNullValue());
103         assertThat(response1.getCode(), CoreMatchers.equalTo(200));
104         final String body1 = response1.getBodyText();
105         assertThat(body1, CoreMatchers.notNullValue());
106         assertThat(body1.length(), CoreMatchers.equalTo(2048));
107 
108 
109         try (final CloseableHttpAsyncClient httpclient2 = HttpAsyncClients.custom()
110                 .setConnectionManager(connManager)
111                 .setConnectionManagerShared(true)
112                 .build()) {
113             httpclient2.start();
114             final Future<SimpleHttpResponse> future2 = httpclient2.execute(
115                     SimpleRequestBuilder.get()
116                             .setHttpHost(target)
117                             .setPath("/random/2048")
118                             .build(), null);
119             final SimpleHttpResponse response2 = future2.get();
120             assertThat(response2, CoreMatchers.notNullValue());
121             assertThat(response2.getCode(), CoreMatchers.equalTo(200));
122             final String body2 = response2.getBodyText();
123             assertThat(body2, CoreMatchers.notNullValue());
124             assertThat(body2.length(), CoreMatchers.equalTo(2048));
125         }
126 
127         final Future<SimpleHttpResponse> future3 = client.execute(
128                 SimpleRequestBuilder.get()
129                         .setHttpHost(target)
130                         .setPath("/random/2048")
131                         .build(), null);
132         final SimpleHttpResponse response3 = future3.get();
133         assertThat(response3, CoreMatchers.notNullValue());
134         assertThat(response3.getCode(), CoreMatchers.equalTo(200));
135         final String body3 = response3.getBodyText();
136         assertThat(body3, CoreMatchers.notNullValue());
137         assertThat(body3.length(), CoreMatchers.equalTo(2048));
138     }
139 
140     @Test
141     void testRequestCancellation() throws Exception {
142         configureServer(bootstrap -> bootstrap.register("/random/*", AsyncRandomHandler::new));
143         final HttpHost target = startServer();
144 
145         final TestAsyncClient client = startClient();
146         final PoolingAsyncClientConnectionManager connManager = client.getConnectionManager();
147         connManager.setDefaultMaxPerRoute(1);
148         connManager.setMaxTotal(1);
149 
150         final ScheduledExecutorService executorService = Executors.newSingleThreadScheduledExecutor();
151         try {
152 
153             for (int i = 0; i < 20; i++) {
154                 final Future<SimpleHttpResponse> future = client.execute(
155                         SimpleRequestBuilder.get()
156                                 .setHttpHost(target)
157                                 .setPath("/random/1000")
158                                 .build(), null);
159 
160                 executorService.schedule(() -> future.cancel(true), i % 5, TimeUnit.MILLISECONDS);
161 
162                 try {
163                     future.get(TIMEOUT.getDuration(), TIMEOUT.getTimeUnit());
164                 } catch (final TimeoutException ex) {
165                     throw ex;
166                 } catch (final Exception ignore) {
167                 }
168             }
169 
170             final Random rnd = new Random();
171             for (int i = 0; i < 20; i++) {
172                 final Future<SimpleHttpResponse> future = client.execute(
173                         SimpleRequestBuilder.get()
174                                 .setHttpHost(target)
175                                 .setPath("/random/1000")
176                                 .build(), null);
177 
178                 executorService.schedule(() -> future.cancel(true), rnd.nextInt(200), TimeUnit.MILLISECONDS);
179 
180                 try {
181                     future.get(TIMEOUT.getDuration(), TIMEOUT.getTimeUnit());
182                 } catch (final TimeoutException ex) {
183                     throw ex;
184                 } catch (final Exception ignore) {
185                 }
186             }
187 
188             for (int i = 0; i < 5; i++) {
189                 final Future<SimpleHttpResponse> future = client.execute(
190                         SimpleRequestBuilder.get()
191                                 .setHttpHost(target)
192                                 .setPath("/random/1000")
193                                 .build(), null);
194                 final SimpleHttpResponse response = future.get(TIMEOUT.getDuration(), TIMEOUT.getTimeUnit());
195                 assertThat(response, CoreMatchers.notNullValue());
196                 assertThat(response.getCode(), CoreMatchers.equalTo(200));
197             }
198 
199         } finally {
200             executorService.shutdownNow();
201         }
202     }
203 
204 }