16
16
*/
17
17
package org .asynchttpclient .async ;
18
18
19
- import static org .testng .Assert .*;
20
-
21
19
import static org .asynchttpclient .async .util .TestUtils .findFreePort ;
22
20
import static org .asynchttpclient .async .util .TestUtils .newJettyHttpServer ;
21
+ import static org .testng .Assert .assertEquals ;
23
22
24
23
import java .io .IOException ;
25
24
import java .io .OutputStream ;
26
25
import java .net .URI ;
27
26
import java .net .URISyntaxException ;
28
27
import java .util .concurrent .CountDownLatch ;
29
- import java .util .concurrent .atomic .AtomicReference ;
28
+ import java .util .concurrent .atomic .AtomicInteger ;
30
29
31
30
import javax .servlet .ServletException ;
32
31
import javax .servlet .http .HttpServlet ;
@@ -58,12 +57,11 @@ public void testMaxConnectionsWithinThreads() throws InterruptedException {
58
57
.setMaxConnections (1 ).setMaxConnectionsPerHost (1 ).build ();
59
58
60
59
final CountDownLatch inThreadsLatch = new CountDownLatch (2 );
61
- final AtomicReference < Integer > failedRank = new AtomicReference <>(- 1 );
60
+ final AtomicInteger failedCount = new AtomicInteger ( );
62
61
63
62
try (AsyncHttpClient client = getAsyncHttpClient (config )) {
64
63
for (int i = 0 ; i < urls .length ; i ++) {
65
64
final String url = urls [i ];
66
- final int rank = i ;
67
65
Thread t = new Thread () {
68
66
public void run () {
69
67
client .prepareGet (url ).execute (new AsyncCompletionHandlerBase () {
@@ -77,7 +75,7 @@ public Response onCompleted(Response response) throws Exception {
77
75
@ Override
78
76
public void onThrowable (Throwable t ) {
79
77
super .onThrowable (t );
80
- failedRank . set ( rank );
78
+ failedCount . incrementAndGet ( );
81
79
inThreadsLatch .countDown ();
82
80
}
83
81
});
@@ -88,13 +86,12 @@ public void onThrowable(Throwable t) {
88
86
89
87
inThreadsLatch .await ();
90
88
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 " );
92
90
93
91
final CountDownLatch notInThreadsLatch = new CountDownLatch (2 );
94
- failedRank .set (- 1 );
92
+ failedCount .set (0 );
95
93
for (int i = 0 ; i < urls .length ; i ++) {
96
94
final String url = urls [i ];
97
- final int rank = i ;
98
95
client .prepareGet (url ).execute (new AsyncCompletionHandlerBase () {
99
96
@ Override
100
97
public Response onCompleted (Response response ) throws Exception {
@@ -106,15 +103,15 @@ public Response onCompleted(Response response) throws Exception {
106
103
@ Override
107
104
public void onThrowable (Throwable t ) {
108
105
super .onThrowable (t );
109
- failedRank . set ( rank );
106
+ failedCount . incrementAndGet ( );
110
107
notInThreadsLatch .countDown ();
111
108
}
112
109
});
113
110
}
114
111
115
112
notInThreadsLatch .await ();
116
113
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 " );
118
115
}
119
116
}
120
117
0 commit comments