Skip to content

Commit 08f3fdc

Browse files
committed
TestConnectionManagement test setup improvements
1 parent 8290f5a commit 08f3fdc

File tree

3 files changed

+120
-22
lines changed

3 files changed

+120
-22
lines changed
Lines changed: 82 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,82 @@
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+
28+
package org.apache.hc.client5.testing.extension.sync;
29+
30+
import java.util.function.Consumer;
31+
32+
import org.apache.hc.core5.http.URIScheme;
33+
import org.apache.hc.core5.io.CloseMode;
34+
import org.apache.hc.core5.util.Asserts;
35+
import org.apache.hc.core5.util.Timeout;
36+
import org.junit.jupiter.api.extension.AfterEachCallback;
37+
import org.junit.jupiter.api.extension.ExtensionContext;
38+
import org.slf4j.Logger;
39+
import org.slf4j.LoggerFactory;
40+
41+
public class TestServerResources implements AfterEachCallback {
42+
43+
private static final Logger LOG = LoggerFactory.getLogger(TestServerResources.class);
44+
45+
private final URIScheme scheme;
46+
private final Timeout timeout;
47+
private final TestServerBootstrap serverBootstrap;
48+
49+
private TestServer server;
50+
51+
public TestServerResources(final URIScheme scheme, final Timeout timeout) {
52+
this.scheme = scheme != null ? scheme : URIScheme.HTTP;
53+
this.timeout = timeout;
54+
this.serverBootstrap = new TestServerBootstrap(this.scheme)
55+
.setTimeout(this.timeout);
56+
}
57+
58+
@Override
59+
public void afterEach(final ExtensionContext extensionContext) {
60+
LOG.debug("Shutting down test server");
61+
if (server != null) {
62+
server.shutdown(CloseMode.IMMEDIATE);
63+
}
64+
}
65+
66+
public URIScheme scheme() {
67+
return this.scheme;
68+
}
69+
70+
public void configureServer(final Consumer<TestServerBootstrap> serverCustomizer) {
71+
Asserts.check(server == null, "Server is already running and cannot be changed");
72+
serverCustomizer.accept(serverBootstrap);
73+
}
74+
75+
public TestServer server() throws Exception {
76+
if (server == null) {
77+
server = serverBootstrap.build();
78+
}
79+
return server;
80+
}
81+
82+
}

httpclient5-testing/src/test/java/org/apache/hc/client5/testing/sync/TestConnectionManagement.java

Lines changed: 38 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,9 @@
2828
package org.apache.hc.client5.testing.sync;
2929

3030
import java.io.IOException;
31+
import java.net.InetSocketAddress;
3132
import java.util.concurrent.TimeoutException;
33+
import java.util.function.Consumer;
3234

3335
import org.apache.hc.client5.http.HttpRoute;
3436
import org.apache.hc.client5.http.config.ConnectionConfig;
@@ -39,8 +41,9 @@
3941
import org.apache.hc.client5.http.io.LeaseRequest;
4042
import org.apache.hc.client5.http.protocol.HttpClientContext;
4143
import org.apache.hc.client5.testing.classic.RandomHandler;
42-
import org.apache.hc.client5.testing.extension.sync.ClientProtocolLevel;
43-
import org.apache.hc.client5.testing.extension.sync.TestClient;
44+
import org.apache.hc.client5.testing.extension.sync.TestServer;
45+
import org.apache.hc.client5.testing.extension.sync.TestServerBootstrap;
46+
import org.apache.hc.client5.testing.extension.sync.TestServerResources;
4447
import org.apache.hc.core5.http.ClassicHttpRequest;
4548
import org.apache.hc.core5.http.ClassicHttpResponse;
4649
import org.apache.hc.core5.http.HttpConnection;
@@ -57,28 +60,48 @@
5760
import org.apache.hc.core5.http.protocol.RequestConnControl;
5861
import org.apache.hc.core5.http.protocol.RequestContent;
5962
import org.apache.hc.core5.http.protocol.RequestTargetHost;
63+
import org.apache.hc.core5.io.CloseMode;
6064
import org.apache.hc.core5.pool.PoolConcurrencyPolicy;
6165
import org.apache.hc.core5.pool.PoolReusePolicy;
6266
import org.apache.hc.core5.util.TimeValue;
6367
import org.apache.hc.core5.util.Timeout;
68+
import org.junit.jupiter.api.AfterEach;
6469
import org.junit.jupiter.api.Assertions;
6570
import org.junit.jupiter.api.BeforeEach;
6671
import org.junit.jupiter.api.Test;
72+
import org.junit.jupiter.api.extension.RegisterExtension;
6773

