You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@hc.apache.org by ol...@apache.org on 2017/04/24 10:04:28 UTC

svn commit: r1792447 - in /httpcomponents/httpcore/branches/4.4.x: httpcore-nio/src/test/java/org/apache/http/nio/pool/ httpcore/src/main/java/org/apache/http/concurrent/ httpcore/src/test/java/org/apache/http/concurrent/

Author: olegk
Date: Mon Apr 24 10:04:28 2017
New Revision: 1792447

URL: http://svn.apache.org/viewvc?rev=1792447&view=rev
Log:
HTTPCORE-456: BasicFuture fails to honor Future interface contract by not throwing CancellationException when cancelled

Modified:
    httpcomponents/httpcore/branches/4.4.x/httpcore-nio/src/test/java/org/apache/http/nio/pool/TestNIOConnPool.java
    httpcomponents/httpcore/branches/4.4.x/httpcore/src/main/java/org/apache/http/concurrent/BasicFuture.java
    httpcomponents/httpcore/branches/4.4.x/httpcore/src/test/java/org/apache/http/concurrent/TestBasicFuture.java

Modified: httpcomponents/httpcore/branches/4.4.x/httpcore-nio/src/test/java/org/apache/http/nio/pool/TestNIOConnPool.java
URL: http://svn.apache.org/viewvc/httpcomponents/httpcore/branches/4.4.x/httpcore-nio/src/test/java/org/apache/http/nio/pool/TestNIOConnPool.java?rev=1792447&r1=1792446&r2=1792447&view=diff
==============================================================================
--- httpcomponents/httpcore/branches/4.4.x/httpcore-nio/src/test/java/org/apache/http/nio/pool/TestNIOConnPool.java (original)
+++ httpcomponents/httpcore/branches/4.4.x/httpcore-nio/src/test/java/org/apache/http/nio/pool/TestNIOConnPool.java Mon Apr 24 10:04:28 2017
@@ -32,6 +32,7 @@ import java.net.InetSocketAddress;
 import java.net.SocketAddress;
 import java.net.UnknownHostException;
 import java.util.Collections;
+import java.util.concurrent.CancellationException;
 import java.util.concurrent.ExecutionException;
 import java.util.concurrent.Future;
 import java.util.concurrent.TimeUnit;
@@ -259,8 +260,11 @@ public class TestNIOConnPool {
 
         Assert.assertTrue(future.isDone());
         Assert.assertTrue(future.isCancelled());
-        final LocalPoolEntry entry = future.get();
-        Assert.assertNull(entry);
+        try {
+            future.get();
+            Assert.fail("CancellationException expected");
+        } catch (CancellationException ignore) {
+        }
 
         totals = pool.getTotalStats();
         Assert.assertEquals(0, totals.getAvailable());
@@ -1046,8 +1050,11 @@ public class TestNIOConnPool {
         pool.requestCompleted(sessionRequest1);
 
         Assert.assertTrue(future1.isDone());
-        final LocalPoolEntry entry1 = future1.get();
-        Assert.assertNull(entry1);
+        try {
+            future1.get();
+            Assert.fail("CancellationException expected");
+        } catch (CancellationException ignore) {
+        }
 
         final PoolStats totals = pool.getTotalStats();
         Assert.assertEquals(1, totals.getAvailable());

Modified: httpcomponents/httpcore/branches/4.4.x/httpcore/src/main/java/org/apache/http/concurrent/BasicFuture.java
URL: http://svn.apache.org/viewvc/httpcomponents/httpcore/branches/4.4.x/httpcore/src/main/java/org/apache/http/concurrent/BasicFuture.java?rev=1792447&r1=1792446&r2=1792447&view=diff
==============================================================================
--- httpcomponents/httpcore/branches/4.4.x/httpcore/src/main/java/org/apache/http/concurrent/BasicFuture.java (original)
+++ httpcomponents/httpcore/branches/4.4.x/httpcore/src/main/java/org/apache/http/concurrent/BasicFuture.java Mon Apr 24 10:04:28 2017
@@ -28,6 +28,7 @@ package org.apache.http.concurrent;
 
 import org.apache.http.util.Args;
 
+import java.util.concurrent.CancellationException;
 import java.util.concurrent.ExecutionException;
 import java.util.concurrent.Future;
 import java.util.concurrent.TimeUnit;
@@ -69,6 +70,9 @@ public class BasicFuture<T> implements F
         if (this.ex != null) {
             throw new ExecutionException(this.ex);
         }
+        if (cancelled) {
+            throw new CancellationException();
+        }
         return this.result;
     }
 

Modified: httpcomponents/httpcore/branches/4.4.x/httpcore/src/test/java/org/apache/http/concurrent/TestBasicFuture.java
URL: http://svn.apache.org/viewvc/httpcomponents/httpcore/branches/4.4.x/httpcore/src/test/java/org/apache/http/concurrent/TestBasicFuture.java?rev=1792447&r1=1792446&r2=1792447&view=diff
==============================================================================
--- httpcomponents/httpcore/branches/4.4.x/httpcore/src/test/java/org/apache/http/concurrent/TestBasicFuture.java (original)
+++ httpcomponents/httpcore/branches/4.4.x/httpcore/src/test/java/org/apache/http/concurrent/TestBasicFuture.java Mon Apr 24 10:04:28 2017
@@ -26,6 +26,7 @@
  */
 package org.apache.http.concurrent;
 
+import java.util.concurrent.CancellationException;
 import java.util.concurrent.ExecutionException;
 import java.util.concurrent.TimeUnit;
 import java.util.concurrent.TimeoutException;
@@ -118,7 +119,11 @@ public class TestBasicFuture {
         Assert.assertNull(callback.getException());
         Assert.assertTrue(callback.isCancelled());
 
-        Assert.assertNull(future.get());
+        try {
+            future.get();
+            Assert.fail("CancellationException expected");
+        } catch (final CancellationException ex) {
+        }
         Assert.assertTrue(future.isDone());
         Assert.assertTrue(future.isCancelled());
     }
@@ -175,7 +180,7 @@ public class TestBasicFuture {
         Assert.assertFalse(future.isCancelled());
     }
 
-    @Test
+    @Test(expected = CancellationException.class)
     public void testAsyncCancelled() throws Exception {
         final BasicFuture<Object> future = new BasicFuture<Object>(null);
 
@@ -193,9 +198,7 @@ public class TestBasicFuture {
         };
         t.setDaemon(true);
         t.start();
-        Assert.assertNull(future.get(60, TimeUnit.SECONDS));
-        Assert.assertTrue(future.isDone());
-        Assert.assertTrue(future.isCancelled());
+        future.get(60, TimeUnit.SECONDS);
     }
 
     @Test(expected=TimeoutException.class)