Skip to content

Commit 5991755

Browse files
author
eugenp
committed
connection management work
1 parent 0a76200 commit 5991755

File tree

1 file changed

+46
-25
lines changed

1 file changed

+46
-25
lines changed

httpclient/src/test/java/org/baeldung/httpclient/HttpClientConnectionManagementTest.java

Lines changed: 46 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -36,18 +36,22 @@
3636
import org.junit.Test;
3737

3838
public class HttpClientConnectionManagementTest {
39+
private static final String SERVER1 = "http://www.petrikainulainen.net/";
40+
private static final String SERVER7 = "http://www.baeldung.com/";
41+
3942
private BasicHttpClientConnectionManager basicConnManager;
43+
private PoolingHttpClientConnectionManager poolingConnManager;
44+
4045
private HttpClientContext context;
4146
private HttpRoute route;
42-
private static final String SERVER1 = "http://echo.200please.com";
43-
private static final String SERVER7 = "http://localhost";
44-
private HttpGet get1;
45-
private HttpGet get2;
46-
private static CloseableHttpResponse response;
4747
private HttpClientConnection conn1;
4848
private HttpClientConnection conn;
4949
private HttpClientConnection conn2;
50-
private PoolingHttpClientConnectionManager poolingConnManager;
50+
51+
private CloseableHttpResponse response;
52+
private HttpGet get1;
53+
private HttpGet get2;
54+
5155
private CloseableHttpClient client;
5256

5357
@Before
@@ -75,7 +79,7 @@ public final void after() throws IllegalStateException, IOException {
7579
response.close();
7680
}
7781

78-
// tests
82+
// 2
7983

8084
@Test
8185
// @Ignore
@@ -102,12 +106,11 @@ public final void whenOpeningLowLevelConnectionWithSocketTimeout_thenNoException
102106
assertTrue(conn.isOpen());
103107
}
104108