6874
/**
6975
* Tests for {@code PoolingHttpClientConnectionManager} that do require a server
7076
* to communicate with.
7177
*/
72-
class TestConnectionManagement extends AbstractIntegrationTestBase {
78+
class TestConnectionManagement {
7379

74-
public TestConnectionManagement() {
75-
super(URIScheme.HTTP, ClientProtocolLevel.STANDARD);
76-
}
80+
public static final Timeout TIMEOUT = Timeout.ofMinutes(1);
81+
82+
@RegisterExtension
83+
private final TestServerResources testResources;
7784

85+
PoolingHttpClientConnectionManager connManager;
7886
ConnectionEndpoint.RequestExecutor exec;
7987

88+
TestConnectionManagement() {
89+
this.testResources = new TestServerResources(URIScheme.HTTP, TIMEOUT);
90+
}
91+
92+
public void configureServer(final Consumer<TestServerBootstrap> serverCustomizer) {
93+
testResources.configureServer(serverCustomizer);
94+
}
95+
96+
public HttpHost startServer() throws Exception {
97+
final TestServer server = testResources.server();
98+
final InetSocketAddress inetSocketAddress = server.start();
99+
return new HttpHost(testResources.scheme().id, "localhost", inetSocketAddress.getPort());
100+
}
101+
80102
@BeforeEach
81103
void setup() {
104+
connManager = new PoolingHttpClientConnectionManager();
82105
exec = new ConnectionEndpoint.RequestExecutor() {
83106

84107
final HttpRequestExecutor requestExecutor = new HttpRequestExecutor();
@@ -97,6 +120,13 @@ public ClassicHttpResponse execute(final ClassicHttpRequest request,
97120
};
98121
}
99122

123+
@AfterEach
124+
void cleanup() {
125+
if (connManager != null) {
126+
connManager.close(CloseMode.IMMEDIATE);
127+
}
128+
}
129+
100130
/**
101131
* Tests releasing and re-using a connection after a response is read.
102132
*/
@@ -106,8 +136,6 @@ void testReleaseConnection() throws Exception {
106136
.register("/random/*", new RandomHandler()));
107137
final HttpHost target = startServer();
108138

109-
final TestClient client = client();
110-
final PoolingHttpClientConnectionManager connManager = client.getConnectionManager();
111139
connManager.setMaxTotal(1);
112140

113141
final HttpRoute route = new HttpRoute(target, null, false);
@@ -171,8 +199,6 @@ void testReleaseConnectionWithTimeLimits() throws Exception {
171199
.register("/random/*", new RandomHandler()));
172200
final HttpHost target = startServer();
173201

174-
final TestClient client = client();
175-
final PoolingHttpClientConnectionManager connManager = client.getConnectionManager();
176202
connManager.setMaxTotal(1);
177203

178204
final HttpRoute route = new HttpRoute(target, null, false);
@@ -239,8 +265,6 @@ void testReleaseConnectionWithTimeLimits() throws Exception {
239265
@Test
240266
void testCloseExpiredIdleConnections() throws Exception {
241267
final HttpHost target = startServer();
242-
final TestClient client = client();
243-
final PoolingHttpClientConnectionManager connManager = client.getConnectionManager();
244268
connManager.setMaxTotal(1);
245269

246270
final HttpRoute route = new HttpRoute(target, null, false);
@@ -281,18 +305,13 @@ void testCloseExpiredTTLConnections() throws Exception {
281305
configureServer(bootstrap -> bootstrap
282306
.register("/random/*", new RandomHandler()));
283307
final HttpHost target = startServer();
284-
285-
configureClient(builder -> builder
286-
.setConnectionManager(PoolingHttpClientConnectionManagerBuilder.create()
308+
connManager = PoolingHttpClientConnectionManagerBuilder.create()
287309
.setPoolConcurrencyPolicy(PoolConcurrencyPolicy.STRICT)
288310
.setConnPoolPolicy(PoolReusePolicy.LIFO)
289311
.setDefaultConnectionConfig(ConnectionConfig.custom()
290312
.setTimeToLive(TimeValue.ofMilliseconds(100))
291313
.build())
292-
.build()));
293-
final TestClient client = client();
294-
295-
final PoolingHttpClientConnectionManager connManager = client.getConnectionManager();
314+
.build();
296315
connManager.setMaxTotal(1);
297316

298317
final HttpRoute route = new HttpRoute(target, null, false);
@@ -336,8 +355,6 @@ void testConnectionTimeoutSetting() throws Exception {
336355

337356
final Timeout connectionSocketTimeout = Timeout.ofMinutes(5);
338357

339-
final TestClient client = client();
340-
final PoolingHttpClientConnectionManager connManager = client.getConnectionManager();
341358
connManager.setMaxTotal(1);
342359
connManager.setDefaultConnectionConfig(ConnectionConfig.custom()
343360
.setSocketTimeout(connectionSocketTimeout)

pom.xml

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -69,7 +69,6 @@
6969
<ehcache.version>3.11.1</ehcache.version>
7070
<memcached.version>2.12.3</memcached.version>
7171
<slf4j.version>1.7.36</slf4j.version>
72-
<junit.version>5.14.2</junit.version>
7372
<junit.version>5.14.3</junit.version>
7473
<mockito.version>4.11.0</mockito.version>
7574
<hc.stylecheck.version>1</hc.stylecheck.version>

0 commit comments

Comments
 (0)