2828package org .apache .hc .client5 .testing .sync ;
2929
3030import java .io .IOException ;
31+ import java .net .InetSocketAddress ;
3132import java .util .concurrent .TimeoutException ;
33+ import java .util .function .Consumer ;
3234
3335import org .apache .hc .client5 .http .HttpRoute ;
3436import org .apache .hc .client5 .http .config .ConnectionConfig ;
3941import org .apache .hc .client5 .http .io .LeaseRequest ;
4042import org .apache .hc .client5 .http .protocol .HttpClientContext ;
4143import 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 ;
4447import org .apache .hc .core5 .http .ClassicHttpRequest ;
4548import org .apache .hc .core5 .http .ClassicHttpResponse ;
4649import org .apache .hc .core5 .http .HttpConnection ;
5760import org .apache .hc .core5 .http .protocol .RequestConnControl ;
5861import org .apache .hc .core5 .http .protocol .RequestContent ;
5962import org .apache .hc .core5 .http .protocol .RequestTargetHost ;
63+ import org .apache .hc .core5 .io .CloseMode ;
6064import org .apache .hc .core5 .pool .PoolConcurrencyPolicy ;
6165import org .apache .hc .core5 .pool .PoolReusePolicy ;
6266import org .apache .hc .core5 .util .TimeValue ;
6367import org .apache .hc .core5 .util .Timeout ;
68+ import org .junit .jupiter .api .AfterEach ;
6469import org .junit .jupiter .api .Assertions ;
6570import org .junit .jupiter .api .BeforeEach ;
6671import 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 )
0 commit comments