|
19 | 19 | import static org.testng.Assert.assertTrue;
|
20 | 20 | import static org.testng.Assert.fail;
|
21 | 21 |
|
| 22 | +import java.io.IOException; |
| 23 | +import java.util.ArrayList; |
| 24 | +import java.util.List; |
| 25 | +import java.util.concurrent.ExecutionException; |
| 26 | +import java.util.concurrent.Future; |
| 27 | + |
22 | 28 | import org.asynchttpclient.AsyncHttpClient;
|
23 | 29 | import org.asynchttpclient.AsyncHttpClientConfig;
|
| 30 | +import org.asynchttpclient.ListenableFuture; |
24 | 31 | import org.asynchttpclient.Response;
|
25 | 32 | import org.slf4j.Logger;
|
26 | 33 | import org.slf4j.LoggerFactory;
|
| 34 | +import org.testng.Assert; |
27 | 35 | import org.testng.annotations.Test;
|
28 | 36 |
|
29 |
| -import java.io.IOException; |
30 |
| -import java.util.ArrayList; |
31 |
| -import java.util.List; |
32 |
| -import java.util.concurrent.ExecutionException; |
33 |
| -import java.util.concurrent.Future; |
34 |
| - |
35 | 37 | public abstract class MaxTotalConnectionTest extends AbstractBasicTest {
|
36 | 38 | protected final Logger log = LoggerFactory.getLogger(AbstractBasicTest.class);
|
37 | 39 |
|
38 | 40 | @Test(groups = { "standalone", "default_provider" })
|
39 |
| - public void testMaxTotalConnectionsExceedingException() { |
| 41 | + public void testMaxTotalConnectionsExceedingException() throws IOException { |
40 | 42 | String[] urls = new String[] { "http://google.com", "http://github.com/" };
|
41 | 43 |
|
42 |
| - AsyncHttpClient client = getAsyncHttpClient(new AsyncHttpClientConfig.Builder().setConnectTimeout(1000).setRequestTimeout(5000).setAllowPoolingConnections(false).setMaxConnections(1).setMaxConnectionsPerHost(1).build()); |
| 44 | + AsyncHttpClient client = getAsyncHttpClient(new AsyncHttpClientConfig.Builder().setConnectTimeout(1000) |
| 45 | + .setRequestTimeout(5000).setAllowPoolingConnections(false).setMaxConnections(1).setMaxConnectionsPerHost(1) |
| 46 | + .build()); |
| 47 | + |
43 | 48 | try {
|
44 |
| - boolean caughtError = false; |
| 49 | + List<ListenableFuture<Response>> futures = new ArrayList<>(); |
45 | 50 | for (int i = 0; i < urls.length; i++) {
|
| 51 | + futures.add(client.prepareGet(urls[i]).execute()); |
| 52 | + } |
| 53 | + |
| 54 | + boolean caughtError = false; |
| 55 | + int i; |
| 56 | + for (i = 0; i < urls.length; i++) { |
46 | 57 | try {
|
47 |
| - client.prepareGet(urls[i]).execute(); |
48 |
| - } catch (IOException e) { |
| 58 | + futures.get(i).get(); |
| 59 | + } catch (Exception e) { |
49 | 60 | // assert that 2nd request fails, because maxTotalConnections=1
|
50 |
| - assertEquals(i, 1); |
51 | 61 | caughtError = true;
|
| 62 | + break; |
52 | 63 | }
|
53 | 64 | }
|
54 |
| - assertTrue(caughtError); |
| 65 | + |
| 66 | + Assert.assertEquals(1, i); |
| 67 | + Assert.assertTrue(caughtError); |
55 | 68 | } finally {
|
56 | 69 | client.close();
|
57 | 70 | }
|
|
0 commit comments