3333import org .apache .http .util .EntityUtils ;
3434import org .junit .After ;
3535import org .junit .Before ;
36+ import org .junit .Ignore ;
3637import org .junit .Test ;
3738
3839public class HttpClientConnectionManagementTest {
@@ -58,31 +59,37 @@ public class HttpClientConnectionManagementTest {
5859 public final void before () {
5960 get1 = new HttpGet (SERVER1 );
6061 get2 = new HttpGet (SERVER7 );
61- route = new HttpRoute (new HttpHost ("localhost " , 80 ));
62+ route = new HttpRoute (new HttpHost ("www.baeldung.com " , 80 ));
6263 }
6364
6465 @ After
6566 public final void after () throws IllegalStateException , IOException {
66- if (conn != null )
67+ if (conn != null ) {
6768 conn .close ();
68- if (conn1 != null )
69+ }
70+ if (conn1 != null ) {
6971 conn1 .close ();
70- if (conn2 != null )
72+ }
73+ if (conn2 != null ) {
7174 conn2 .close ();
72- if (poolingConnManager != null )
75+ }
76+ if (poolingConnManager != null ) {
7377 poolingConnManager .shutdown ();
74- if (basicConnManager != null )
78+ }
79+ if (basicConnManager != null ) {
7580 basicConnManager .shutdown ();
76- if (client != null )
81+ }
82+ if (client != null ) {
7783 client .close ();
78- if (response != null )
84+ }
85+ if (response != null ) {
7986 response .close ();
87+ }
8088 }
8189
8290 // 2
8391
8492 @ Test
85- // @Ignore
8693 // 2.1 IN ARTCLE
8794 public final void whenLowLevelConnectionIsEstablished_thenNoExceptions () throws IOException , HttpException , InterruptedException , ExecutionException {
8895 basicConnManager = new BasicHttpClientConnectionManager ();
@@ -98,8 +105,9 @@ public final void whenOpeningLowLevelConnectionWithSocketTimeout_thenNoException
98105 context = HttpClientContext .create ();
99106 final ConnectionRequest connRequest = basicConnManager .requestConnection (route , null );
100107 conn = connRequest .get (1000 , TimeUnit .SECONDS );
101- if (!conn .isOpen ())
108+ if (!conn .isOpen ()) {
102109 basicConnManager .connect (conn , route , 1000 , context );
110+ }
103111 conn .setSocketTimeout (30000 );
104112
105113 assertTrue (conn .getSocketTimeout () == 30000 );
@@ -109,6 +117,7 @@ public final void whenOpeningLowLevelConnectionWithSocketTimeout_thenNoException
109117 // 3
110118
111119 @ Test
120+ // Example 3.1.
112121 public final void whenPollingConnectionManagerIsConfiguredOnHttpClient_thenNoExceptions () throws InterruptedException , ClientProtocolException , IOException {
113122 poolingConnManager = new PoolingHttpClientConnectionManager ();
114123 client = HttpClients .custom ().setConnectionManager (poolingConnManager ).build ();
@@ -127,8 +136,8 @@ public final void whenPollingConnectionManagerIsConfiguredOnHttpClient_thenNoExc
127136 final TesterVersion_MultiHttpClientConnThread thread1 = new TesterVersion_MultiHttpClientConnThread (client1 , get1 , poolingConnManager );
128137 final TesterVersion_MultiHttpClientConnThread thread2 = new TesterVersion_MultiHttpClientConnThread (client2 , get2 , poolingConnManager );
129138 thread1 .start ();
130- thread1 .join ();
131139 thread2 .start ();
140+ thread1 .join ();
132141 thread2 .join (1000 );
133142 assertTrue (poolingConnManager .getTotalStats ().getLeased () == 2 );
134143 }
@@ -148,42 +157,40 @@ public final void whenTwoConnectionsForTwoRequests_thenNoExceptions() throws Int
148157 thread2 .join ();
149158 }
150159
160+ // 4
161+
151162 @ Test
152- // @Ignore
153- // 3.4
163+ // Example 4.1
154164 public final void whenIncreasingConnectionPool_thenNoEceptions () {
155165 poolingConnManager = new PoolingHttpClientConnectionManager ();
156166 poolingConnManager .setMaxTotal (5 );
157167 poolingConnManager .setDefaultMaxPerRoute (4 );
168+
158169 final HttpHost localhost = new HttpHost ("locahost" , 80 );
159170 poolingConnManager .setMaxPerRoute (new HttpRoute (localhost ), 5 );
160171 }
161172
162173 @ Test
163174 // @Ignore
164- // 3.5 Tester Version
175+ // 4.2 Tester Version
165176 /*tester*/ public final void whenExecutingSameRequestsInDifferentThreads_thenUseDefaultConnLimit () throws InterruptedException , IOException {
166- final HttpGet get = new HttpGet ("http://google.com" );
167177 poolingConnManager = new PoolingHttpClientConnectionManager ();
168178 client = HttpClients .custom ().setConnectionManager (poolingConnManager ).build ();
169- final TesterVersion_MultiHttpClientConnThread thread1 = new TesterVersion_MultiHttpClientConnThread (client , get , poolingConnManager );
170- final TesterVersion_MultiHttpClientConnThread thread2 = new TesterVersion_MultiHttpClientConnThread (client , get , poolingConnManager );
171- final TesterVersion_MultiHttpClientConnThread thread3 = new TesterVersion_MultiHttpClientConnThread (client , get , poolingConnManager );
179+ final TesterVersion_MultiHttpClientConnThread thread1 = new TesterVersion_MultiHttpClientConnThread (client , new HttpGet ( "http://www.google.com" ) , poolingConnManager );
180+ final TesterVersion_MultiHttpClientConnThread thread2 = new TesterVersion_MultiHttpClientConnThread (client , new HttpGet ( "http://www.google.com" ) , poolingConnManager );
181+ final TesterVersion_MultiHttpClientConnThread thread3 = new TesterVersion_MultiHttpClientConnThread (client , new HttpGet ( "http://www.google.com" ) , poolingConnManager );
172182 thread1 .start ();
173- thread1 .join (1000 );
174- assertTrue (poolingConnManager .getTotalStats ().getLeased () == 1 );
175183 thread2 .start ();
176- thread2 .join (1000 );
177- assertTrue (poolingConnManager .getTotalStats ().getLeased () == 2 );
178184 thread3 .start ();
179- thread3 .join (1000 );
185+ thread1 .join (10000 );
186+ thread2 .join (10000 );
187+ thread3 .join (10000 );
180188 }
181189
182190 @ Test
183- // @Ignore
184- // 3.5 Article version
191+ // 4.2 Article version
185192 public final void whenExecutingSameRequestsInDifferentThreads_thenExecuteReuqest () throws InterruptedException {
186- final HttpGet get = new HttpGet ("http://localhost " );
193+ final HttpGet get = new HttpGet ("http://www.google.com " );
187194 poolingConnManager = new PoolingHttpClientConnectionManager ();
188195 client = HttpClients .custom ().setConnectionManager (poolingConnManager ).build ();
189196 final MultiHttpClientConnThread thread1 = new MultiHttpClientConnThread (client , get );
@@ -197,11 +204,11 @@ public final void whenExecutingSameRequestsInDifferentThreads_thenExecuteReuqest
197204 thread3 .join ();
198205 }
199206
200- // 4
207+ // 5
201208
202209 @ Test
203210 // @Ignore
204- // 4 .1
211+ // 5 .1
205212 public final void whenCustomizingKeepAliveStrategy_thenNoExceptions () throws ClientProtocolException , IOException {
206213 final ConnectionKeepAliveStrategy myStrategy = new ConnectionKeepAliveStrategy () {
207214 @ Override
@@ -229,37 +236,36 @@ public long getKeepAliveDuration(final HttpResponse myResponse, final HttpContex
229236 client .execute (get2 );
230237 }
231238
232- // 5
239+ // 6
233240
234241 @ Test
235242 // @Ignore
236- // 5 .1
243+ // 6 .1
237244 public final void givenBasicHttpClientConnManager_whenConnectionReuse_thenNoExceptions () throws InterruptedException , ExecutionException , IOException , HttpException {
238245 basicConnManager = new BasicHttpClientConnectionManager ();
239246 context = HttpClientContext .create ();
240- final HttpGet get = new HttpGet ("http://localhost" );
241- HttpResponse thisResponse = null ;
247+
242248 final ConnectionRequest connRequest = basicConnManager .requestConnection (route , null );
243- client = HttpClients .custom ().setConnectionManager (basicConnManager ).build ();
244- boolean respAvail = false ;
245249 conn = connRequest .get (10 , TimeUnit .SECONDS );
246- if (!conn .isOpen ()) {
247- basicConnManager .connect (conn , route , 1000 , context );
248- basicConnManager .routeComplete (conn , route , context );
249- final HttpRequestExecutor exeRequest = new HttpRequestExecutor ();
250- context .setTargetHost ((new HttpHost ("localhost" , 80 )));
251- thisResponse = exeRequest .execute (get , conn , context );
252- respAvail = conn .isResponseAvailable (1000 );
253- }
250+
251+ basicConnManager .connect (conn , route , 1000 , context );
252+ basicConnManager .routeComplete (conn , route , context );
253+ final HttpRequestExecutor exeRequest = new HttpRequestExecutor ();
254+ context .setTargetHost ((new HttpHost ("www.baeldung.com" , 80 )));
255+
256+ final HttpGet get = new HttpGet ("http://www.baeldung.com" );
257+ exeRequest .execute (get , conn , context );
258+ conn .isResponseAvailable (1000 );
254259 basicConnManager .releaseConnection (conn , null , 1 , TimeUnit .SECONDS );
255- if (respAvail ) {
256- client .execute (get );
257- }
260+
261+ //
262+ client = HttpClients .custom ().setConnectionManager (basicConnManager ).build ();
263+ client .execute (get );
258264 }
259265
260266 @ Test
261267 // @Ignore
262- // 5 .2 TESTER VERSION
268+ // 6 .2 TESTER VERSION
263269 /*tester*/ public final void whenConnectionsNeededGreaterThanMaxTotal_thenReuseConnections () throws InterruptedException {
264270 poolingConnManager = new PoolingHttpClientConnectionManager ();
265271 poolingConnManager .setDefaultMaxPerRoute (5 );
@@ -282,7 +288,7 @@ public final void givenBasicHttpClientConnManager_whenConnectionReuse_thenNoExce
282288 }
283289
284290 @ Test
285- // 5 .2 ARTICLE VERSION
291+ // 7 .2 ARTICLE VERSION
286292 // @Ignore
287293 public final void whenConnectionsNeededGreaterThanMaxTotal_thenLeaseMasTotalandReuse () throws InterruptedException {
288294 final HttpGet get = new HttpGet ("http://echo.200please.com" );
@@ -304,25 +310,27 @@ public final void whenConnectionsNeededGreaterThanMaxTotal_thenLeaseMasTotalandR
304310
305311 @ Test
306312 // @Ignore
307- // 6 .2.1
313+ // 7 .2.1
308314 public final void whenConfiguringTimeOut_thenNoExceptions () {
309315 route = new HttpRoute (new HttpHost ("localhost" , 80 ));
310316 poolingConnManager = new PoolingHttpClientConnectionManager ();
311317 poolingConnManager .setSocketConfig (route .getTargetHost (), SocketConfig .custom ().setSoTimeout (5000 ).build ());
312318 assertTrue (poolingConnManager .getSocketConfig (route .getTargetHost ()).getSoTimeout () == 5000 );
313319 }
314320
321+ // 8
322+
315323 @ Test
316324 // @Ignore
317- // 7 .1
325+ // 8 .1
318326 public final void whenHttpClientChecksStaleConns_thenNoExceptions () {
319327 poolingConnManager = new PoolingHttpClientConnectionManager ();
320328 client = HttpClients .custom ().setDefaultRequestConfig (RequestConfig .custom ().setStaleConnectionCheckEnabled (true ).build ()).setConnectionManager (poolingConnManager ).build ();
321329 }
322330
323331 @ Test
324- // @Ignore
325- // 7 .2 TESTER VERSION
332+ @ Ignore ( "Very Long Running" )
333+ // 8 .2 TESTER VERSION
326334 /*tester*/ public final void whenCustomizedIdleConnMonitor_thenEliminateIdleConns () throws InterruptedException , IOException {
327335 poolingConnManager = new PoolingHttpClientConnectionManager ();
328336 client = HttpClients .custom ().setConnectionManager (poolingConnManager ).build ();
@@ -345,7 +353,7 @@ public final void whenHttpClientChecksStaleConns_thenNoExceptions() {
345353
346354 @ Test
347355 // @Ignore
348- // 7 .2 ARTICLE VERSION
356+ // 8 .2 ARTICLE VERSION
349357 public final void whenCustomizedIdleConnMonitor_thenNoExceptions () throws InterruptedException , IOException {
350358 final HttpGet get = new HttpGet ("http://google.com" );
351359 poolingConnManager = new PoolingHttpClientConnectionManager ();
@@ -355,9 +363,11 @@ public final void whenCustomizedIdleConnMonitor_thenNoExceptions() throws Interr
355363 staleMonitor .join (1000 );
356364 }
357365
366+ // 9
367+
358368 @ Test (expected = IllegalStateException .class )
359369 // @Ignore
360- // 8 .1
370+ // 9 .1
361371 public final void whenClosingConnectionsandManager_thenCloseWithNoExceptions () throws InterruptedException , ExecutionException , IOException , HttpException {
362372 route = new HttpRoute (new HttpHost ("google.com" , 80 ));
363373 final HttpGet get = new HttpGet ("http://google.com" );
0 commit comments