-
Notifications
You must be signed in to change notification settings - Fork 1.6k
/
Copy pathMaxTotalConnectionTest.java
112 lines (97 loc) · 3.47 KB
/
MaxTotalConnectionTest.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
/*
* Copyright 2010 Ning, Inc.
*
* This program is licensed to you under the Apache License, version 2.0
* (the "License"); you may not use this file except in compliance with the
* License. You may obtain a copy of the License at:
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
* WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
* License for the specific language governing permissions and limitations
* under the License.
*/
package org.asynchttpclient.channel;
import org.asynchttpclient.*;
import org.testng.Assert;
import org.testng.annotations.Test;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.CountDownLatch;
import java.util.concurrent.atomic.AtomicReference;
import static org.asynchttpclient.Dsl.asyncHttpClient;
import static org.asynchttpclient.Dsl.config;
import static org.testng.Assert.assertNull;
public class MaxTotalConnectionTest extends AbstractBasicTest {
@Test(groups = "online")
public void testMaxTotalConnectionsExceedingException() throws IOException {
String[] urls = new String[]{"https://google.com", "https://github.com"};
AsyncHttpClientConfig config = config()
.setConnectTimeout(1000)
.setRequestTimeout(5000)
.setKeepAlive(false)
.setMaxConnections(1)
.setMaxConnectionsPerHost(1)
.build();
try (AsyncHttpClient client = asyncHttpClient(config)) {
List<ListenableFuture<Response>> futures = new ArrayList<>();
for (String url : urls) {
futures.add(client.prepareGet(url).execute());
}
boolean caughtError = false;
int i;
for (i = 0; i < urls.length; i++) {
try {
futures.get(i).get();
} catch (Exception e) {
// assert that 2nd request fails, because
// maxTotalConnections=1
caughtError = true;
break;
}
}
Assert.assertEquals(1, i);
Assert.assertTrue(caughtError);
}
}
@Test(groups = "online")
public void testMaxTotalConnections() throws Exception {
String[] urls = new String[]{"https://www.google.com", "https://www.youtube.com"};
final CountDownLatch latch = new CountDownLatch(2);
final AtomicReference<Throwable> ex = new AtomicReference<>();
final AtomicReference<String> failedUrl = new AtomicReference<>();
AsyncHttpClientConfig config = config()
.setConnectTimeout(1000)
.setRequestTimeout(5000)
.setKeepAlive(false)
.setMaxConnections(2)
.setMaxConnectionsPerHost(1)
.build();
try (AsyncHttpClient client = asyncHttpClient(config)) {
for (String url : urls) {
final String thisUrl = url;
client.prepareGet(url).execute(new AsyncCompletionHandlerBase() {
@Override
public Response onCompleted(Response response) throws Exception {
Response r = super.onCompleted(response);
latch.countDown();
return r;
}
@Override
public void onThrowable(Throwable t) {
super.onThrowable(t);
ex.set(t);
failedUrl.set(thisUrl);
latch.countDown();
}
});
}
latch.await();
assertNull(ex.get());
assertNull(failedUrl.get());
}
}
}