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:06:38 UTC
svn commit: r1792448 - in /httpcomponents/httpcore/trunk:
httpcore5-testing/src/test/java/org/apache/hc/core5/testing/nio/
httpcore5/src/main/java/org/apache/hc/core5/concurrent/
httpcore5/src/test/java/org/apache/hc/core5/concurrent/
Author: olegk
Date: Mon Apr 24 10:06:38 2017
New Revision: 1792448
URL: http://svn.apache.org/viewvc?rev=1792448&view=rev
Log:
HTTPCORE-456: BasicFuture fails to honor Future interface contract by not throwing CancellationException when cancelled
Modified:
httpcomponents/httpcore/trunk/httpcore5-testing/src/test/java/org/apache/hc/core5/testing/nio/Http1IntegrationTest.java
httpcomponents/httpcore/trunk/httpcore5/src/main/java/org/apache/hc/core5/concurrent/BasicFuture.java
httpcomponents/httpcore/trunk/httpcore5/src/test/java/org/apache/hc/core5/concurrent/TestBasicFuture.java
Modified: httpcomponents/httpcore/trunk/httpcore5-testing/src/test/java/org/apache/hc/core5/testing/nio/Http1IntegrationTest.java
URL: http://svn.apache.org/viewvc/httpcomponents/httpcore/trunk/httpcore5-testing/src/test/java/org/apache/hc/core5/testing/nio/Http1IntegrationTest.java?rev=1792448&r1=1792447&r2=1792448&view=diff
==============================================================================
--- httpcomponents/httpcore/trunk/httpcore5-testing/src/test/java/org/apache/hc/core5/testing/nio/Http1IntegrationTest.java (original)
+++ httpcomponents/httpcore/trunk/httpcore5-testing/src/test/java/org/apache/hc/core5/testing/nio/Http1IntegrationTest.java Mon Apr 24 10:06:38 2017
@@ -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 extend
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 extend
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 extend
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) {
}
}
Modified: httpcomponents/httpcore/trunk/httpcore5/src/main/java/org/apache/hc/core5/concurrent/BasicFuture.java
URL: http://svn.apache.org/viewvc/httpcomponents/httpcore/trunk/httpcore5/src/main/java/org/apache/hc/core5/concurrent/BasicFuture.java?rev=1792448&r1=1792447&r2=1792448&view=diff
==============================================================================
--- httpcomponents/httpcore/trunk/httpcore5/src/main/java/org/apache/hc/core5/concurrent/BasicFuture.java (original)
+++ httpcomponents/httpcore/trunk/httpcore5/src/main/java/org/apache/hc/core5/concurrent/BasicFuture.java Mon Apr 24 10:06:38 2017
@@ -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 F
if (this.ex != null) {
throw new ExecutionException(this.ex);
}
+ if (cancelled) {
+ throw new CancellationException();
+ }
return this.result;
}
Modified: httpcomponents/httpcore/trunk/httpcore5/src/test/java/org/apache/hc/core5/concurrent/TestBasicFuture.java
URL: http://svn.apache.org/viewvc/httpcomponents/httpcore/trunk/httpcore5/src/test/java/org/apache/hc/core5/concurrent/TestBasicFuture.java?rev=1792448&r1=1792447&r2=1792448&view=diff
==============================================================================
--- httpcomponents/httpcore/trunk/httpcore5/src/test/java/org/apache/hc/core5/concurrent/TestBasicFuture.java (original)
+++ httpcomponents/httpcore/trunk/httpcore5/src/test/java/org/apache/hc/core5/concurrent/TestBasicFuture.java Mon Apr 24 10:06:38 2017
@@ -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)