Fixed race condition in #shouldExecuteMultipleCallsAndCallback test case

git-svn-id: https://svn.apache.org/repos/asf/httpcomponents/httpclient/trunk@1463521 13f79535-47bb-0310-9956-ffa450edef68
This commit is contained in:
Oleg Kalnichevski 2013-04-02 13:11:24 +00:00
parent 26add4a3d6
commit 2f310c3744

View File

@ -30,6 +30,7 @@
import java.net.InetSocketAddress; import java.net.InetSocketAddress;
import java.util.List; import java.util.List;
import java.util.concurrent.CancellationException; import java.util.concurrent.CancellationException;
import java.util.concurrent.CountDownLatch;
import java.util.concurrent.ExecutionException; import java.util.concurrent.ExecutionException;
import java.util.concurrent.ExecutorService; import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors; import java.util.concurrent.Executors;
@ -137,13 +138,18 @@ public void shouldExecuteMultipleCalls() throws InterruptedException, ExecutionE
@Test @Test
public void shouldExecuteMultipleCallsAndCallback() throws InterruptedException { public void shouldExecuteMultipleCallsAndCallback() throws InterruptedException {
final HttpGet[] requests= new HttpGet[100]; final int reqNo = 100;
for(int i=0;i<100;i++) { final HttpGet[] requests= new HttpGet[reqNo];
requests[i]=new HttpGet(uri); for(int i = 0; i < reqNo; i++) {
requests[i] = new HttpGet(uri);
} }
final CountingCallback callback = new CountingCallback(); final CountDownLatch latch = new CountDownLatch(reqNo);
final CountingCallback callback = new CountingCallback(latch);
httpAsyncClientWithFuture.executeMultiple(null, httpAsyncClientWithFuture.executeMultiple(null,
new OkidokiHandler(), callback , 10, TimeUnit.SECONDS, requests); new OkidokiHandler(), callback , 10, TimeUnit.SECONDS, requests);
latch.await(10, TimeUnit.SECONDS);
Assert.assertEquals(100, callback.completed); Assert.assertEquals(100, callback.completed);
Assert.assertEquals(0, callback.cancelled); Assert.assertEquals(0, callback.cancelled);
Assert.assertEquals(0, callback.failed); Assert.assertEquals(0, callback.failed);
@ -151,19 +157,30 @@ public void shouldExecuteMultipleCallsAndCallback() throws InterruptedException
private final class CountingCallback implements FutureCallback<Boolean> { private final class CountingCallback implements FutureCallback<Boolean> {
private final CountDownLatch latch;
int failed=0; int failed=0;
int cancelled=0; int cancelled=0;
int completed=0; int completed=0;
CountingCallback(final CountDownLatch latch) {
super();
this.latch = latch;
}
public void failed(final Exception ex) { public void failed(final Exception ex) {
latch.countDown();
failed++; failed++;
} }
public void completed(final Boolean result) { public void completed(final Boolean result) {
latch.countDown();
completed++; completed++;
} }
public void cancelled() { public void cancelled() {
latch.countDown();
cancelled++; cancelled++;
} }
} }