Skip to content

Commit a745b6e

Browse files
author
Stephane Landelle
committed
Clean up testMaxConnectionsWithinThreads, close AsyncHttpClient#794
1 parent 3f11433 commit a745b6e

File tree

1 file changed

+8
-11
lines changed

1 file changed

+8
-11
lines changed

api/src/test/java/org/asynchttpclient/async/MaxConnectionsInThreads.java

+8-11
Original file line numberDiff line numberDiff line change
@@ -16,17 +16,16 @@
1616
*/
1717
package org.asynchttpclient.async;
1818

19-
import static org.testng.Assert.*;
20-
2119
import static org.asynchttpclient.async.util.TestUtils.findFreePort;
2220
import static org.asynchttpclient.async.util.TestUtils.newJettyHttpServer;
21+
import static org.testng.Assert.assertEquals;
2322

2423
import java.io.IOException;
2524
import java.io.OutputStream;
2625
import java.net.URI;
2726
import java.net.URISyntaxException;
2827
import java.util.concurrent.CountDownLatch;
29-
import java.util.concurrent.atomic.AtomicReference;
28+
import java.util.concurrent.atomic.AtomicInteger;
3029

3130
import javax.servlet.ServletException;
3231
import javax.servlet.http.HttpServlet;
@@ -58,12 +57,11 @@ public void testMaxConnectionsWithinThreads() throws InterruptedException {
5857
.setMaxConnections(1).setMaxConnectionsPerHost(1).build();
5958

6059
final CountDownLatch inThreadsLatch = new CountDownLatch(2);
61-
final AtomicReference<Integer> failedRank = new AtomicReference<>(-1);
60+
final AtomicInteger failedCount = new AtomicInteger();
6261

6362
try (AsyncHttpClient client = getAsyncHttpClient(config)) {
6463
for (int i = 0; i < urls.length; i++) {
6564
final String url = urls[i];
66-
final int rank = i;
6765
Thread t = new Thread() {
6866
public void run() {
6967
client.prepareGet(url).execute(new AsyncCompletionHandlerBase() {
@@ -77,7 +75,7 @@ public Response onCompleted(Response response) throws Exception {
7775
@Override
7876
public void onThrowable(Throwable t) {
7977
super.onThrowable(t);
80-
failedRank.set(rank);
78+
failedCount.incrementAndGet();
8179
inThreadsLatch.countDown();
8280
}
8381
});
@@ -88,13 +86,12 @@ public void onThrowable(Throwable t) {
8886

8987
inThreadsLatch.await();
9088

91-
assertEquals(failedRank.get().intValue(), 1, "Max Connections should have been reached");
89+
assertEquals(failedCount.get(), 1, "Max Connections should have been reached when launching from concurrent threads");
9290

9391
final CountDownLatch notInThreadsLatch = new CountDownLatch(2);
94-
failedRank.set(-1);
92+
failedCount.set(0);
9593
for (int i = 0; i < urls.length; i++) {
9694
final String url = urls[i];
97-
final int rank = i;
9895
client.prepareGet(url).execute(new AsyncCompletionHandlerBase() {
9996
@Override
10097
public Response onCompleted(Response response) throws Exception {
@@ -106,15 +103,15 @@ public Response onCompleted(Response response) throws Exception {
106103
@Override
107104
public void onThrowable(Throwable t) {
108105
super.onThrowable(t);
109-
failedRank.set(rank);
106+
failedCount.incrementAndGet();
110107
notInThreadsLatch.countDown();
111108
}
112109
});
113110
}
114111

115112
notInThreadsLatch.await();
116113

117-
assertEquals(failedRank.get().intValue(), 1, "Max Connections should have been reached");
114+
assertEquals(failedCount.get(), 1, "Max Connections should have been reached when launching from main thread");
118115
}
119116
}
120117

0 commit comments

Comments
 (0)