Skip to content

Commit b4eb9d5

Browse files
author
Stephane Landelle
committed
Ensure clients are finally closed, fix AsyncHttpClient#239
1 parent d2eed46 commit b4eb9d5

File tree

110 files changed

+5016
-5268
lines changed

Some content is hidden

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

110 files changed

+5016
-5268
lines changed

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

Lines changed: 1253 additions & 1160 deletions
Large diffs are not rendered by default.

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

Lines changed: 431 additions & 404 deletions
Large diffs are not rendered by default.

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

Lines changed: 51 additions & 50 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 {
107-
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-
}
106+
AsyncHttpClient client = getAsyncHttpClient(null);
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+
client.executeRequest(client.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.isEmpty()) {
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.isEmpty()) {
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+
client.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
}

0 commit comments

Comments
 (0)