/
MultiplexedConnectionPoolTest.java
307 lines (266 loc) · 11.3 KB
/
MultiplexedConnectionPoolTest.java
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
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
//
// ========================================================================
// Copyright (c) 1995-2021 Mort Bay Consulting Pty Ltd and others.
// ------------------------------------------------------------------------
// All rights reserved. This program and the accompanying materials
// are made available under the terms of the Eclipse Public License v1.0
// and Apache License v2.0 which accompanies this distribution.
//
// The Eclipse Public License is available at
// http://www.eclipse.org/legal/epl-v10.html
//
// The Apache License v2.0 is available at
// http://www.opensource.org/licenses/apache2.0.php
//
// You may elect to redistribute this code under either of these licenses.
// ========================================================================
//
package org.eclipse.jetty.http2.client.http;
import java.util.concurrent.CountDownLatch;
import java.util.concurrent.Semaphore;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.atomic.AtomicInteger;
import java.util.concurrent.atomic.AtomicReference;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.eclipse.jetty.client.ConnectionPool;
import org.eclipse.jetty.client.HttpClient;
import org.eclipse.jetty.client.HttpClientTransport;
import org.eclipse.jetty.client.MultiplexConnectionPool;
import org.eclipse.jetty.client.api.Connection;
import org.eclipse.jetty.client.api.ContentResponse;
import org.eclipse.jetty.http2.client.HTTP2Client;
import org.eclipse.jetty.http2.server.HTTP2ServerConnectionFactory;
import org.eclipse.jetty.server.Handler;
import org.eclipse.jetty.server.HttpConfiguration;
import org.eclipse.jetty.server.Request;
import org.eclipse.jetty.server.Server;
import org.eclipse.jetty.server.ServerConnector;
import org.eclipse.jetty.util.Pool;
import org.hamcrest.Matchers;
import org.junit.jupiter.api.AfterEach;
import org.junit.jupiter.api.Test;
import static org.hamcrest.MatcherAssert.assertThat;
import static org.hamcrest.Matchers.is;
import static org.junit.jupiter.api.Assertions.assertTrue;
// Sibling of ConnectionPoolTest, but using H2 to multiplex connections.
public class MultiplexedConnectionPoolTest
{
private static final int MAX_MULTIPLEX = 2;
private Server server;
private ServerConnector connector;
private HttpClient client;
private void startServer(Handler handler) throws Exception
{
server = new Server();
HTTP2ServerConnectionFactory http2ServerConnectionFactory = new HTTP2ServerConnectionFactory(new HttpConfiguration());
http2ServerConnectionFactory.setMaxConcurrentStreams(MAX_MULTIPLEX);
connector = new ServerConnector(server, 1, 1, http2ServerConnectionFactory);
server.addConnector(connector);
server.setHandler(handler);
server.start();
}
@AfterEach
public void disposeServer() throws Exception
{
connector = null;
if (server != null)
{
server.stop();
server = null;
}
}
@AfterEach
public void disposeClient() throws Exception
{
if (client != null)
{
client.stop();
client = null;
}
}
@Test
public void testMaxDurationConnectionsWithMultiplexedPool() throws Exception
{
final int maxDuration = 30;
AtomicInteger poolCreateCounter = new AtomicInteger();
AtomicInteger poolRemoveCounter = new AtomicInteger();
AtomicReference<Pool<Connection>> poolRef = new AtomicReference<>();
ConnectionPoolFactory factory = new ConnectionPoolFactory("duplex-maxDuration", destination ->
{
int maxConnections = destination.getHttpClient().getMaxConnectionsPerDestination();
MultiplexConnectionPool connectionPool = new MultiplexConnectionPool(destination, Pool.StrategyType.FIRST, maxConnections, false, destination, MAX_MULTIPLEX)
{
@Override
protected void onCreated(Connection connection)
{
poolCreateCounter.incrementAndGet();
}
@Override
protected void removed(Connection connection)
{
poolRemoveCounter.incrementAndGet();
}
};
poolRef.set(connectionPool.getBean(Pool.class));
connectionPool.setMaxDuration(maxDuration);
return connectionPool;
});
startServer(new EmptyServerHandler());
HttpClientTransport transport = new HttpClientTransportOverHTTP2(new HTTP2Client());
transport.setConnectionPoolFactory(factory.factory);
client = new HttpClient(transport, null);
client.start();
// Use the connection pool 5 times with a delay that is longer than the max duration in between each time.
for (int i = 0; i < 5; i++)
{
ContentResponse response = client.newRequest("localhost", connector.getLocalPort())
.timeout(5, TimeUnit.SECONDS)
.send();
assertThat(response.getStatus(), Matchers.is(200));
// Check that the pool never grows above 1.
assertThat(poolRef.get().size(), is(1));
Thread.sleep(maxDuration * 2);
}
// Check that the pool created 5 and removed 4 connections;
// it must be exactly 4 removed b/c while the pool is not
// constrained, it can multiplex requests on a single connection
// so that should prevent opening more connections than needed.
assertThat(poolCreateCounter.get(), is(5));
assertThat(poolRemoveCounter.get(), is(4));
}
@Test
public void testMaxDurationConnectionsWithMultiplexedPoolClosesExpiredConnectionWhileStillInUse() throws Exception
{
final int maxDuration = 1000;
final int maxIdle = 2000;
AtomicInteger poolCreateCounter = new AtomicInteger();
AtomicInteger poolRemoveCounter = new AtomicInteger();
AtomicReference<Pool<Connection>> poolRef = new AtomicReference<>();
ConnectionPoolFactory factory = new ConnectionPoolFactory("duplex-maxDuration", destination ->
{
int maxConnections = destination.getHttpClient().getMaxConnectionsPerDestination();
MultiplexConnectionPool connectionPool = new MultiplexConnectionPool(destination, Pool.StrategyType.FIRST, maxConnections, false, destination, MAX_MULTIPLEX)
{
@Override
protected void onCreated(Connection connection)
{
poolCreateCounter.incrementAndGet();
}
@Override
protected void removed(Connection connection)
{
poolRemoveCounter.incrementAndGet();
}
};
poolRef.set(connectionPool.getBean(Pool.class));
connectionPool.setMaxDuration(maxDuration);
return connectionPool;
});
Semaphore handlerSignalingSemaphore = new Semaphore(0);
Semaphore handlerWaitingSemaphore = new Semaphore(0);
startServer(new EmptyServerHandler()
{
@Override
protected void service(String target, Request jettyRequest, HttpServletRequest request, HttpServletResponse response) throws ServletException
{
if (!target.equals("/block"))
return;
handlerSignalingSemaphore.release();
try
{
handlerWaitingSemaphore.acquire();
}
catch (Exception e)
{
throw new ServletException(e);
}
}
});
HttpClientTransport transport = new HttpClientTransportOverHTTP2(new HTTP2Client());
transport.setConnectionPoolFactory(factory.factory);
client = new HttpClient(transport, null);
client.setIdleTimeout(maxIdle);
client.start();
CountDownLatch latch1 = new CountDownLatch(1);
CountDownLatch latch2 = new CountDownLatch(2);
// create 2 requests that are going to consume all the multiplexing slots
client.newRequest("localhost", connector.getLocalPort())
.path("/block")
.timeout(5, TimeUnit.SECONDS)
.send(result ->
{
if (result.isSucceeded())
{
latch1.countDown();
latch2.countDown();
}
});
// wait for the 1st request to be serviced to make sure only 1 connection gets created
handlerSignalingSemaphore.acquire();
client.newRequest("localhost", connector.getLocalPort())
.path("/block")
.timeout(5, TimeUnit.SECONDS)
.send(result ->
{
if (result.isSucceeded())
{
latch1.countDown();
latch2.countDown();
}
});
// wait for both requests to start being serviced
handlerSignalingSemaphore.acquire();
assertThat(poolCreateCounter.get(), is(1));
// finalize 1 request, freeing up 1 multiplexing slot
handlerWaitingSemaphore.release();
// wait until 1st request finished
assertTrue(latch1.await(5, TimeUnit.SECONDS));
assertThat(poolRef.get().getInUseCount(), is(1));
assertThat(poolRef.get().getIdleCount(), is(0));
assertThat(poolRef.get().getClosedCount(), is(0));
assertThat(poolRef.get().size(), is(1));
// wait for the connection to expire
Thread.sleep(maxDuration + 500);
// send a 3rd request that will close the expired multiplexed connection
ContentResponse response = client.newRequest("localhost", connector.getLocalPort())
.path("/do-not-block")
.timeout(5, TimeUnit.SECONDS)
.send();
assertThat(response.getStatus(), is(200));
assertThat(poolRef.get().getInUseCount(), is(0));
assertThat(poolRef.get().getIdleCount(), is(1));
assertThat(poolRef.get().getClosedCount(), is(1));
assertThat(poolRef.get().size(), is(2));
// unblock 2nd request
handlerWaitingSemaphore.release();
//wait until 2nd request finished
assertTrue(latch2.await(5, TimeUnit.SECONDS));
assertThat(poolRef.get().getInUseCount(), is(0));
assertThat(poolRef.get().getIdleCount(), is(1));
assertThat(poolRef.get().getClosedCount(), is(0));
assertThat(poolRef.get().size(), is(1));
assertThat(poolCreateCounter.get(), is(2));
// wait for idle connections to be closed
Thread.sleep(maxIdle + 500);
assertThat(poolRef.get().getIdleCount(), is(0));
assertThat(poolRef.get().size(), is(0));
assertThat(poolRemoveCounter.get(), is(3));
}
private static class ConnectionPoolFactory
{
private final String name;
private final ConnectionPool.Factory factory;
private ConnectionPoolFactory(String name, ConnectionPool.Factory factory)
{
this.name = name;
this.factory = factory;
}
@Override
public String toString()
{
return name;
}
}
}