hc-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From ol...@apache.org
Subject svn commit: r1169460 - in /httpcomponents/httpcore/trunk/httpcore-nio/src: main/java/org/apache/http/nio/protocol/ test/java/org/apache/http/nio/protocol/
Date Sun, 11 Sep 2011 14:12:19 GMT
Author: olegk
Date: Sun Sep 11 14:12:18 2011
New Revision: 1169460

URL: http://svn.apache.org/viewvc?rev=1169460&view=rev
Log:
Added more protocol test cases for HttpAsyncServiceHandler

Modified:
    httpcomponents/httpcore/trunk/httpcore-nio/src/main/java/org/apache/http/nio/protocol/BasicAsyncRequestProducer.java
    httpcomponents/httpcore/trunk/httpcore-nio/src/main/java/org/apache/http/nio/protocol/HttpAsyncServiceHandler.java
    httpcomponents/httpcore/trunk/httpcore-nio/src/test/java/org/apache/http/nio/protocol/TestHttpAsyncHandlers.java

Modified: httpcomponents/httpcore/trunk/httpcore-nio/src/main/java/org/apache/http/nio/protocol/BasicAsyncRequestProducer.java
URL: http://svn.apache.org/viewvc/httpcomponents/httpcore/trunk/httpcore-nio/src/main/java/org/apache/http/nio/protocol/BasicAsyncRequestProducer.java?rev=1169460&r1=1169459&r2=1169460&view=diff
==============================================================================
--- httpcomponents/httpcore/trunk/httpcore-nio/src/main/java/org/apache/http/nio/protocol/BasicAsyncRequestProducer.java
(original)
+++ httpcomponents/httpcore/trunk/httpcore-nio/src/main/java/org/apache/http/nio/protocol/BasicAsyncRequestProducer.java
Sun Sep 11 14:12:18 2011
@@ -76,10 +76,14 @@ public class BasicAsyncRequestProducer i
         this.request = request;
         if (request instanceof HttpEntityEnclosingRequest) {
             HttpEntity entity = ((HttpEntityEnclosingRequest) request).getEntity();
-            if (entity instanceof ProducingNHttpEntity) {
-                this.producer = (ProducingNHttpEntity) entity;
+            if (entity != null) {
+                if (entity instanceof ProducingNHttpEntity) {
+                    this.producer = (ProducingNHttpEntity) entity;
+                } else {
+                    this.producer = new NHttpEntityWrapper(entity);
+                }
             } else {
-                this.producer = new NHttpEntityWrapper(entity);
+                this.producer = null;
             }
         } else {
             this.producer = null;

Modified: httpcomponents/httpcore/trunk/httpcore-nio/src/main/java/org/apache/http/nio/protocol/HttpAsyncServiceHandler.java
URL: http://svn.apache.org/viewvc/httpcomponents/httpcore/trunk/httpcore-nio/src/main/java/org/apache/http/nio/protocol/HttpAsyncServiceHandler.java?rev=1169460&r1=1169459&r2=1169460&view=diff
==============================================================================
--- httpcomponents/httpcore/trunk/httpcore-nio/src/main/java/org/apache/http/nio/protocol/HttpAsyncServiceHandler.java
(original)
+++ httpcomponents/httpcore/trunk/httpcore-nio/src/main/java/org/apache/http/nio/protocol/HttpAsyncServiceHandler.java
Sun Sep 11 14:12:18 2011
@@ -351,15 +351,25 @@ public class HttpAsyncServiceHandler imp
         HttpAsyncRequestConsumer<?> consumer = httpExchange.getRequestConsumer();
         consumer.requestCompleted(context);
         httpExchange.setRequestState(MessageState.COMPLETED);
-        Exception ex = consumer.getException();
-        if (ex != null) {
-            HttpAsyncResponseProducer responseProducer = handleException(ex);
+        Exception exception = consumer.getException();
+        if (exception != null) {
+            HttpAsyncResponseProducer responseProducer = handleException(exception);
             httpExchange.setResponseProducer(responseProducer);
             conn.requestOutput();
         } else {
             Object result = consumer.getResult();
             HttpAsyncResponseTrigger trigger = new ResponseTriggerImpl(httpExchange, conn);
-            handler.handle(result, trigger, context);
+            try {
+                handler.handle(result, trigger, context);
+            } catch (HttpException ex) {
+                HttpAsyncResponseProducer responseProducer = handleException(ex);
+                httpExchange.setResponseProducer(responseProducer);
+                conn.requestOutput();
+            } catch (IOException ex) {
+                HttpAsyncResponseProducer responseProducer = handleException(ex);
+                httpExchange.setResponseProducer(responseProducer);
+                conn.requestOutput();
+            }
         }
     }
 
@@ -405,7 +415,8 @@ public class HttpAsyncServiceHandler imp
         if (this.handlerResolver != null) {
             String requestURI = request.getRequestLine().getUri();
             handler = (HttpAsyncRequestHandler<Object>) this.handlerResolver.lookup(requestURI);
-        } else {
+        }
+        if (handler == null) {
             handler = new NullRequestHandler();
         }
         return handler;

Modified: httpcomponents/httpcore/trunk/httpcore-nio/src/test/java/org/apache/http/nio/protocol/TestHttpAsyncHandlers.java
URL: http://svn.apache.org/viewvc/httpcomponents/httpcore/trunk/httpcore-nio/src/test/java/org/apache/http/nio/protocol/TestHttpAsyncHandlers.java?rev=1169460&r1=1169459&r2=1169460&view=diff
==============================================================================
--- httpcomponents/httpcore/trunk/httpcore-nio/src/test/java/org/apache/http/nio/protocol/TestHttpAsyncHandlers.java
(original)
+++ httpcomponents/httpcore/trunk/httpcore-nio/src/test/java/org/apache/http/nio/protocol/TestHttpAsyncHandlers.java
Sun Sep 11 14:12:18 2011
@@ -27,6 +27,7 @@
 
 package org.apache.http.nio.protocol;
 
+import java.io.IOException;
 import java.net.InetSocketAddress;
 import java.util.Queue;
 import java.util.concurrent.ConcurrentLinkedQueue;
@@ -150,8 +151,6 @@ public class TestHttpAsyncHandlers exten
             queue.add(future);
         }
 
-        Assert.assertEquals("Test client status", IOReactorStatus.ACTIVE, this.client.getStatus());
-
         while (!queue.isEmpty()) {
             Future<HttpResponse> future = queue.remove();
             HttpResponse response = future.get();
@@ -161,6 +160,38 @@ public class TestHttpAsyncHandlers exten
     }
 
     @Test
+    public void testHttpHeads() throws Exception {
+        HttpAsyncRequestHandlerRegistry registry = new HttpAsyncRequestHandlerRegistry();
+        registry.register("*", new BufferingAsyncRequestHandler(new SimpleRequestHandler()));
+        InetSocketAddress address = start(registry, null);
+
+        this.connpool.setDefaultMaxPerRoute(3);
+        this.connpool.setMaxTotal(3);
+
+        String pattern = RndTestPatternGenerator.generateText();
+        int count = RndTestPatternGenerator.generateCount(1000);
+
+        HttpHost target = new HttpHost("localhost", address.getPort());
+
+        Queue<Future<HttpResponse>> queue = new ConcurrentLinkedQueue<Future<HttpResponse>>();
+        for (int i = 0; i < 30; i++) {
+            BasicHttpRequest request = new BasicHttpRequest("HEAD", createRequestUri(pattern,
count));
+            Future<HttpResponse> future = this.executor.execute(
+                    new BasicAsyncRequestProducer(target, request),
+                    new BasicAsyncResponseConsumer(),
+                    this.connpool);
+            queue.add(future);
+        }
+
+        while (!queue.isEmpty()) {
+            Future<HttpResponse> future = queue.remove();
+            HttpResponse response = future.get();
+            Assert.assertNotNull(response);
+            Assert.assertEquals(HttpStatus.SC_OK, response.getStatusLine().getStatusCode());
+        }
+    }
+
+    @Test
     public void testHttpPostsWithContentLength() throws Exception {
         HttpAsyncRequestHandlerRegistry registry = new HttpAsyncRequestHandlerRegistry();
         registry.register("*", new BufferingAsyncRequestHandler(new SimpleRequestHandler()));
@@ -188,8 +219,6 @@ public class TestHttpAsyncHandlers exten
             queue.add(future);
         }
 
-        Assert.assertEquals("Test client status", IOReactorStatus.ACTIVE, this.client.getStatus());
-
         while (!queue.isEmpty()) {
             Future<HttpResponse> future = queue.remove();
             HttpResponse response = future.get();
@@ -227,8 +256,6 @@ public class TestHttpAsyncHandlers exten
             queue.add(future);
         }
 
-        Assert.assertEquals("Test client status", IOReactorStatus.ACTIVE, this.client.getStatus());
-
         while (!queue.isEmpty()) {
             Future<HttpResponse> future = queue.remove();
             HttpResponse response = future.get();
@@ -265,8 +292,6 @@ public class TestHttpAsyncHandlers exten
             queue.add(future);
         }
 
-        Assert.assertEquals("Test client status", IOReactorStatus.ACTIVE, this.client.getStatus());
-
         while (!queue.isEmpty()) {
             Future<HttpResponse> future = queue.remove();
             HttpResponse response = future.get();
@@ -276,6 +301,40 @@ public class TestHttpAsyncHandlers exten
     }
 
     @Test
+    public void testHttpPostsNoEntity() throws Exception {
+        HttpAsyncRequestHandlerRegistry registry = new HttpAsyncRequestHandlerRegistry();
+        registry.register("*", new BufferingAsyncRequestHandler(new SimpleRequestHandler()));
+        InetSocketAddress address = start(registry, null);
+
+        this.connpool.setDefaultMaxPerRoute(3);
+        this.connpool.setMaxTotal(3);
+
+        String pattern = RndTestPatternGenerator.generateText();
+        int count = RndTestPatternGenerator.generateCount(1000);
+
+        HttpHost target = new HttpHost("localhost", address.getPort());
+
+        Queue<Future<HttpResponse>> queue = new ConcurrentLinkedQueue<Future<HttpResponse>>();
+        for (int i = 0; i < 30; i++) {
+            BasicHttpEntityEnclosingRequest request = new BasicHttpEntityEnclosingRequest(
+                    "POST", createRequestUri(pattern, count));
+            request.setEntity(null);
+            Future<HttpResponse> future = this.executor.execute(
+                    new BasicAsyncRequestProducer(target, request),
+                    new BasicAsyncResponseConsumer(),
+                    this.connpool);
+            queue.add(future);
+        }
+
+        while (!queue.isEmpty()) {
+            Future<HttpResponse> future = queue.remove();
+            HttpResponse response = future.get();
+            Assert.assertNotNull(response);
+            Assert.assertEquals(HttpStatus.SC_OK, response.getStatusLine().getStatusCode());
+        }
+    }
+
+    @Test
     public void testHttpPostsWithExpectContinue() throws Exception {
         HttpAsyncRequestHandlerRegistry registry = new HttpAsyncRequestHandlerRegistry();
         registry.register("*", new BufferingAsyncRequestHandler(new SimpleRequestHandler()));
@@ -304,8 +363,6 @@ public class TestHttpAsyncHandlers exten
             queue.add(future);
         }
 
-        Assert.assertEquals("Test client status", IOReactorStatus.ACTIVE, this.client.getStatus());
-
         while (!queue.isEmpty()) {
             Future<HttpResponse> future = queue.remove();
             HttpResponse response = future.get();
@@ -369,7 +426,156 @@ public class TestHttpAsyncHandlers exten
             queue.add(future);
         }
 
-        Assert.assertEquals("Test client status", IOReactorStatus.ACTIVE, this.client.getStatus());
+        Future<HttpResponse> future1 = queue.remove();
+        HttpResponse response1 = future1.get();
+        Assert.assertEquals(HttpStatus.SC_OK, response1.getStatusLine().getStatusCode());
+
+        Future<HttpResponse> future2 = queue.remove();
+        HttpResponse response2 = future2.get();
+        Assert.assertEquals(HttpStatus.SC_OK, response2.getStatusLine().getStatusCode());
+
+        Future<HttpResponse> future3 = queue.remove();
+        HttpResponse response3 = future3.get();
+        Assert.assertEquals(HttpStatus.SC_EXPECTATION_FAILED, response3.getStatusLine().getStatusCode());
+    }
+
+    @Test
+    public void testHttpHeadsDelayedResponse() throws Exception {
+
+        class DelayedRequestHandler implements HttpAsyncRequestHandler<HttpRequest>
{
+
+            private final SimpleRequestHandler requestHandler;
+
+            public DelayedRequestHandler() {
+                super();
+                this.requestHandler = new SimpleRequestHandler();
+            }
+
+            public HttpAsyncRequestConsumer<HttpRequest> processRequest(
+                    final HttpRequest request,
+                    final HttpContext context) {
+                return new BasicAsyncRequestConsumer();
+            }
+
+            public void handle(
+                    final HttpRequest request,
+                    final HttpAsyncResponseTrigger trigger,
+                    final HttpContext context) throws HttpException, IOException {
+                ProtocolVersion ver = request.getRequestLine().getProtocolVersion();
+                if (!ver.lessEquals(HttpVersion.HTTP_1_1)) {
+                    ver = HttpVersion.HTTP_1_1;
+                }
+                final BasicHttpResponse response = new BasicHttpResponse(ver, HttpStatus.SC_OK,
"OK");
+                new Thread() {
+                    @Override
+                    public void run() {
+                        // Wait a bit, to make sure this is delayed.
+                        try { Thread.sleep(100); } catch(InterruptedException ie) {}
+                        // Set the entity after delaying...
+                        try {
+                            requestHandler.handle(request, response, context);
+                        } catch (Exception ex) {
+                            response.setStatusCode(HttpStatus.SC_INTERNAL_SERVER_ERROR);
+                        }
+                        trigger.submitResponse(new BasicAsyncResponseProducer(response));
+                    }
+                }.start();
+            }
+
+        }
+
+        HttpAsyncRequestHandlerRegistry registry = new HttpAsyncRequestHandlerRegistry();
+        registry.register("*", new DelayedRequestHandler());
+        InetSocketAddress address = start(registry, null);
+
+        this.connpool.setDefaultMaxPerRoute(3);
+        this.connpool.setMaxTotal(3);
+
+        String pattern = RndTestPatternGenerator.generateText();
+        int count = RndTestPatternGenerator.generateCount(1000);
+
+        HttpHost target = new HttpHost("localhost", address.getPort());
+
+        Queue<Future<HttpResponse>> queue = new ConcurrentLinkedQueue<Future<HttpResponse>>();
+        for (int i = 0; i < 30; i++) {
+            BasicHttpRequest request = new BasicHttpRequest("HEAD", createRequestUri(pattern,
count));
+            Future<HttpResponse> future = this.executor.execute(
+                    new BasicAsyncRequestProducer(target, request),
+                    new BasicAsyncResponseConsumer(),
+                    this.connpool);
+            queue.add(future);
+        }
+
+        while (!queue.isEmpty()) {
+            Future<HttpResponse> future = queue.remove();
+            HttpResponse response = future.get();
+            Assert.assertNotNull(response);
+            Assert.assertEquals(HttpStatus.SC_OK, response.getStatusLine().getStatusCode());
+        }
+    }
+
+    @Test
+    public void testHttpPostsWithExpectationVerificationDelayedResponse() throws Exception
{
+        HttpAsyncExpectationVerifier expectationVerifier = new HttpAsyncExpectationVerifier()
{
+
+            public void verify(
+                    final HttpRequest request,
+                    final HttpAsyncContinueTrigger trigger,
+                    final HttpContext context) throws HttpException {
+                new Thread() {
+                    @Override
+                    public void run() {
+                        // Wait a bit, to make sure this is delayed.
+                        try { Thread.sleep(100); } catch(InterruptedException ie) {}
+                        // Set the entity after delaying...
+                        ProtocolVersion ver = request.getRequestLine().getProtocolVersion();
+                        String s = request.getRequestLine().getUri();
+                        if (!s.equals("AAAAAx10")) {
+                            if (!ver.lessEquals(HttpVersion.HTTP_1_1)) {
+                                ver = HttpVersion.HTTP_1_1;
+                            }
+                            BasicHttpResponse response = new BasicHttpResponse(ver,
+                                    HttpStatus.SC_EXPECTATION_FAILED, "Expectation failed");
+                            response.setEntity(NStringEntity.create("Expectation failed"));
+                            trigger.submitResponse(new BasicAsyncResponseProducer(response));
+                        } else {
+                            trigger.continueRequest();
+                        }
+                    }
+                }.start();
+            }
+
+        };
+
+        HttpAsyncRequestHandlerRegistry registry = new HttpAsyncRequestHandlerRegistry();
+        registry.register("*", new BufferingAsyncRequestHandler(new SimpleRequestHandler()));
+        InetSocketAddress address = start(registry, expectationVerifier);
+
+        BasicHttpEntityEnclosingRequest request1 = new BasicHttpEntityEnclosingRequest(
+                "POST", createRequestUri("AAAAA", 10));
+        request1.getParams().setBooleanParameter(CoreProtocolPNames.USE_EXPECT_CONTINUE,
true);
+        request1.setEntity(NStringEntity.create(createExpectedString("AAAAA", 10)));
+        BasicHttpEntityEnclosingRequest request2 = new BasicHttpEntityEnclosingRequest(
+                "POST", createRequestUri("AAAAA", 10));
+        request2.getParams().setBooleanParameter(CoreProtocolPNames.USE_EXPECT_CONTINUE,
true);
+        request2.setEntity(NStringEntity.create(createExpectedString("AAAAA", 10)));
+        BasicHttpEntityEnclosingRequest request3 = new BasicHttpEntityEnclosingRequest(
+                "POST", createRequestUri("BBBBB", 10));
+        request3.getParams().setBooleanParameter(CoreProtocolPNames.USE_EXPECT_CONTINUE,
true);
+        request3.setEntity(NStringEntity.create(createExpectedString("BBBBB", 10)));
+
+        HttpRequest[] requests = new HttpRequest[] { request1, request2, request3 };
+
+        HttpHost target = new HttpHost("localhost", address.getPort());
+
+        Queue<Future<HttpResponse>> queue = new ConcurrentLinkedQueue<Future<HttpResponse>>();
+        for (int i = 0; i < requests.length; i++) {
+            Future<HttpResponse> future = this.executor.execute(
+                    new BasicAsyncRequestProducer(target, requests[i]),
+                    new BasicAsyncResponseConsumer(),
+                    this.connpool);
+            queue.add(future);
+        }
 
         Future<HttpResponse> future1 = queue.remove();
         HttpResponse response1 = future1.get();
@@ -384,4 +590,89 @@ public class TestHttpAsyncHandlers exten
         Assert.assertEquals(HttpStatus.SC_EXPECTATION_FAILED, response3.getStatusLine().getStatusCode());
     }
 
