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.Arrays;
30  import java.util.Collection;
31  import java.util.concurrent.Future;
32  
33  import org.apache.hc.client5.http.async.methods.SimpleHttpRequest;
34  import org.apache.hc.client5.http.async.methods.SimpleHttpRequests;
35  import org.apache.hc.client5.http.async.methods.SimpleHttpResponse;
36  import org.apache.hc.client5.http.config.RequestConfig;
37  import org.apache.hc.client5.http.impl.async.CloseableHttpAsyncClient;
38  import org.apache.hc.client5.http.impl.async.HttpAsyncClientBuilder;
39  import org.apache.hc.client5.http.impl.async.HttpAsyncClients;
40  import org.apache.hc.client5.http.impl.nio.PoolingAsyncClientConnectionManager;
41  import org.apache.hc.client5.http.impl.nio.PoolingAsyncClientConnectionManagerBuilder;
42  import org.apache.hc.client5.http.ssl.DefaultClientTlsStrategy;
43  import org.apache.hc.client5.testing.SSLTestContexts;
44  import org.apache.hc.core5.http.HeaderElements;
45  import org.apache.hc.core5.http.HttpHeaders;
46  import org.apache.hc.core5.http.HttpHost;
47  import org.apache.hc.core5.http.URIScheme;
48  import org.apache.hc.core5.http.config.Http1Config;
49  import org.hamcrest.CoreMatchers;
50  import org.junit.Assert;
51  import org.junit.Rule;
52  import org.junit.Test;
53  import org.junit.rules.ExternalResource;
54  import org.junit.runner.RunWith;
55  import org.junit.runners.Parameterized;
56  
57  @RunWith(Parameterized.class)
58  public class TestHttp1Async extends AbstractHttpAsyncFundamentalsTest<CloseableHttpAsyncClient> {
59  
60      @Parameterized.Parameters(name = "HTTP/1.1 {0}")
61      public static Collection<Object[]> protocols() {
62          return Arrays.asList(new Object[][]{
63                  { URIScheme.HTTP },
64                  { URIScheme.HTTPS },
65          });
66      }
67  
68      protected HttpAsyncClientBuilder clientBuilder;
69      protected PoolingAsyncClientConnectionManager connManager;
70  
71      @Rule
72      public ExternalResource connManagerResource = new ExternalResource() {
73  
74          @Override
75          protected void before() throws Throwable {
76              connManager = PoolingAsyncClientConnectionManagerBuilder.create()
77                      .setTlsStrategy(new DefaultClientTlsStrategy(SSLTestContexts.createClientSSLContext()))
78                      .build();
79          }
80  
81          @Override
82          protected void after() {
83              if (connManager != null) {
84                  connManager.close();
85                  connManager = null;
86              }
87          }
88  
89      };
90  
91      @Rule
92      public ExternalResource clientBuilderResource = new ExternalResource() {
93  
94          @Override
95          protected void before() throws Throwable {
96              clientBuilder = HttpAsyncClientBuilder.create()
97                      .setDefaultRequestConfig(RequestConfig.custom()
98                              .setConnectionRequestTimeout(TIMEOUT)
99                              .setConnectTimeout(TIMEOUT)
100                             .build())
101                     .setConnectionManager(connManager);
102         }
103 
104     };
105 
106     public TestHttp1Async(final URIScheme scheme) {
107         super(scheme);
108     }
109 
110     @Override
111     protected CloseableHttpAsyncClient createClient() {
112         return clientBuilder.build();
113     }
114 
115     @Override
116     public HttpHost start() throws Exception {
117         return super.start(null, Http1Config.DEFAULT);
118     }
119 
120     @Test
121     public void testSequenctialGetRequestsCloseConnection() throws Exception {
122         final HttpHost target = start();
123         for (int i = 0; i < 3; i++) {
124             final SimpleHttpRequest get = SimpleHttpRequests.get(target, "/random/2048");
125             get.setHeader(HttpHeaders.CONNECTION, HeaderElements.CLOSE);
126             final Future<SimpleHttpResponse> future = httpclient.execute(get, null);
127             final SimpleHttpResponse response = future.get();
128             Assert.assertThat(response, CoreMatchers.notNullValue());
129             Assert.assertThat(response.getCode(), CoreMatchers.equalTo(200));
130             final String body = response.getBodyText();
131             Assert.assertThat(body, CoreMatchers.notNullValue());
132             Assert.assertThat(body.length(), CoreMatchers.equalTo(2048));
133         }
134     }
135 
136     @Test
137     public void testConcurrentPostsOverMultipleConnections() throws Exception {
138         connManager.setDefaultMaxPerRoute(20);
139         connManager.setMaxTotal(100);
140         super.testConcurrentPostRequests();
141     }
142 
143     @Test
144     public void testConcurrentPostsOverSingleConnection() throws Exception {
145         connManager.setDefaultMaxPerRoute(1);
146         connManager.setMaxTotal(100);
147         super.testConcurrentPostRequests();
148     }
149 
150     @Test
151     public void testSharedPool() throws Exception {
152         final HttpHost target = start();
153         final Future<SimpleHttpResponse> future1 = httpclient.execute(
154                 SimpleHttpRequests.get(target, "/random/2048"), null);
155         final SimpleHttpResponse response1 = future1.get();
156         Assert.assertThat(response1, CoreMatchers.notNullValue());
157         Assert.assertThat(response1.getCode(), CoreMatchers.equalTo(200));
158         final String body1 = response1.getBodyText();
159         Assert.assertThat(body1, CoreMatchers.notNullValue());
160         Assert.assertThat(body1.length(), CoreMatchers.equalTo(2048));
161 
162 
163         try (final CloseableHttpAsyncClient httpclient2 = HttpAsyncClients.custom()
164                 .setConnectionManager(connManager)
165                 .setConnectionManagerShared(true)
166                 .build()) {
167             httpclient2.start();
168             final Future<SimpleHttpResponse> future2 = httpclient2.execute(
169                     SimpleHttpRequests.get(target, "/random/2048"), null);
170             final SimpleHttpResponse response2 = future2.get();
171             Assert.assertThat(response2, CoreMatchers.notNullValue());
172             Assert.assertThat(response2.getCode(), CoreMatchers.equalTo(200));
173             final String body2 = response2.getBodyText();
174             Assert.assertThat(body2, CoreMatchers.notNullValue());
175             Assert.assertThat(body2.length(), CoreMatchers.equalTo(2048));
176         }
177 
178         final Future<SimpleHttpResponse> future3 = httpclient.execute(
179                 SimpleHttpRequests.get(target, "/random/2048"), null);
180         final SimpleHttpResponse response3 = future3.get();
181         Assert.assertThat(response3, CoreMatchers.notNullValue());
182         Assert.assertThat(response3.getCode(), CoreMatchers.equalTo(200));
183         final String body3 = response3.getBodyText();
184         Assert.assertThat(body3, CoreMatchers.notNullValue());
185         Assert.assertThat(body3.length(), CoreMatchers.equalTo(2048));
186     }
187 
188 }