109+
// 3
110+
105111
@Test
106112
// @Ignore
107-
// Example 3.1. TESTER VERSION
108-
public final void WhenTwoConnectionsForTwoRequests_ThenLeaseTwoConnectionsNoExceptions() throws InterruptedException {
109-
get1 = new HttpGet("http://www.petrikainulainen.net/");
110-
get2 = new HttpGet("http://www.baeldung.com/");
113+
public final void whenPollingConnectionManagerIsConfiguredOnHttpClient_thenNoExceptions() throws InterruptedException {
111114
poolingConnManager = new PoolingHttpClientConnectionManager();
112115
final CloseableHttpClient client1 = HttpClients.custom().setConnectionManager(poolingConnManager).build();
113116
final CloseableHttpClient client2 = HttpClients.custom().setConnectionManager(poolingConnManager).build();
@@ -122,10 +125,24 @@ public final void WhenTwoConnectionsForTwoRequests_ThenLeaseTwoConnectionsNoExce
122125

123126
@Test
124127
// @Ignore
125-
// Example 3.1.ARTICLE VERSION
126-
public final void WhenTwoConnectionsForTwoRequests_ThensNoExceptions() throws InterruptedException {
127-
get1 = new HttpGet("http://localhost");
128-
get2 = new HttpGet("http://google.com");
128+
// Example 3.2. TESTER VERSION
129+
/*tester*/public final void whenTwoConnectionsForTwoRequests_thenTwoConnectionsAreLeased() throws InterruptedException {
130+
poolingConnManager = new PoolingHttpClientConnectionManager();
131+
final CloseableHttpClient client1 = HttpClients.custom().setConnectionManager(poolingConnManager).build();
132+
final CloseableHttpClient client2 = HttpClients.custom().setConnectionManager(poolingConnManager).build();
133+
final TesterVersion_MultiHttpClientConnThread thread1 = new TesterVersion_MultiHttpClientConnThread(client1, get1, poolingConnManager);
134+
final TesterVersion_MultiHttpClientConnThread thread2 = new TesterVersion_MultiHttpClientConnThread(client2, get2, poolingConnManager);
135+
thread1.start();
136+
thread1.join();
137+
thread2.start();
138+
thread2.join(1000);
139+
assertTrue(poolingConnManager.getTotalStats().getLeased() == 2);
140+
}
141+
142+
@Test
143+
// @Ignore
144+
// Example 3.2. ARTICLE VERSION
145+
public final void whenTwoConnectionsForTwoRequests_thenNoExceptions() throws InterruptedException {
129146
poolingConnManager = new PoolingHttpClientConnectionManager();
130147
final CloseableHttpClient client1 = HttpClients.custom().setConnectionManager(poolingConnManager).build();
131148
final CloseableHttpClient client2 = HttpClients.custom().setConnectionManager(poolingConnManager).build();
@@ -139,7 +156,7 @@ public final void WhenTwoConnectionsForTwoRequests_ThensNoExceptions() throws In
139156

140157
@Test
141158
// @Ignore
142-
// 3.3
159+
// 3.4
143160
public final void whenIncreasingConnectionPool_thenNoEceptions() {
144161
poolingConnManager = new PoolingHttpClientConnectionManager();
145162
poolingConnManager.setMaxTotal(5);
@@ -150,8 +167,8 @@ public final void whenIncreasingConnectionPool_thenNoEceptions() {
150167

151168
@Test
152169
// @Ignore
153-
// 3.4 Tester Version
154-
public final void whenExecutingSameRequestsInDifferentThreads_thenUseDefaultConnLimitNoExceptions() throws InterruptedException, IOException {
170+
// 3.5 Tester Version
171+
/*tester*/public final void whenExecutingSameRequestsInDifferentThreads_thenUseDefaultConnLimit() throws InterruptedException, IOException {
155172
final HttpGet get = new HttpGet("http://google.com");
156173
poolingConnManager = new PoolingHttpClientConnectionManager();
157174
client = HttpClients.custom().setConnectionManager(poolingConnManager).build();
@@ -170,8 +187,8 @@ public final void whenExecutingSameRequestsInDifferentThreads_thenUseDefaultConn
170187

171188
@Test
172189
// @Ignore
173-
// 3.4 Article version
174-
public final void whenExecutingSameRequestsInDifferentThreads_thenExxecuteReuqesttNoExceptions() throws InterruptedException {
190+
// 3.5 Article version
191+
public final void whenExecutingSameRequestsInDifferentThreads_thenExecuteReuqest() throws InterruptedException {
175192
final HttpGet get = new HttpGet("http://localhost");
176193
poolingConnManager = new PoolingHttpClientConnectionManager();
177194
client = HttpClients.custom().setConnectionManager(poolingConnManager).build();
@@ -186,6 +203,8 @@ public final void whenExecutingSameRequestsInDifferentThreads_thenExxecuteReuqes
186203
thread3.join();
187204
}
188205

206+
// 4
207+
189208
@Test
190209
// @Ignore
191210
// 4.1
@@ -216,10 +235,12 @@ public long getKeepAliveDuration(final HttpResponse myResponse, final HttpContex
216235
client.execute(get2);
217236
}
218237

238+
// 5
239+
219240
@Test
220241
// @Ignore
221242
// 5.1
222-
public final void GivenBasicHttpClientConnManager_whenConnectionReuse_thenNoExceptions() throws InterruptedException, ExecutionException, IOException, HttpException {
243+
public final void givenBasicHttpClientConnManager_whenConnectionReuse_thenNoExceptions() throws InterruptedException, ExecutionException, IOException, HttpException {
223244
basicConnManager = new BasicHttpClientConnectionManager();
224245
context = HttpClientContext.create();
225246
final HttpGet get = new HttpGet("http://localhost");
@@ -245,7 +266,7 @@ public final void GivenBasicHttpClientConnManager_whenConnectionReuse_thenNoExce
245266
@Test
246267
// @Ignore
247268
// 5.2 TESTER VERSION
248-
public final void WhenConnectionsNeededGreaterThanMaxTotal_thenReuseConnectionsNoExceptions() throws InterruptedException {
269+
/*tester*/public final void whenConnectionsNeededGreaterThanMaxTotal_thenReuseConnections() throws InterruptedException {
249270
poolingConnManager = new PoolingHttpClientConnectionManager();
250271
poolingConnManager.setDefaultMaxPerRoute(5);
251272
poolingConnManager.setMaxTotal(5);
@@ -266,10 +287,10 @@ public final void WhenConnectionsNeededGreaterThanMaxTotal_thenReuseConnectionsN
266287
}
267288
}
268289

269-
// 5.2 ARTICLE VERSION
270290
@Test
291+
// 5.2 ARTICLE VERSION
271292
// @Ignore
272-
public final void WhenConnectionsNeededGreaterThanMaxTotal_thenLeaseMasTotalandReuseNoExceptions() throws InterruptedException {
293+
public final void whenConnectionsNeededGreaterThanMaxTotal_thenLeaseMasTotalandReuse() throws InterruptedException {
273294
final HttpGet get = new HttpGet("http://echo.200please.com");
274295
poolingConnManager = new PoolingHttpClientConnectionManager();
275296
poolingConnManager.setDefaultMaxPerRoute(5);
@@ -308,7 +329,7 @@ public final void whenHttpClientChecksStaleConns_thenNoExceptions() {
308329
@Test
309330
// @Ignore
310331
// 7.2 TESTER VERSION
311-
public final void whenCustomizedIdleConnMonitor_thenEliminateIdleConnsNoExceptions() throws InterruptedException, IOException {
332+
/*tester*/public final void whenCustomizedIdleConnMonitor_thenEliminateIdleConns() throws InterruptedException, IOException {
312333
poolingConnManager = new PoolingHttpClientConnectionManager();
313334
client = HttpClients.custom().setConnectionManager(poolingConnManager).build();
314335
final IdleConnectionMonitorThread staleMonitor = new IdleConnectionMonitorThread(poolingConnManager);

0 commit comments

Comments
 (0)