+    @Test
+    public void testHttpExceptionInHandler() throws Exception {
+
+        class FailingRequestHandler implements HttpAsyncRequestHandler<HttpRequest>
{
+
+            public FailingRequestHandler() {
+                super();
+            }
+
+            public HttpAsyncRequestConsumer<HttpRequest> processRequest(
+                    final HttpRequest request,
+                    final HttpContext context) {
+                return new BasicAsyncRequestConsumer();
+            }
+
+            public void handle(
+                    final HttpRequest request,
+                    final HttpAsyncResponseTrigger trigger,
+                    final HttpContext context) throws HttpException, IOException {
+                throw new HttpException("Boom");
+            }
+
+        }
+
+        HttpAsyncRequestHandlerRegistry registry = new HttpAsyncRequestHandlerRegistry();
+        registry.register("*", new FailingRequestHandler());
+        InetSocketAddress address = start(registry, null);
+
+        this.connpool.setDefaultMaxPerRoute(3);
+        this.connpool.setMaxTotal(3);
+
+        String pattern = RndTestPatternGenerator.generateText();
+        int count = RndTestPatternGenerator.generateCount(1000);
+
+        HttpHost target = new HttpHost("localhost", address.getPort());
+
+        Queue<Future<HttpResponse>> queue = new ConcurrentLinkedQueue<Future<HttpResponse>>();
+        for (int i = 0; i < 1; i++) {
+            BasicHttpRequest request = new BasicHttpRequest("GET", createRequestUri(pattern,
count));
+            Future<HttpResponse> future = this.executor.execute(
+                    new BasicAsyncRequestProducer(target, request),
+                    new BasicAsyncResponseConsumer(),
+                    this.connpool);
+            queue.add(future);
+        }
+
+        while (!queue.isEmpty()) {
+            Future<HttpResponse> future = queue.remove();
+            HttpResponse response = future.get();
+            Assert.assertNotNull(response);
+            Assert.assertEquals(HttpStatus.SC_INTERNAL_SERVER_ERROR, response.getStatusLine().getStatusCode());
+        }
+    }
+
+    @Test
+    public void testNoServiceHandler() throws Exception {
+        HttpAsyncRequestHandlerRegistry registry = new HttpAsyncRequestHandlerRegistry();
+        InetSocketAddress address = start(registry, null);
+
+        this.connpool.setDefaultMaxPerRoute(3);
+        this.connpool.setMaxTotal(3);
+
+        String pattern = RndTestPatternGenerator.generateText();
+        int count = RndTestPatternGenerator.generateCount(1000);
+
+        HttpHost target = new HttpHost("localhost", address.getPort());
+
+        Queue<Future<HttpResponse>> queue = new ConcurrentLinkedQueue<Future<HttpResponse>>();
+        for (int i = 0; i < 30; i++) {
+            BasicHttpRequest request = new BasicHttpRequest("GET", createRequestUri(pattern,
count));
+            Future<HttpResponse> future = this.executor.execute(
+                    new BasicAsyncRequestProducer(target, request),
+                    new BasicAsyncResponseConsumer(),
+                    this.connpool);
+            queue.add(future);
+        }
+
+        while (!queue.isEmpty()) {
+            Future<HttpResponse> future = queue.remove();
+            HttpResponse response = future.get();
+            Assert.assertNotNull(response);
+            Assert.assertEquals(HttpStatus.SC_NOT_IMPLEMENTED, response.getStatusLine().getStatusCode());
+        }
+    }
+
 }



Mime
View raw message