Skip to content

Commit 8c63656

Browse files
committed
Improve the test and support
1 parent 967f684 commit 8c63656

File tree

3 files changed

+22
-12
lines changed

3 files changed

+22
-12
lines changed

src/main/java/com/ning/http/client/providers/apache/ApacheResponseFuture.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,7 @@ public void done(Callable callable) {
6363
if (reaperFuture != null) {
6464
reaperFuture.cancel(true);
6565
}
66+
super.done();
6667
}
6768

6869
/**

src/main/java/com/ning/http/client/providers/netty/NettyResponseFuture.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -239,7 +239,6 @@ V getContent() throws ExecutionException {
239239
}
240240

241241
public final void done(Callable callable) {
242-
super.done();
243242
try {
244243
if (exEx.get() != null) {
245244
return;
@@ -261,6 +260,7 @@ public final void done(Callable callable) {
261260
} finally {
262261
latch.countDown();
263262
}
263+
super.done();
264264
}
265265

266266
public final void abort(final Throwable t) {

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

Lines changed: 20 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -20,12 +20,14 @@
2020
import com.ning.http.client.Response;
2121
import org.testng.annotations.Test;
2222

23+
import java.util.concurrent.CountDownLatch;
24+
import java.util.concurrent.ExecutionException;
2325
import java.util.concurrent.Executors;
24-
import java.util.concurrent.atomic.AtomicBoolean;
26+
import java.util.concurrent.Future;
27+
import java.util.concurrent.TimeUnit;
28+
import java.util.concurrent.atomic.AtomicInteger;
2529

2630
import static org.testng.Assert.assertEquals;
27-
import static org.testng.Assert.assertNotNull;
28-
import static org.testng.Assert.assertTrue;
2931

3032
/**
3133
* Tests case where response doesn't have body.
@@ -36,19 +38,26 @@ public abstract class ListenableFutureTest extends AbstractBasicTest {
3638

3739
@Test(groups = {"standalone", "default_provider"})
3840
public void testListenableFuture() throws Throwable {
39-
final AtomicBoolean executed = new AtomicBoolean(false);
41+
final AtomicInteger statusCode = new AtomicInteger(500);
4042
AsyncHttpClient ahc = getAsyncHttpClient(null);
41-
Response response = ((ListenableFuture<Response>)ahc.prepareGet(getTargetUrl()).execute()).addListener(new Runnable(){
42-
43+
final CountDownLatch latch = new CountDownLatch(1);
44+
final Future<Response> future = ((ListenableFuture<Response>)ahc.prepareGet(getTargetUrl()).execute());
45+
((ListenableFuture)future).addListener(new Runnable(){
4346

4447
public void run() {
45-
executed.set(true);
48+
try {
49+
statusCode.set(future.get().getStatusCode());
50+
latch.countDown();
51+
} catch (InterruptedException e) {
52+
e.printStackTrace();
53+
} catch (ExecutionException e) {
54+
e.printStackTrace();
55+
}
4656
}
47-
}, Executors.newFixedThreadPool(1)).get();
57+
}, Executors.newFixedThreadPool(1));
4858

49-
assertNotNull(response);
50-
assertEquals(response.getStatusCode(), 200);
51-
assertTrue(executed.get());
59+
latch.await(10, TimeUnit.SECONDS);
60+
assertEquals(statusCode.get(), 200);
5261
ahc.close();
5362
}
5463
}

0 commit comments

Comments
 (0)