Skip to content

Commit 87d2666

Browse files
author
Stephane Landelle
committed
Port fix for AsyncHttpClient#239 to master
1 parent ffc8457 commit 87d2666

File tree

159 files changed

+5088
-5289
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

159 files changed

+5088
-5289
lines changed

api/src/test/java/com/ning/http/client/async/AsyncProvidersBasicTest.java

Lines changed: 1194 additions & 1088 deletions
Large diffs are not rendered by default.

api/src/test/java/com/ning/http/client/async/AsyncStreamHandlerTest.java

Lines changed: 385 additions & 364 deletions
Large diffs are not rendered by default.

api/src/test/java/com/ning/http/client/async/AsyncStreamLifecycleTest.java

Lines changed: 50 additions & 49 deletions
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,7 @@
4747

4848
/**
4949
* Tests default asynchronous life cycle.
50-
*
50+
*
5151
* @author Hubert Iwaniuk
5252
*/
5353
public abstract class AsyncStreamLifecycleTest extends AbstractBasicTest {
@@ -63,8 +63,7 @@ public void tearDownGlobal() throws Exception {
6363
@Override
6464
public AbstractHandler configureHandler() throws Exception {
6565
return new AbstractHandler() {
66-
public void handle(String s, Request request, HttpServletRequest req, final HttpServletResponse resp)
67-
throws IOException, ServletException {
66+
public void handle(String s, Request request, HttpServletRequest req, final HttpServletResponse resp) throws IOException, ServletException {
6867
resp.setContentType("text/plain;charset=utf-8");
6968
resp.setStatus(200);
7069
final Continuation continuation = ContinuationSupport.getContinuation(req);
@@ -100,62 +99,64 @@ public void run() {
10099
};
101100
}
102101

103-
//TODO Netty only.
102+
// TODO Netty only.
104103

105-
@Test(groups = {"standalone", "default_provider"})
104+
@Test(groups = { "standalone", "default_provider" })
106105
public void testStream() throws IOException {
107106
AsyncHttpClient ahc = getAsyncHttpClient(null);
108-
final AtomicBoolean err = new AtomicBoolean(false);
109-
final LinkedBlockingQueue<String> queue = new LinkedBlockingQueue<String>();
110-
final AtomicBoolean status = new AtomicBoolean(false);
111-
final AtomicInteger headers = new AtomicInteger(0);
112-
final CountDownLatch latch = new CountDownLatch(1);
113-
ahc.executeRequest(ahc.prepareGet(getTargetUrl()).build(), new AsyncHandler<Object>() {
114-
public void onThrowable(Throwable t) {
115-
fail("Got throwable.", t);
116-
err.set(true);
117-
}
107+
try {
108+
final AtomicBoolean err = new AtomicBoolean(false);
109+
final LinkedBlockingQueue<String> queue = new LinkedBlockingQueue<String>();
110+
final AtomicBoolean status = new AtomicBoolean(false);
111+
final AtomicInteger headers = new AtomicInteger(0);
112+
final CountDownLatch latch = new CountDownLatch(1);
113+
ahc.executeRequest(ahc.prepareGet(getTargetUrl()).build(), new AsyncHandler<Object>() {
114+
public void onThrowable(Throwable t) {
115+
fail("Got throwable.", t);
116+
err.set(true);
117+
}
118118

119-
public STATE onBodyPartReceived(HttpResponseBodyPart e) throws Exception {
120-
String s = new String(e.getBodyPartBytes());
121-
log.info("got part: {}", s);
122-
if (s.equals("")) {
123-
//noinspection ThrowableInstanceNeverThrown
124-
log.warn("Sampling stacktrace.",
125-
new Throwable("trace that, we should not get called for empty body."));
119+
public STATE onBodyPartReceived(HttpResponseBodyPart e) throws Exception {
120+
String s = new String(e.getBodyPartBytes());
121+
log.info("got part: {}", s);
122+
if (s.equals("")) {
123+
// noinspection ThrowableInstanceNeverThrown
124+
log.warn("Sampling stacktrace.", new Throwable("trace that, we should not get called for empty body."));
125+
}
126+
queue.put(s);
127+
return STATE.CONTINUE;
126128
}
127-
queue.put(s);
128-
return STATE.CONTINUE;
129-
}
130129

131-
public STATE onStatusReceived(HttpResponseStatus e) throws Exception {
132-
status.set(true);
133-
return STATE.CONTINUE;
134-
}
130+
public STATE onStatusReceived(HttpResponseStatus e) throws Exception {
131+
status.set(true);
132+
return STATE.CONTINUE;
133+
}
135134

136-
public STATE onHeadersReceived(HttpResponseHeaders e) throws Exception {
137-
if (headers.incrementAndGet() == 2) {
138-
throw new Exception("Analyze this.");
135+
public STATE onHeadersReceived(HttpResponseHeaders e) throws Exception {
136+
if (headers.incrementAndGet() == 2) {
137+
throw new Exception("Analyze this.");
138+
}
139+
return STATE.CONTINUE;
139140
}
140-
return STATE.CONTINUE;
141-
}
142141

143-
public Object onCompleted() throws Exception {
144-
latch.countDown();
145-
return null;
142+
public Object onCompleted() throws Exception {
143+
latch.countDown();
144+
return null;
145+
}
146+
});
147+
try {
148+
assertTrue(latch.await(1, TimeUnit.SECONDS), "Latch failed.");
149+
} catch (InterruptedException e) {
150+
fail("Interrupted.", e);
146151
}
147-
});
148-
try {
149-
assertTrue(latch.await(1, TimeUnit.SECONDS), "Latch failed.");
150-
} catch (InterruptedException e) {
151-
fail("Interrupted.", e);
152+
assertFalse(err.get());
153+
assertEquals(queue.size(), 2);
154+
assertTrue(queue.contains("part1"));
155+
assertTrue(queue.contains("part2"));
156+
assertTrue(status.get());
157+
assertEquals(headers.get(), 1);
158+
} finally {
159+
ahc.close();
152160
}
153-
assertFalse(err.get());
154-
assertEquals(queue.size(), 2);
155-
assertTrue(queue.contains("part1"));
156-
assertTrue(queue.contains("part2"));
157-
assertTrue(status.get());
158-
assertEquals(headers.get(), 1);
159-
ahc.close();
160161
}
161162
}

api/src/test/java/com/ning/http/client/async/AuthTimeoutTest.java

Lines changed: 59 additions & 79 deletions
Original file line numberDiff line numberDiff line change
@@ -49,17 +49,15 @@
4949
import static org.testng.Assert.assertNotNull;
5050
import static org.testng.Assert.fail;
5151

52-
public abstract class AuthTimeoutTest
53-
extends AbstractBasicTest {
52+
public abstract class AuthTimeoutTest extends AbstractBasicTest {
5453

5554
private final static String user = "user";
5655

5756
private final static String admin = "admin";
5857

5958
protected AsyncHttpClient client;
6059

61-
public void setUpServer(String auth)
62-
throws Exception {
60+
public void setUpServer(String auth) throws Exception {
6361
server = new Server();
6462
Logger root = Logger.getRootLogger();
6563
root.setLevel(Level.DEBUG);
@@ -78,7 +76,7 @@ public void setUpServer(String auth)
7876

7977
Constraint constraint = new Constraint();
8078
constraint.setName(auth);
81-
constraint.setRoles(new String[]{user, admin});
79+
constraint.setRoles(new String[] { user, admin });
8280
constraint.setAuthenticate(true);
8381

8482
ConstraintMapping mapping = new ConstraintMapping();
@@ -105,10 +103,8 @@ public void setUpServer(String auth)
105103
log.info("Local HTTP server started successfully");
106104
}
107105

108-
private class SimpleHandler
109-
extends AbstractHandler {
110-
public void handle(String s, Request r, HttpServletRequest request, HttpServletResponse response)
111-
throws IOException, ServletException {
106+
private class SimpleHandler extends AbstractHandler {
107+
public void handle(String s, Request r, HttpServletRequest request, HttpServletResponse response) throws IOException, ServletException {
112108

113109
// NOTE: handler sends less bytes than are given in Content-Length, which should lead to timeout
114110

@@ -128,150 +124,135 @@ public void handle(String s, Request r, HttpServletRequest request, HttpServletR
128124
}
129125
}
130126

131-
@Test(groups = {"standalone", "default_provider"}, enabled = false)
132-
public void basicAuthTimeoutTest()
133-
throws Exception {
127+
@Test(groups = { "standalone", "default_provider" }, enabled = false)
128+
public void basicAuthTimeoutTest() throws Exception {
134129
setUpServer(Constraint.__BASIC_AUTH);
135-
136-
Future<Response> f = execute(false);
137130
try {
131+
Future<Response> f = execute(false);
138132
f.get();
139133
fail("expected timeout");
140-
}
141-
catch (Exception e) {
134+
} catch (Exception e) {
142135
inspectException(e);
136+
} finally {
137+
client.close();
143138
}
144-
client.close();
145139
}
146140

147-
@Test(groups = {"standalone", "default_provider"}, enabled = false)
148-
public void basicPreemptiveAuthTimeoutTest()
149-
throws Exception {
141+
@Test(groups = { "standalone", "default_provider" }, enabled = false)
142+
public void basicPreemptiveAuthTimeoutTest() throws Exception {
150143
setUpServer(Constraint.__BASIC_AUTH);
151-
152-
Future<Response> f = execute(true);
153144
try {
145+
Future<Response> f = execute(true);
154146
f.get();
155147
fail("expected timeout");
156-
}
157-
catch (Exception e) {
148+
} catch (Exception e) {
158149
inspectException(e);
150+
} finally {
151+
client.close();
159152
}
160-
client.close();
161153
}
162154

163-
@Test(groups = {"standalone", "default_provider"}, enabled = false)
164-
public void digestAuthTimeoutTest()
165-
throws Exception {
155+
@Test(groups = { "standalone", "default_provider" }, enabled = false)
156+
public void digestAuthTimeoutTest() throws Exception {
166157
setUpServer(Constraint.__DIGEST_AUTH);
167158

168-
Future<Response> f = execute(false);
169159
try {
160+
Future<Response> f = execute(false);
170161
f.get();
171162
fail("expected timeout");
172-
}
173-
catch (Exception e) {
163+
} catch (Exception e) {
174164
inspectException(e);
165+
} finally {
166+
client.close();
175167
}
176-
client.close();
177168
}
178169

179-
@Test(groups = {"standalone", "default_provider"}, enabled = false)
180-
public void digestPreemptiveAuthTimeoutTest()
181-
throws Exception {
170+
@Test(groups = { "standalone", "default_provider" }, enabled = false)
171+
public void digestPreemptiveAuthTimeoutTest() throws Exception {
182172
setUpServer(Constraint.__DIGEST_AUTH);
183173

184-
Future<Response> f = execute(true);
185174
try {
175+
Future<Response> f = execute(true);
186176
f.get();
187177
fail("expected timeout");
188-
}
189-
catch (Exception e) {
178+
} catch (Exception e) {
190179
inspectException(e);
180+
} finally {
181+
client.close();
191182
}
192-
client.close();
193183
}
194184

195-
@Test(groups = {"standalone", "default_provider"}, enabled = false)
196-
public void basicFutureAuthTimeoutTest()
197-
throws Exception {
185+
@Test(groups = { "standalone", "default_provider" }, enabled = false)
186+
public void basicFutureAuthTimeoutTest() throws Exception {
198187
setUpServer(Constraint.__BASIC_AUTH);
199188

200-
Future<Response> f = execute(false);
201189
try {
190+
Future<Response> f = execute(false);
202191
f.get(1, TimeUnit.SECONDS);
203192
fail("expected timeout");
204-
}
205-
catch (Exception e) {
193+
} catch (Exception e) {
206194
inspectException(e);
195+
} finally {
196+
client.close();
207197
}
208-
client.close();
209198
}
210199

211-
@Test(groups = {"standalone", "default_provider"}, enabled = false)
212-
public void basicFuturePreemptiveAuthTimeoutTest()
213-
throws Exception {
200+
@Test(groups = { "standalone", "default_provider" }, enabled = false)
201+
public void basicFuturePreemptiveAuthTimeoutTest() throws Exception {
214202
setUpServer(Constraint.__BASIC_AUTH);
215203

216-
Future<Response> f = execute(true);
217204
try {
205+
Future<Response> f = execute(true);
218206
f.get(1, TimeUnit.SECONDS);
219207
fail("expected timeout");
220-
}
221-
catch (Exception e) {
208+
} catch (Exception e) {
222209
inspectException(e);
210+
} finally {
211+
client.close();
223212
}
224-
client.close();
225213
}
226214

227-
@Test(groups = {"standalone", "default_provider"}, enabled = false)
228-
public void digestFutureAuthTimeoutTest()
229-
throws Exception {
215+
@Test(groups = { "standalone", "default_provider" }, enabled = false)
216+
public void digestFutureAuthTimeoutTest() throws Exception {
230217
setUpServer(Constraint.__DIGEST_AUTH);
231218

232-
Future<Response> f = execute(false);
233219
try {
220+
Future<Response> f = execute(false);
234221
f.get(1, TimeUnit.SECONDS);
235222
fail("expected timeout");
236-
}
237-
catch (Exception e) {
223+
} catch (Exception e) {
238224
inspectException(e);
225+
} finally {
226+
client.close();
239227
}
240-
client.close();
241228
}
242229

243-
@Test(groups = {"standalone", "default_provider"}, enabled = false)
244-
public void digestFuturePreemptiveAuthTimeoutTest()
245-
throws Exception {
230+
@Test(groups = { "standalone", "default_provider" }, enabled = false)
231+
public void digestFuturePreemptiveAuthTimeoutTest() throws Exception {
246232
setUpServer(Constraint.__DIGEST_AUTH);
247233

248-
Future<Response> f = execute(true);
249234
try {
235+
Future<Response> f = execute(true);
250236
f.get(1, TimeUnit.SECONDS);
251237
fail("expected timeout");
252-
}
253-
catch (Exception e) {
238+
} catch (Exception e) {
254239
inspectException(e);
240+
} finally {
241+
client.close();
255242
}
256-
client.close();
257243
}
258244

259245
protected void inspectException(Throwable t) {
260246
assertNotNull(t.getCause());
261247
assertEquals(t.getCause().getClass(), IOException.class);
262-
if (!t.getCause().getMessage().startsWith("Remotely Closed")){
248+
if (!t.getCause().getMessage().startsWith("Remotely Closed")) {
263249
fail();
264-
};
250+
}
265251
}
266252

267-
protected Future<Response> execute(boolean preemptive)
268-
throws IOException {
269-
client =
270-
getAsyncHttpClient(
271-
new AsyncHttpClientConfig.Builder().setIdleConnectionInPoolTimeoutInMs(2000).setConnectionTimeoutInMs(20000).setRequestTimeoutInMs(2000).build());
272-
AsyncHttpClient.BoundRequestBuilder r =
273-
client.prepareGet(getTargetUrl()).setRealm(realm(preemptive)).setHeader("X-Content",
274-
"Test");
253+
protected Future<Response> execute(boolean preemptive) throws IOException {
254+
client = getAsyncHttpClient(new AsyncHttpClientConfig.Builder().setIdleConnectionInPoolTimeoutInMs(2000).setConnectionTimeoutInMs(20000).setRequestTimeoutInMs(2000).build());
255+
AsyncHttpClient.BoundRequestBuilder r = client.prepareGet(getTargetUrl()).setRealm(realm(preemptive)).setHeader("X-Content", "Test");
275256
Future<Response> f = r.execute();
276257
return f;
277258
}
@@ -286,8 +267,7 @@ protected String getTargetUrl() {
286267
}
287268

288269
@Override
289-
public AbstractHandler configureHandler()
290-
throws Exception {
270+
public AbstractHandler configureHandler() throws Exception {
291271
return new SimpleHandler();
292272
}
293273
}

0 commit comments

Comments
 (0)