20
20
import static org .testng .Assert .assertNull ;
21
21
import static org .testng .Assert .fail ;
22
22
23
+ import java .io .IOException ;
24
+ import java .util .ArrayList ;
25
+ import java .util .List ;
26
+ import java .util .Map ;
27
+ import java .util .concurrent .ConcurrentHashMap ;
28
+ import java .util .concurrent .CountDownLatch ;
29
+ import java .util .concurrent .ExecutionException ;
30
+ import java .util .concurrent .TimeUnit ;
31
+ import java .util .concurrent .atomic .AtomicInteger ;
32
+
23
33
import org .asynchttpclient .AsyncCompletionHandler ;
24
34
import org .asynchttpclient .AsyncCompletionHandlerBase ;
25
35
import org .asynchttpclient .AsyncHttpClient ;
26
36
import org .asynchttpclient .AsyncHttpClientConfig ;
37
+ import org .asynchttpclient .ListenableFuture ;
27
38
import org .asynchttpclient .Response ;
28
39
import org .slf4j .Logger ;
29
40
import org .slf4j .LoggerFactory ;
30
41
import org .testng .annotations .Test ;
31
42
32
- import java .io .IOException ;
33
- import java .util .Map ;
34
- import java .util .concurrent .ConcurrentHashMap ;
35
- import java .util .concurrent .CountDownLatch ;
36
- import java .util .concurrent .ExecutionException ;
37
- import java .util .concurrent .TimeUnit ;
38
- import java .util .concurrent .atomic .AtomicInteger ;
39
-
40
43
public abstract class ConnectionPoolTest extends AbstractBasicTest {
41
44
protected final Logger log = LoggerFactory .getLogger (AbstractBasicTest .class );
42
45
@@ -63,29 +66,30 @@ public void testMaxTotalConnections() {
63
66
}
64
67
65
68
@ Test (groups = { "standalone" , "default_provider" })
66
- public void testMaxTotalConnectionsException () {
69
+ public void testMaxTotalConnectionsException () throws IOException {
67
70
AsyncHttpClient client = getAsyncHttpClient (new AsyncHttpClientConfig .Builder ().setAllowPoolingConnections (true ).setMaxConnections (1 ).build ());
68
71
try {
69
72
String url = getTargetUrl ();
70
- int i ;
73
+
74
+ List <ListenableFuture <Response >> futures = new ArrayList <>();
75
+ for (int i = 0 ; i < 5 ; i ++) {
76
+ log .info ("{} requesting url [{}]..." , i , url );
77
+ futures .add (client .prepareGet (url ).execute ());
78
+ }
79
+
71
80
Exception exception = null ;
72
- for (i = 0 ; i < 20 ; i ++ ) {
81
+ for (ListenableFuture < Response > future : futures ) {
73
82
try {
74
- log .info ("{} requesting url [{}]..." , i , url );
75
-
76
- if (i < 5 ) {
77
- client .prepareGet (url ).execute ().get ();
78
- } else {
79
- client .prepareGet (url ).execute ();
80
- }
83
+ future .get ();
81
84
} catch (Exception ex ) {
82
85
exception = ex ;
83
86
break ;
84
87
}
85
88
}
89
+
86
90
assertNotNull (exception );
87
- assertNotNull (exception .getMessage ());
88
- assertEquals (exception .getMessage (), "Too many connections 1" );
91
+ assertNotNull (exception .getCause ());
92
+ assertEquals (exception .getCause (). getMessage (), "Too many connections 1" );
89
93
} finally {
90
94
client .close ();
91
95
}
@@ -152,7 +156,8 @@ public void multipleMaxConnectionOpenTest() throws Exception {
152
156
exception = ex ;
153
157
}
154
158
assertNotNull (exception );
155
- assertEquals (exception .getMessage (), "Too many connections 1" );
159
+ assertNotNull (exception .getCause ());
160
+ assertEquals (exception .getCause ().getMessage (), "Too many connections 1" );
156
161
} finally {
157
162
c .close ();
158
163
}
0 commit comments