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 2014/08/06 10:29:26 UTC

svn commit: r1616124 - in /httpcomponents/httpasyncclient/branches/4.0.x/httpasyncclient/src: main/java/org/apache/http/impl/nio/client/ test/java/org/apache/http/nio/client/integration/

Author: olegk
Date: Wed Aug  6 08:29:26 2014
New Revision: 1616124

URL: http://svn.apache.org/r1616124
Log:
HTTPASYNC-81: Premature response consumer termination

Modified:
    httpcomponents/httpasyncclient/branches/4.0.x/httpasyncclient/src/main/java/org/apache/http/impl/nio/client/DefaultClientExchangeHandlerImpl.java
    httpcomponents/httpasyncclient/branches/4.0.x/httpasyncclient/src/test/java/org/apache/http/nio/client/integration/TestHttpAsyncPrematureTermination.java

Modified: httpcomponents/httpasyncclient/branches/4.0.x/httpasyncclient/src/main/java/org/apache/http/impl/nio/client/DefaultClientExchangeHandlerImpl.java
URL: http://svn.apache.org/viewvc/httpcomponents/httpasyncclient/branches/4.0.x/httpasyncclient/src/main/java/org/apache/http/impl/nio/client/DefaultClientExchangeHandlerImpl.java?rev=1616124&r1=1616123&r2=1616124&view=diff
==============================================================================
--- httpcomponents/httpasyncclient/branches/4.0.x/httpasyncclient/src/main/java/org/apache/http/impl/nio/client/DefaultClientExchangeHandlerImpl.java (original)
+++ httpcomponents/httpasyncclient/branches/4.0.x/httpasyncclient/src/main/java/org/apache/http/impl/nio/client/DefaultClientExchangeHandlerImpl.java Wed Aug  6 08:29:26 2014
@@ -153,7 +153,11 @@ class DefaultClientExchangeHandlerImpl<T
         this.exec.consumeContent(this.state, decoder, ioctrl);
         if (!decoder.isCompleted() && this.responseConsumer.isDone()) {
             if (this.completed.compareAndSet(false, true)) {
-                this.resultFuture.cancel();
+                try {
+                    this.resultFuture.cancel();
+                } finally {
+                    responseConsumer.close();
+                }
             }
             this.state.setNonReusable();
             releaseConnection();

Modified: httpcomponents/httpasyncclient/branches/4.0.x/httpasyncclient/src/test/java/org/apache/http/nio/client/integration/TestHttpAsyncPrematureTermination.java
URL: http://svn.apache.org/viewvc/httpcomponents/httpasyncclient/branches/4.0.x/httpasyncclient/src/test/java/org/apache/http/nio/client/integration/TestHttpAsyncPrematureTermination.java?rev=1616124&r1=1616123&r2=1616124&view=diff
==============================================================================
--- httpcomponents/httpasyncclient/branches/4.0.x/httpasyncclient/src/test/java/org/apache/http/nio/client/integration/TestHttpAsyncPrematureTermination.java (original)
+++ httpcomponents/httpasyncclient/branches/4.0.x/httpasyncclient/src/test/java/org/apache/http/nio/client/integration/TestHttpAsyncPrematureTermination.java Wed Aug  6 08:29:26 2014
@@ -50,6 +50,8 @@ import org.apache.http.impl.DefaultHttpR
 import org.apache.http.impl.nio.DefaultNHttpServerConnection;
 import org.apache.http.impl.nio.DefaultNHttpServerConnectionFactory;
 import org.apache.http.impl.nio.client.HttpAsyncClients;
+import org.apache.http.localserver.EchoHandler;
+import org.apache.http.localserver.RandomHandler;
 import org.apache.http.nio.ContentDecoder;
 import org.apache.http.nio.ContentEncoder;
 import org.apache.http.nio.IOControl;
@@ -57,6 +59,7 @@ import org.apache.http.nio.NHttpConnecti
 import org.apache.http.nio.client.methods.HttpAsyncMethods;
 import org.apache.http.nio.entity.NStringEntity;
 import org.apache.http.nio.protocol.BasicAsyncRequestConsumer;
+import org.apache.http.nio.protocol.BasicAsyncRequestHandler;
 import org.apache.http.nio.protocol.BasicAsyncResponseProducer;
 import org.apache.http.nio.protocol.HttpAsyncExchange;
 import org.apache.http.nio.protocol.HttpAsyncExpectationVerifier;
@@ -350,7 +353,7 @@ public class TestHttpAsyncPrematureTermi
             }
         };
 
-        final Future<?> future = httpclient.execute(producer, consumer, null, null);
+        final Future<?> future = this.httpclient.execute(producer, consumer, null, null);
         try {
             future.get();
             Assert.fail();
@@ -364,4 +367,76 @@ public class TestHttpAsyncPrematureTermi
         Assert.assertTrue(failed.get());
     }
 
+    @Test
+    public void testConsumerIsDone() throws Exception {
+        final UriHttpAsyncRequestHandlerMapper registry = new UriHttpAsyncRequestHandlerMapper();
+        registry.register("/echo/*", new BasicAsyncRequestHandler(new EchoHandler()));
+        registry.register("/random/*", new BasicAsyncRequestHandler(new RandomHandler()));
+
+        this.httpclient = HttpAsyncClients.custom()
+                .setConnectionManager(this.connMgr)
+                .build();
+
+        final HttpHost target = start(registry, null);
+
+        final HttpAsyncRequestProducer producer = HttpAsyncMethods.create(target, new HttpGet("/"));
+
+        final AtomicBoolean closed = new AtomicBoolean(false);
+        final AtomicBoolean cancelled = new AtomicBoolean(false);
+        final AtomicBoolean failed = new AtomicBoolean(false);
+
+        final HttpAsyncResponseConsumer<?> consumer = new HttpAsyncResponseConsumer<Object>() {
+
+            @Override
+            public void close() throws IOException {
+                closed.set(true);
+            }
+
+            @Override
+            public boolean cancel() {
+                cancelled.set(true);
+                return false;
+            }
+
+            @Override
+            public void failed(final Exception ex) {
+                failed.set(true);
+            }
+
+            public void responseReceived(
+                    final HttpResponse response) throws IOException, HttpException {
+            }
+
+            public void consumeContent(
+                    final ContentDecoder decoder, final IOControl ioctrl) throws IOException {
+            }
+
+            public void responseCompleted(final HttpContext context) {
+            }
+
+            public Exception getException() {
+                return null;
+            }
+
+            public String getResult() {
+                return null;
+            }
+
+            @Override
+            public boolean isDone() {
+                return true; // cancels fetching the response-body
+            }
+        };
+
+        final Future<?> future = this.httpclient.execute(producer, consumer, null, null);
+        future.get();
+        Thread.sleep(1000);
+
+        connMgr.shutdown(1000);
+
+        Assert.assertTrue(future.isCancelled());
+        Assert.assertFalse(failed.get());
+        Assert.assertTrue(closed.get());
+    }
+
 }