Skip to content

Commit 5bdac75

Browse files
committed
Fix racy ListenableFutureTest, close AsyncHttpClient#1178
1 parent e0cdfc3 commit 5bdac75

File tree

1 file changed

+20
-23
lines changed

1 file changed

+20
-23
lines changed

client/src/test/java/org/asynchttpclient/ListenableFutureTest.java

Lines changed: 20 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -25,23 +25,20 @@
2525

2626
public class ListenableFutureTest extends AbstractBasicTest {
2727

28-
@Test(groups = "standalone")
28+
@Test
2929
public void testListenableFuture() throws Exception {
3030
final AtomicInteger statusCode = new AtomicInteger(500);
3131
try (AsyncHttpClient ahc = asyncHttpClient()) {
3232
final CountDownLatch latch = new CountDownLatch(1);
3333
final ListenableFuture<Response> future = ahc.prepareGet(getTargetUrl()).execute();
34-
future.addListener(new Runnable() {
35-
36-
public void run() {
37-
try {
38-
statusCode.set(future.get().getStatusCode());
39-
latch.countDown();
40-
} catch (InterruptedException e) {
41-
e.printStackTrace();
42-
} catch (ExecutionException e) {
43-
e.printStackTrace();
44-
}
34+
future.addListener(() -> {
35+
try {
36+
statusCode.set(future.get().getStatusCode());
37+
latch.countDown();
38+
} catch (InterruptedException e) {
39+
e.printStackTrace();
40+
} catch (ExecutionException e) {
41+
e.printStackTrace();
4542
}
4643
}, Executors.newFixedThreadPool(1));
4744

@@ -50,32 +47,32 @@ public void run() {
5047
}
5148
}
5249

53-
@Test(groups = "standalone")
50+
@Test
5451
public void testListenableFutureAfterCompletion() throws Exception {
5552

56-
AtomicInteger counter = new AtomicInteger(1);
53+
final CountDownLatch latch = new CountDownLatch(1);
5754

5855
try (AsyncHttpClient ahc = asyncHttpClient()) {
5956
final ListenableFuture<Response> future = ahc.prepareGet(getTargetUrl()).execute();
6057
future.get();
61-
future.addListener(() -> counter.decrementAndGet(), Runnable::run);
58+
future.addListener(() -> latch.countDown(), Runnable::run);
6259
}
63-
assertEquals(counter.get(), 0);
60+
61+
latch.await(10, TimeUnit.SECONDS);
6462
}
6563

66-
@Test(groups = "standalone")
64+
@Test
6765
public void testListenableFutureBeforeAndAfterCompletion() throws Exception {
6866

69-
AtomicInteger counter = new AtomicInteger(2);
67+
final CountDownLatch latch = new CountDownLatch(2);
7068

7169
try (AsyncHttpClient ahc = asyncHttpClient()) {
7270
final ListenableFuture<Response> future = ahc.prepareGet(getTargetUrl()).execute();
73-
74-
future.addListener(() -> counter.decrementAndGet(), Runnable::run);
75-
71+
future.addListener(() -> latch.countDown(), Runnable::run);
7672
future.get();
77-
future.addListener(() -> counter.decrementAndGet(), Runnable::run);
73+
future.addListener(() -> latch.countDown(), Runnable::run);
7874
}
79-
assertEquals(counter.get(), 0);
75+
76+
latch.await(10, TimeUnit.SECONDS);
8077
}
8178
}

0 commit comments

Comments
 (0)