hc-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From ol...@apache.org
Subject svn commit: r1463521 - /httpcomponents/httpclient/trunk/httpclient/src/test/java/org/apache/http/impl/client/TestFutureRequestExecutionService.java
Date Tue, 02 Apr 2013 13:11:24 GMT
Author: olegk
Date: Tue Apr  2 13:11:24 2013
New Revision: 1463521

URL: http://svn.apache.org/r1463521
Log:
Fixed race condition in #shouldExecuteMultipleCallsAndCallback test case

Modified:
    httpcomponents/httpclient/trunk/httpclient/src/test/java/org/apache/http/impl/client/TestFutureRequestExecutionService.java

Modified: httpcomponents/httpclient/trunk/httpclient/src/test/java/org/apache/http/impl/client/TestFutureRequestExecutionService.java
URL: http://svn.apache.org/viewvc/httpcomponents/httpclient/trunk/httpclient/src/test/java/org/apache/http/impl/client/TestFutureRequestExecutionService.java?rev=1463521&r1=1463520&r2=1463521&view=diff
==============================================================================
--- httpcomponents/httpclient/trunk/httpclient/src/test/java/org/apache/http/impl/client/TestFutureRequestExecutionService.java
(original)
+++ httpcomponents/httpclient/trunk/httpclient/src/test/java/org/apache/http/impl/client/TestFutureRequestExecutionService.java
Tue Apr  2 13:11:24 2013
@@ -30,6 +30,7 @@ import java.io.IOException;
 import java.net.InetSocketAddress;
 import java.util.List;
 import java.util.concurrent.CancellationException;
+import java.util.concurrent.CountDownLatch;
 import java.util.concurrent.ExecutionException;
 import java.util.concurrent.ExecutorService;
 import java.util.concurrent.Executors;
@@ -137,13 +138,18 @@ public class TestFutureRequestExecutionS
 
     @Test
     public void shouldExecuteMultipleCallsAndCallback() throws InterruptedException {
-        final HttpGet[] requests= new HttpGet[100];
-        for(int i=0;i<100;i++) {
-            requests[i]=new HttpGet(uri);
+        final int reqNo = 100;
+        final HttpGet[] requests= new HttpGet[reqNo];
+        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,
             new OkidokiHandler(), callback , 10, TimeUnit.SECONDS, requests);
+
+        latch.await(10, TimeUnit.SECONDS);
+
         Assert.assertEquals(100, callback.completed);
         Assert.assertEquals(0, callback.cancelled);
         Assert.assertEquals(0, callback.failed);
@@ -151,19 +157,30 @@ public class TestFutureRequestExecutionS
 
 
     private final class CountingCallback implements FutureCallback<Boolean> {
+
+        private final CountDownLatch latch;
+
         int failed=0;
         int cancelled=0;
         int completed=0;
 
+        CountingCallback(final CountDownLatch latch) {
+            super();
+            this.latch = latch;
+        }
+
         public void failed(final Exception ex) {
+            latch.countDown();
             failed++;
         }
 
         public void completed(final Boolean result) {
+            latch.countDown();
             completed++;
         }
 
         public void cancelled() {
+            latch.countDown();
             cancelled++;
         }
     }



Mime
View raw message