-
Notifications
You must be signed in to change notification settings - Fork 1.6k
/
MaxConnectionsInThreads.java
186 lines (154 loc) · 6.97 KB
/
MaxConnectionsInThreads.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
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
/*
* Copyright 2010 Ning, Inc.
*
* Ning licenses this file 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 static org.asynchttpclient.Dsl.*;
import static org.asynchttpclient.test.TestUtils.*;
import static org.testng.Assert.assertEquals;
import java.io.IOException;
import java.io.OutputStream;
import java.net.URI;
import java.net.URISyntaxException;
import java.util.concurrent.CountDownLatch;
import java.util.concurrent.atomic.AtomicInteger;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.asynchttpclient.AbstractBasicTest;
import org.asynchttpclient.AsyncCompletionHandlerBase;
import org.asynchttpclient.AsyncHttpClient;
import org.asynchttpclient.AsyncHttpClientConfig;
import org.asynchttpclient.Response;
import org.eclipse.jetty.servlet.ServletContextHandler;
import org.eclipse.jetty.servlet.ServletHolder;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.testng.annotations.BeforeClass;
import org.testng.annotations.Test;
public class MaxConnectionsInThreads extends AbstractBasicTest {
// FIXME weird
private static URI servletEndpointUri;
@Test(groups = { "online", "default_provider" })
public void testMaxConnectionsWithinThreads() throws Exception {
String[] urls = new String[] { servletEndpointUri.toString(), servletEndpointUri.toString() };
AsyncHttpClientConfig config = config().setConnectTimeout(1000).setRequestTimeout(5000).setKeepAlive(true)//
.setMaxConnections(1).setMaxConnectionsPerHost(1).build();
final CountDownLatch inThreadsLatch = new CountDownLatch(2);
final AtomicInteger failedCount = new AtomicInteger();
try (AsyncHttpClient client = asyncHttpClient(config)) {
for (int i = 0; i < urls.length; i++) {
final String url = urls[i];
Thread t = new Thread() {
public void run() {
client.prepareGet(url).execute(new AsyncCompletionHandlerBase() {
@Override
public Response onCompleted(Response response) throws Exception {
Response r = super.onCompleted(response);
inThreadsLatch.countDown();
return r;
}
@Override
public void onThrowable(Throwable t) {
super.onThrowable(t);
failedCount.incrementAndGet();
inThreadsLatch.countDown();
}
});
}
};
t.start();
}
inThreadsLatch.await();
assertEquals(failedCount.get(), 1, "Max Connections should have been reached when launching from concurrent threads");
final CountDownLatch notInThreadsLatch = new CountDownLatch(2);
failedCount.set(0);
for (int i = 0; i < urls.length; i++) {
final String url = urls[i];
client.prepareGet(url).execute(new AsyncCompletionHandlerBase() {
@Override
public Response onCompleted(Response response) throws Exception {
Response r = super.onCompleted(response);
notInThreadsLatch.countDown();
return r;
}
@Override
public void onThrowable(Throwable t) {
super.onThrowable(t);
failedCount.incrementAndGet();
notInThreadsLatch.countDown();
}
});
}
notInThreadsLatch.await();
assertEquals(failedCount.get(), 1, "Max Connections should have been reached when launching from main thread");
}
}
@Override
@BeforeClass
public void setUpGlobal() throws Exception {
port1 = findFreePort();
server = newJettyHttpServer(port1);
ServletContextHandler context = new ServletContextHandler(ServletContextHandler.SESSIONS);
context.setContextPath("/");
server.setHandler(context);
context.addServlet(new ServletHolder(new MockTimeoutHttpServlet()), "/timeout/*");
server.start();
String endpoint = "http://127.0.0.1:" + port1 + "/timeout/";
servletEndpointUri = new URI(endpoint);
}
public String getTargetUrl() {
String s = "http://127.0.0.1:" + port1 + "/timeout/";
try {
servletEndpointUri = new URI(s);
} catch (URISyntaxException e) {
e.printStackTrace();
}
return s;
}
@SuppressWarnings("serial")
public static class MockTimeoutHttpServlet extends HttpServlet {
private static final Logger LOGGER = LoggerFactory.getLogger(MockTimeoutHttpServlet.class);
private static final String contentType = "text/plain";
public static long DEFAULT_TIMEOUT = 2000;
public void service(HttpServletRequest req, HttpServletResponse res) throws ServletException, IOException {
res.setStatus(200);
res.addHeader("Content-Type", contentType);
long sleepTime = DEFAULT_TIMEOUT;
try {
sleepTime = Integer.parseInt(req.getParameter("timeout"));
} catch (NumberFormatException e) {
sleepTime = DEFAULT_TIMEOUT;
}
try {
LOGGER.debug("=======================================");
LOGGER.debug("Servlet is sleeping for: " + sleepTime);
LOGGER.debug("=======================================");
Thread.sleep(sleepTime);
LOGGER.debug("=======================================");
LOGGER.debug("Servlet is awake for");
LOGGER.debug("=======================================");
} catch (Exception e) {
}
res.setHeader("XXX", "TripleX");
byte[] retVal = "1".getBytes();
OutputStream os = res.getOutputStream();
res.setContentLength(retVal.length);
os.write(retVal);
os.close();
}
}
}