hc-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From ol...@apache.org
Subject svn commit: r1491891 - /httpcomponents/httpclient/trunk/httpclient/src/test/java/org/apache/http/impl/client/TestFutureRequestExecutionService.java
Date Tue, 11 Jun 2013 18:14:10 GMT
Author: olegk
Date: Tue Jun 11 18:14:10 2013
New Revision: 1491891

URL: http://svn.apache.org/r1491891
Log:
Fixed race condition in TestFutureRequestExecutionService#shouldExecuteMultipleCallsAndCallback

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=1491891&r1=1491890&r2=1491891&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 Jun 11 18:14:10 2013
@@ -39,7 +39,6 @@ import java.util.concurrent.Future;
 import java.util.concurrent.TimeUnit;
 import java.util.concurrent.TimeoutException;
 import java.util.concurrent.atomic.AtomicBoolean;
-import java.util.concurrent.atomic.AtomicInteger;
 
 import junit.framework.Assert;
 
@@ -126,7 +125,7 @@ public class TestFutureRequestExecutionS
     }
 
     @Test
-    public void shouldExecuteMultipleCalls() throws InterruptedException, ExecutionException
{
+    public void shouldExecuteMultipleCalls() throws Exception {
         final int reqNo = 100;
         final Queue<Future<Boolean>> tasks = new LinkedList<Future<Boolean>>();
         for(int i = 0; i < reqNo; i++) {
@@ -142,31 +141,28 @@ public class TestFutureRequestExecutionS
     }
 
     @Test
-    public void shouldExecuteMultipleCallsAndCallback() throws InterruptedException {
+    public void shouldExecuteMultipleCallsAndCallback() throws Exception {
         final int reqNo = 100;
+        final Queue<Future<Boolean>> tasks = new LinkedList<Future<Boolean>>();
         final CountDownLatch latch = new CountDownLatch(reqNo);
-        final CountingCallback callback = new CountingCallback(latch);
         for(int i = 0; i < reqNo; i++) {
-            httpAsyncClientWithFuture.execute(
+            final Future<Boolean> task = httpAsyncClientWithFuture.execute(
                     new HttpGet(uri), HttpClientContext.create(),
-                    new OkidokiHandler(), callback);
+                    new OkidokiHandler(), new CountingCallback(latch));
+            tasks.add(task);
+        }
+        Assert.assertTrue(latch.await(5, TimeUnit.SECONDS));
+        for (final Future<Boolean> task : tasks) {
+            final Boolean b = task.get();
+            Assert.assertNotNull(b);
+            Assert.assertTrue("request should have returned OK", b.booleanValue());
         }
-        latch.await(10, TimeUnit.SECONDS);
-
-        Assert.assertEquals(100, callback.completed.get());
-        Assert.assertEquals(0, callback.cancelled.get());
-        Assert.assertEquals(0, callback.failed.get());
     }
 
-
     private final class CountingCallback implements FutureCallback<Boolean> {
 
         private final CountDownLatch latch;
 
-        AtomicInteger failed = new AtomicInteger(0);
-        AtomicInteger cancelled = new AtomicInteger(0);
-        AtomicInteger completed = new AtomicInteger(0);
-
         CountingCallback(final CountDownLatch latch) {
             super();
             this.latch = latch;
@@ -174,17 +170,14 @@ public class TestFutureRequestExecutionS
 
         public void failed(final Exception ex) {
             latch.countDown();
-            failed.incrementAndGet();
         }
 
         public void completed(final Boolean result) {
             latch.countDown();
-            completed.incrementAndGet();
         }
 
         public void cancelled() {
             latch.countDown();
-            cancelled.incrementAndGet();
         }
     }
 



Mime
View raw message