hc-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From ol...@apache.org
Subject [15/50] [abbrv] httpcomponents-core git commit: HTTPCORE-456: BasicFuture fails to honor Future interface contract by not throwing CancellationException when cancelled
Date Tue, 09 May 2017 19:58:07 GMT
HTTPCORE-456: BasicFuture fails to honor Future interface contract by not throwing CancellationException
when cancelled

git-svn-id: https://svn.apache.org/repos/asf/httpcomponents/httpcore/trunk@1792448 13f79535-47bb-0310-9956-ffa450edef68


Project: http://git-wip-us.apache.org/repos/asf/httpcomponents-core/repo
Commit: http://git-wip-us.apache.org/repos/asf/httpcomponents-core/commit/c0815154
Tree: http://git-wip-us.apache.org/repos/asf/httpcomponents-core/tree/c0815154
Diff: http://git-wip-us.apache.org/repos/asf/httpcomponents-core/diff/c0815154

Branch: refs/heads/trunk
Commit: c0815154c785054f4a66f6f2876ffa465891519c
Parents: 2bb3e67
Author: Oleg Kalnichevski <olegk@apache.org>
Authored: Mon Apr 24 10:06:38 2017 +0000
Committer: Oleg Kalnichevski <olegk@apache.org>
Committed: Mon Apr 24 10:06:38 2017 +0000

----------------------------------------------------------------------
 .../core5/testing/nio/Http1IntegrationTest.java | 21 +++++++++++---------
 .../apache/hc/core5/concurrent/BasicFuture.java |  4 ++++
 .../hc/core5/concurrent/TestBasicFuture.java    | 13 +++++++-----
 3 files changed, 24 insertions(+), 14 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/httpcomponents-core/blob/c0815154/httpcore5-testing/src/test/java/org/apache/hc/core5/testing/nio/Http1IntegrationTest.java
----------------------------------------------------------------------
diff --git a/httpcore5-testing/src/test/java/org/apache/hc/core5/testing/nio/Http1IntegrationTest.java
b/httpcore5-testing/src/test/java/org/apache/hc/core5/testing/nio/Http1IntegrationTest.java
index 2d0430a..b6dbaba 100644
--- a/httpcore5-testing/src/test/java/org/apache/hc/core5/testing/nio/Http1IntegrationTest.java
+++ b/httpcore5-testing/src/test/java/org/apache/hc/core5/testing/nio/Http1IntegrationTest.java
@@ -52,6 +52,7 @@ import java.util.Map;
 import java.util.Queue;
 import java.util.Random;
 import java.util.StringTokenizer;
+import java.util.concurrent.CancellationException;
 import java.util.concurrent.ExecutionException;
 import java.util.concurrent.Executors;
 import java.util.concurrent.Future;
@@ -1212,9 +1213,8 @@ public class Http1IntegrationTest extends InternalHttp1ServerTestBase
{
         Assert.assertEquals("Hi back", entity2);
 
         try {
-            final Message<HttpResponse, String> result3 = future3.get(TIMEOUT.getDuration(),
TIMEOUT.getTimeUnit());
-            Assert.assertNull(result3);
-            Assert.assertTrue(future3.isCancelled());
+            future3.get(TIMEOUT.getDuration(), TIMEOUT.getTimeUnit());
+            Assert.fail("ExecutionException expected");
         } catch (final ExecutionException ignore) {
         }
 
@@ -1222,9 +1222,12 @@ public class Http1IntegrationTest extends InternalHttp1ServerTestBase
{
                 new BasicRequestProducer("POST", createRequestURI(serverEndpoint, "/hello-3"),
                         new BasicAsyncEntityProducer("Hi there")),
                 new BasicResponseConsumer<>(new StringAsyncEntityConsumer()), null);
-        final Message<HttpResponse, String> result4 = future4.get(TIMEOUT.getDuration(),
TIMEOUT.getTimeUnit());
-        Assert.assertNull(result4);
-        Assert.assertTrue(future4.isCancelled());
+        try {
+            future4.get(TIMEOUT.getDuration(), TIMEOUT.getTimeUnit());
+            Assert.fail("CancellationException expected");
+        } catch (CancellationException ignore) {
+            Assert.assertTrue(future4.isCancelled());
+        }
     }
 
     @Test
@@ -1276,10 +1279,10 @@ public class Http1IntegrationTest extends InternalHttp1ServerTestBase
{
         Assert.assertTrue(entity2.length() > 0);
 
         try {
-            final Message<HttpResponse, String> result3 = future3.get(TIMEOUT.getDuration(),
TIMEOUT.getTimeUnit());
-            Assert.assertNull(result3);
+            future3.get(TIMEOUT.getDuration(), TIMEOUT.getTimeUnit());
+            Assert.fail("CancellationException expected");
+        } catch (final CancellationException ignore) {
             Assert.assertTrue(future3.isCancelled());
-        } catch (final ExecutionException ignore) {
         }
     }
 

http://git-wip-us.apache.org/repos/asf/httpcomponents-core/blob/c0815154/httpcore5/src/main/java/org/apache/hc/core5/concurrent/BasicFuture.java
----------------------------------------------------------------------
diff --git a/httpcore5/src/main/java/org/apache/hc/core5/concurrent/BasicFuture.java b/httpcore5/src/main/java/org/apache/hc/core5/concurrent/BasicFuture.java
index f33a37f..399e46d 100644
--- a/httpcore5/src/main/java/org/apache/hc/core5/concurrent/BasicFuture.java
+++ b/httpcore5/src/main/java/org/apache/hc/core5/concurrent/BasicFuture.java
@@ -26,6 +26,7 @@
  */
 package org.apache.hc.core5.concurrent;
 
+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 Future<T>, Cancellable
{
         if (this.ex != null) {
             throw new ExecutionException(this.ex);
         }
+        if (cancelled) {
+            throw new CancellationException();
+        }
         return this.result;
     }
 

http://git-wip-us.apache.org/repos/asf/httpcomponents-core/blob/c0815154/httpcore5/src/test/java/org/apache/hc/core5/concurrent/TestBasicFuture.java
----------------------------------------------------------------------
diff --git a/httpcore5/src/test/java/org/apache/hc/core5/concurrent/TestBasicFuture.java b/httpcore5/src/test/java/org/apache/hc/core5/concurrent/TestBasicFuture.java
index 5667800..5a827e9 100644
--- a/httpcore5/src/test/java/org/apache/hc/core5/concurrent/TestBasicFuture.java
+++ b/httpcore5/src/test/java/org/apache/hc/core5/concurrent/TestBasicFuture.java
@@ -26,6 +26,7 @@
  */
 package org.apache.hc.core5.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<>(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)


Mime
View raw message