1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
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.core5.http.HeaderElements;
44 import org.apache.hc.core5.http.HttpHeaders;
45 import org.apache.hc.core5.http.HttpHost;
46 import org.apache.hc.core5.http.URIScheme;
47 import org.apache.hc.core5.http.config.Http1Config;
48 import org.apache.hc.core5.testing.nio.H2TestServer;
49 import org.hamcrest.CoreMatchers;
50 import org.junit.jupiter.api.Test;
51 import org.junit.jupiter.params.ParameterizedTest;
52 import org.junit.jupiter.params.provider.ValueSource;
53
54 public abstract class TestHttp1Async extends AbstractHttpAsyncFundamentalsTest<CloseableHttpAsyncClient> {
55
56 public TestHttp1Async(final URIScheme scheme) {
57 super(scheme);
58 }
59
60 @Override
61 protected H2TestServer startServer() throws Exception {
62 return startServer(Http1Config.DEFAULT, null, null);
63 }
64
65 @Override
66 protected CloseableHttpAsyncClient startClient() throws Exception {
67 return startClient(b -> {});
68 }
69
70 @ParameterizedTest(name = "{displayName}; concurrent connections: {0}")
71 @ValueSource(ints = {5, 1, 20})
72 public void testSequentialGetRequestsCloseConnection(final int concurrentConns) throws Exception {
73 final H2TestServer server = startServer();
74 server.register("/random/*", AsyncRandomHandler::new);
75 final HttpHost target = targetHost();
76
77 final CloseableHttpAsyncClient client = startClient();
78 final PoolingAsyncClientConnectionManager connManager = connManager();
79 connManager.setDefaultMaxPerRoute(concurrentConns);
80 connManager.setMaxTotal(100);
81 for (int i = 0; i < 3; i++) {
82 final Future<SimpleHttpResponse> future = client.execute(
83 SimpleRequestBuilder.get()
84 .setHttpHost(target)
85 .setPath("/random/2048")
86 .addHeader(HttpHeaders.CONNECTION, HeaderElements.CLOSE)
87 .build(), null);
88 final SimpleHttpResponse response = future.get();
89 assertThat(response, CoreMatchers.notNullValue());
90 assertThat(response.getCode(), CoreMatchers.equalTo(200));
91 final String body = response.getBodyText();
92 assertThat(body, CoreMatchers.notNullValue());
93 assertThat(body.length(), CoreMatchers.equalTo(2048));
94 }
95 }
96
97 @Test
98 public void testSharedPool() throws Exception {
99 final H2TestServer server = startServer();
100 server.register("/random/*", AsyncRandomHandler::new);
101 final HttpHost target = targetHost();
102
103 final CloseableHttpAsyncClient client = startClient();
104 final PoolingAsyncClientConnectionManager connManager = connManager();
105 final Future<SimpleHttpResponse> future1 = client.execute(
106 SimpleRequestBuilder.get()
107 .setHttpHost(target)
108 .setPath("/random/2048")
109 .build(), null);
110 final SimpleHttpResponse response1 = future1.get();
111 assertThat(response1, CoreMatchers.notNullValue());
112 assertThat(response1.getCode(), CoreMatchers.equalTo(200));
113 final String body1 = response1.getBodyText();
114 assertThat(body1, CoreMatchers.notNullValue());
115 assertThat(body1.length(), CoreMatchers.equalTo(2048));
116
117
118 try (final CloseableHttpAsyncClient httpclient2 = HttpAsyncClients.custom()
119 .setConnectionManager(connManager)
120 .setConnectionManagerShared(true)
121 .build()) {
122 httpclient2.start();
123 final Future<SimpleHttpResponse> future2 = httpclient2.execute(
124 SimpleRequestBuilder.get()
125 .setHttpHost(target)
126 .setPath("/random/2048")
127 .build(), null);
128 final SimpleHttpResponse response2 = future2.get();
129 assertThat(response2, CoreMatchers.notNullValue());
130 assertThat(response2.getCode(), CoreMatchers.equalTo(200));
131 final String body2 = response2.getBodyText();
132 assertThat(body2, CoreMatchers.notNullValue());
133 assertThat(body2.length(), CoreMatchers.equalTo(2048));
134 }
135
136 final Future<SimpleHttpResponse> future3 = client.execute(
137 SimpleRequestBuilder.get()
138 .setHttpHost(target)
139 .setPath("/random/2048")
140 .build(), null);
141 final SimpleHttpResponse response3 = future3.get();
142 assertThat(response3, CoreMatchers.notNullValue());
143 assertThat(response3.getCode(), CoreMatchers.equalTo(200));
144 final String body3 = response3.getBodyText();
145 assertThat(body3, CoreMatchers.notNullValue());
146 assertThat(body3.length(), CoreMatchers.equalTo(2048));
147 }
148
149 @Test
150 public void testRequestCancellation() throws Exception {
151 final H2TestServer server = startServer();
152 server.register("/random/*", AsyncRandomHandler::new);
153 final HttpHost target = targetHost();
154
155 final CloseableHttpAsyncClient client = startClient();
156 final PoolingAsyncClientConnectionManager connManager = connManager();
157 connManager.setDefaultMaxPerRoute(1);
158 connManager.setMaxTotal(1);
159
160 final ScheduledExecutorService executorService = Executors.newSingleThreadScheduledExecutor();
161 try {
162
163 for (int i = 0; i < 20; i++) {
164 final Future<SimpleHttpResponse> future = client.execute(
165 SimpleRequestBuilder.get()
166 .setHttpHost(target)
167 .setPath("/random/1000")
168 .build(), null);
169
170 executorService.schedule(new Runnable() {
171
172 @Override
173 public void run() {
174 future.cancel(true);
175 }
176 }, i % 5, TimeUnit.MILLISECONDS);
177
178 try {
179 future.get(TIMEOUT.getDuration(), TIMEOUT.getTimeUnit());
180 } catch (final TimeoutException ex) {
181 throw ex;
182 } catch (final Exception ignore) {
183 }
184 }
185
186 final Random rnd = new Random();
187 for (int i = 0; i < 20; i++) {
188 final Future<SimpleHttpResponse> future = client.execute(
189 SimpleRequestBuilder.get()
190 .setHttpHost(target)
191 .setPath("/random/1000")
192 .build(), null);
193
194 executorService.schedule(new Runnable() {
195
196 @Override
197 public void run() {
198 future.cancel(true);
199 }
200 }, rnd.nextInt(200), TimeUnit.MILLISECONDS);
201
202 try {
203 future.get(TIMEOUT.getDuration(), TIMEOUT.getTimeUnit());
204 } catch (final TimeoutException ex) {
205 throw ex;
206 } catch (final Exception ignore) {
207 }
208 }
209
210 for (int i = 0; i < 5; i++) {
211 final Future<SimpleHttpResponse> future = client.execute(
212 SimpleRequestBuilder.get()
213 .setHttpHost(target)
214 .setPath("/random/1000")
215 .build(), null);
216 final SimpleHttpResponse response = future.get(TIMEOUT.getDuration(), TIMEOUT.getTimeUnit());
217 assertThat(response, CoreMatchers.notNullValue());
218 assertThat(response.getCode(), CoreMatchers.equalTo(200));
219 }
220
221 } finally {
222 executorService.shutdownNow();
223 }
224 }
225
226 }