hc-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From ol...@apache.org
Subject svn commit: r500551 - in /jakarta/httpcomponents/httpcore/trunk/module-nio/src: main/java/org/apache/http/nio/impl/reactor/ main/java/org/apache/http/nio/reactor/ test/java/org/apache/http/nio/ test/java/org/apache/http/nio/mockup/ test/java/org/apache...
Date Sat, 27 Jan 2007 15:43:24 GMT
Author: olegk
Date: Sat Jan 27 07:43:23 2007
New Revision: 500551

URL: http://svn.apache.org/viewvc?view=rev&rev=500551
Log:
Added end to end HTTP protocol tests for content delimited and chunked POSTs

Added:
    jakarta/httpcomponents/httpcore/trunk/module-nio/src/test/java/org/apache/http/nio/protocol/TestAllProtocol.java
  (with props)
Modified:
    jakarta/httpcomponents/httpcore/trunk/module-nio/src/main/java/org/apache/http/nio/impl/reactor/DefaultListeningIOReactor.java
    jakarta/httpcomponents/httpcore/trunk/module-nio/src/main/java/org/apache/http/nio/reactor/ListeningIOReactor.java
    jakarta/httpcomponents/httpcore/trunk/module-nio/src/test/java/org/apache/http/nio/TestAll.java
    jakarta/httpcomponents/httpcore/trunk/module-nio/src/test/java/org/apache/http/nio/mockup/TestHttpServer.java
    jakarta/httpcomponents/httpcore/trunk/module-nio/src/test/java/org/apache/http/nio/protocol/TestNIOHttp.java

Modified: jakarta/httpcomponents/httpcore/trunk/module-nio/src/main/java/org/apache/http/nio/impl/reactor/DefaultListeningIOReactor.java
URL: http://svn.apache.org/viewvc/jakarta/httpcomponents/httpcore/trunk/module-nio/src/main/java/org/apache/http/nio/impl/reactor/DefaultListeningIOReactor.java?view=diff&rev=500551&r1=500550&r2=500551
==============================================================================
--- jakarta/httpcomponents/httpcore/trunk/module-nio/src/main/java/org/apache/http/nio/impl/reactor/DefaultListeningIOReactor.java
(original)
+++ jakarta/httpcomponents/httpcore/trunk/module-nio/src/main/java/org/apache/http/nio/impl/reactor/DefaultListeningIOReactor.java
Sat Jan 27 07:43:23 2007
@@ -161,13 +161,14 @@
         }
     }
 
-    public void listen(
+    public SocketAddress listen(
             final SocketAddress address) throws IOException {
         ServerSocketChannel serverChannel = ServerSocketChannel.open();
         serverChannel.configureBlocking(false);
         serverChannel.socket().bind(address);
         SelectionKey key = serverChannel.register(this.selector, SelectionKey.OP_ACCEPT);
         key.attach(null);
+        return serverChannel.socket().getLocalSocketAddress();
     }
 
     public void shutdown() throws IOException {

Modified: jakarta/httpcomponents/httpcore/trunk/module-nio/src/main/java/org/apache/http/nio/reactor/ListeningIOReactor.java
URL: http://svn.apache.org/viewvc/jakarta/httpcomponents/httpcore/trunk/module-nio/src/main/java/org/apache/http/nio/reactor/ListeningIOReactor.java?view=diff&rev=500551&r1=500550&r2=500551
==============================================================================
--- jakarta/httpcomponents/httpcore/trunk/module-nio/src/main/java/org/apache/http/nio/reactor/ListeningIOReactor.java
(original)
+++ jakarta/httpcomponents/httpcore/trunk/module-nio/src/main/java/org/apache/http/nio/reactor/ListeningIOReactor.java
Sat Jan 27 07:43:23 2007
@@ -36,7 +36,7 @@
 
 public interface ListeningIOReactor extends IOReactor {
 
-    void listen(SocketAddress address) 
+    SocketAddress listen(SocketAddress address) 
         throws IOException;
     
 }

Modified: jakarta/httpcomponents/httpcore/trunk/module-nio/src/test/java/org/apache/http/nio/TestAll.java
URL: http://svn.apache.org/viewvc/jakarta/httpcomponents/httpcore/trunk/module-nio/src/test/java/org/apache/http/nio/TestAll.java?view=diff&rev=500551&r1=500550&r2=500551
==============================================================================
--- jakarta/httpcomponents/httpcore/trunk/module-nio/src/test/java/org/apache/http/nio/TestAll.java
(original)
+++ jakarta/httpcomponents/httpcore/trunk/module-nio/src/test/java/org/apache/http/nio/TestAll.java
Sat Jan 27 07:43:23 2007
@@ -31,6 +31,7 @@
 package org.apache.http.nio;
 
 import org.apache.http.nio.impl.TestAllImpl;
+import org.apache.http.nio.protocol.TestAllProtocol;
 
 import junit.framework.*;
 
@@ -43,7 +44,7 @@
     public static Test suite() {
         TestSuite suite = new TestSuite();
         suite.addTest(TestAllImpl.suite());
-        
+        suite.addTest(TestAllProtocol.suite());
         return suite;
     }
 

Modified: jakarta/httpcomponents/httpcore/trunk/module-nio/src/test/java/org/apache/http/nio/mockup/TestHttpServer.java
URL: http://svn.apache.org/viewvc/jakarta/httpcomponents/httpcore/trunk/module-nio/src/test/java/org/apache/http/nio/mockup/TestHttpServer.java?view=diff&rev=500551&r1=500550&r2=500551
==============================================================================
--- jakarta/httpcomponents/httpcore/trunk/module-nio/src/test/java/org/apache/http/nio/mockup/TestHttpServer.java
(original)
+++ jakarta/httpcomponents/httpcore/trunk/module-nio/src/test/java/org/apache/http/nio/mockup/TestHttpServer.java
Sat Jan 27 07:43:23 2007
@@ -33,6 +33,7 @@
 
 import java.io.IOException;
 import java.net.InetSocketAddress;
+import java.net.SocketAddress;
 
 import org.apache.http.impl.DefaultConnectionReuseStrategy;
 import org.apache.http.impl.DefaultHttpResponseFactory;
@@ -58,12 +59,12 @@
  */
 public class TestHttpServer extends TestHttpServiceBase {
 
-    private final int port;
     private final HttpRequestHandlerRegistry reqistry;
+    private volatile SocketAddress address;
+    private final Object mutex;
     
-    public TestHttpServer(int port) throws IOException {
+    public TestHttpServer() throws IOException {
         super();
-        this.port = port;
         this.params
             .setIntParameter(HttpConnectionParams.SO_TIMEOUT, 2000)
             .setIntParameter(HttpConnectionParams.SOCKET_BUFFER_SIZE, 8 * 1024)
@@ -73,6 +74,7 @@
         
         this.reqistry = new HttpRequestHandlerRegistry();
         this.ioReactor = new DefaultListeningIOReactor(1, this.params);
+        this.mutex = new Object();
     }
     
     public void registerHandler(
@@ -82,6 +84,10 @@
     }
     
     protected void execute() throws IOException {
+        synchronized (this.mutex) {
+            this.address = ((ListeningIOReactor) this.ioReactor).listen(new InetSocketAddress(0));
+            this.mutex.notifyAll();
+        }
         BasicHttpProcessor httpproc = new BasicHttpProcessor();
         httpproc.addInterceptor(new ResponseDate());
         httpproc.addInterceptor(new ResponseServer());
@@ -101,9 +107,16 @@
         IOEventDispatch ioEventDispatch = new DefaultServerIOEventDispatch(
                 serviceHandler, 
                 this.params);
-        
-        ((ListeningIOReactor) this.ioReactor).listen(new InetSocketAddress(this.port));
         this.ioReactor.execute(ioEventDispatch);
+    }
+    
+    public SocketAddress getSocketAddress() throws InterruptedException {
+        synchronized (this.mutex) {
+            while (this.address == null) {
+                this.mutex.wait();
+            }
+        }
+        return this.address;
     }
     
 }

Added: jakarta/httpcomponents/httpcore/trunk/module-nio/src/test/java/org/apache/http/nio/protocol/TestAllProtocol.java
URL: http://svn.apache.org/viewvc/jakarta/httpcomponents/httpcore/trunk/module-nio/src/test/java/org/apache/http/nio/protocol/TestAllProtocol.java?view=auto&rev=500551
==============================================================================
--- jakarta/httpcomponents/httpcore/trunk/module-nio/src/test/java/org/apache/http/nio/protocol/TestAllProtocol.java
(added)
+++ jakarta/httpcomponents/httpcore/trunk/module-nio/src/test/java/org/apache/http/nio/protocol/TestAllProtocol.java
Sat Jan 27 07:43:23 2007
@@ -0,0 +1,52 @@
+/*
+ * $HeadURL$
+ * $Revision$
+ * $Date$
+ * ====================================================================
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License.  You may obtain a copy of the License at
+ *
+ *   http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied.  See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ * ====================================================================
+ *
+ * This software consists of voluntary contributions made by many
+ * individuals on behalf of the Apache Software Foundation.  For more
+ * information on the Apache Software Foundation, please see
+ * <http://www.apache.org/>.
+ *
+ */
+
+package org.apache.http.nio.protocol;
+
+import junit.framework.*;
+
+public class TestAllProtocol extends TestCase {
+
+    public TestAllProtocol(String testName) {
+        super(testName);
+    }
+
+    public static Test suite() {
+        TestSuite suite = new TestSuite();
+        suite.addTest(TestNIOHttp.suite());
+        return suite;
+    }
+
+    public static void main(String args[]) {
+        String[] testCaseName = { TestAllProtocol.class.getName() };
+        junit.textui.TestRunner.main(testCaseName);
+    }
+
+}

Propchange: jakarta/httpcomponents/httpcore/trunk/module-nio/src/test/java/org/apache/http/nio/protocol/TestAllProtocol.java
------------------------------------------------------------------------------
    svn:keywords = Date Author Id Revision HeadURL

Propchange: jakarta/httpcomponents/httpcore/trunk/module-nio/src/test/java/org/apache/http/nio/protocol/TestAllProtocol.java
------------------------------------------------------------------------------
    svn:mime-type = text/plain

Modified: jakarta/httpcomponents/httpcore/trunk/module-nio/src/test/java/org/apache/http/nio/protocol/TestNIOHttp.java
URL: http://svn.apache.org/viewvc/jakarta/httpcomponents/httpcore/trunk/module-nio/src/test/java/org/apache/http/nio/protocol/TestNIOHttp.java?view=diff&rev=500551&r1=500550&r2=500551
==============================================================================
--- jakarta/httpcomponents/httpcore/trunk/module-nio/src/test/java/org/apache/http/nio/protocol/TestNIOHttp.java
(original)
+++ jakarta/httpcomponents/httpcore/trunk/module-nio/src/test/java/org/apache/http/nio/protocol/TestNIOHttp.java
Sat Jan 27 07:43:23 2007
@@ -39,11 +39,14 @@
 import java.util.Random;
 
 import org.apache.http.HttpEntity;
+import org.apache.http.HttpEntityEnclosingRequest;
 import org.apache.http.HttpException;
 import org.apache.http.HttpRequest;
 import org.apache.http.HttpResponse;
 import org.apache.http.entity.ByteArrayEntity;
+import org.apache.http.entity.StringEntity;
 import org.apache.http.message.HttpGet;
+import org.apache.http.message.HttpPost;
 import org.apache.http.nio.NHttpConnection;
 import org.apache.http.nio.mockup.TestHttpClient;
 import org.apache.http.nio.mockup.TestHttpServer;
@@ -87,7 +90,7 @@
     private TestHttpClient client;
     
     protected void setUp() throws Exception {
-        this.server = new TestHttpServer(8888);
+        this.server = new TestHttpServer();
         this.client = new TestHttpClient();
     }
 
@@ -96,7 +99,11 @@
         this.client.shutdown();
     }
 
-    public void testSimpleHttpExchange() throws Exception {
+    /**
+     * This test case executes a series of simple (non-pipelined) GET requests 
+     * over multiple connections. 
+     */
+    public void testSimpleHttpGets() throws Exception {
         
         final int connNo = 3;
         final int reqNo = 20;
@@ -200,17 +207,306 @@
         this.server.start();
         this.client.start();
         
+        InetSocketAddress serverAddress = (InetSocketAddress) this.server.getSocketAddress();
+        
+        for (int i = 0; i < responseData.length; i++) {
+            this.client.openConnection(
+                    new InetSocketAddress("localhost", serverAddress.getPort()), 
+                    responseData[i]);
+        }
+     
+        this.client.await(connNo, 1000);
+        assertEquals(connNo, this.client.getConnCount());
+        
+        this.server.await(connNo, 1000);
+        assertEquals(connNo, this.server.getConnCount());
+
+        for (int c = 0; c < responseData.length; c++) {
+            List receivedPackets = responseData[c];
+            List expectedPackets = testData;
+            assertEquals(receivedPackets.size(), expectedPackets.size());
+            for (int p = 0; p < testData.size(); p++) {
+                byte[] expected = (byte[]) testData.get(p);
+                byte[] received = (byte[]) receivedPackets.get(p);
+                
+                assertEquals(expected.length, received.length);
+                for (int i = 0; i < expected.length; i++) {
+                    assertEquals(expected[i], received[i]);
+                }
+            }
+        }
+        
+    }
+
+    /**
+     * This test case executes a series of simple (non-pipelined) POST requests 
+     * with content length delimited content over multiple connections. 
+     */
+    public void testSimpleHttpPostsWithContentLength() throws Exception {
+        
+        final int connNo = 3;
+        final int reqNo = 20;
+        
+        Random rnd = new Random();
+        
+        // Prepare some random data
+        final List testData = new ArrayList(reqNo);
+        for (int i = 0; i < reqNo; i++) {
+            int size = rnd.nextInt(5000);
+            byte[] data = new byte[size];
+            rnd.nextBytes(data);
+            testData.add(data);
+        }
+        
+        List[] responseData = new List[connNo];
+        for (int i = 0; i < responseData.length; i++) {
+            responseData[i] = new ArrayList();
+        }
+        
+        // Initialize the server-side request handler
+        this.server.registerHandler("*", new HttpRequestHandler() {
+
+            public void handle(
+                    final HttpRequest request, 
+                    final HttpResponse response, 
+                    final HttpContext context) throws HttpException, IOException {
+                
+                if (request instanceof HttpEntityEnclosingRequest) {
+                    HttpEntity incoming = ((HttpEntityEnclosingRequest) request).getEntity();
+                    byte[] data = EntityUtils.toByteArray(incoming);
+                    
+                    ByteArrayEntity outgoing = new ByteArrayEntity(data);
+                    outgoing.setChunked(false);
+                    response.setEntity(outgoing);
+                } else {
+                    StringEntity outgoing = new StringEntity("No content"); 
+                    response.setEntity(outgoing);
+                }
+            }
+            
+        });
+        
+        // Initialize the client side request executor
+        this.client.setHttpRequestExecutionHandler(new HttpRequestExecutionHandler() {
+
+            public void initalizeContext(final HttpContext context, final Object attachment)
{
+                context.setAttribute("LIST", (List) attachment);
+                context.setAttribute("STATUS", "ready");
+            }
+
+            public HttpRequest submitRequest(final HttpContext context) {
+                NHttpConnection conn = (NHttpConnection) context.getAttribute(
+                        HttpExecutionContext.HTTP_CONNECTION);
+                String status = (String) context.getAttribute("STATUS");
+                if (!status.equals("ready")) {
+                    return null;
+                }
+                int index = 0;
+                
+                Integer intobj = (Integer) context.getAttribute("INDEX");
+                if (intobj != null) {
+                    index = intobj.intValue();
+                }
+
+                HttpPost post = null;
+                if (index < reqNo) {
+                    post = new HttpPost("/?" + index);
+                    byte[] data = (byte[]) testData.get(index);
+                    ByteArrayEntity outgoing = new ByteArrayEntity(data);
+                    post.setEntity(outgoing);
+                    
+                    context.setAttribute("INDEX", new Integer(index + 1));
+                    context.setAttribute("STATUS", "busy");
+                } else {
+                    try {
+                        conn.close();
+                    } catch (IOException ex) {
+                        ex.printStackTrace();
+                    }
+                }
+                
+                return post;
+            }
+            
+            public void handleResponse(final HttpResponse response, final HttpContext context)
{
+                NHttpConnection conn = (NHttpConnection) context.getAttribute(
+                        HttpExecutionContext.HTTP_CONNECTION);
+                
+                List list = (List) context.getAttribute("LIST");
+                try {
+                    HttpEntity entity = response.getEntity();
+                    byte[] data = EntityUtils.toByteArray(entity);
+                    list.add(data);
+                } catch (IOException ex) {
+                    fail(ex.getMessage());
+                }
+
+                context.setAttribute("STATUS", "ready");
+                conn.requestInput();
+            }
+            
+        });
+        
+        this.server.start();
+        this.client.start();
+        
+        InetSocketAddress serverAddress = (InetSocketAddress) this.server.getSocketAddress();
+        
+        for (int i = 0; i < responseData.length; i++) {
+            this.client.openConnection(
+                    new InetSocketAddress("localhost", serverAddress.getPort()), 
+                    responseData[i]);
+        }
+     
+        this.client.await(connNo, 1000);
+        assertEquals(connNo, this.client.getConnCount());
+        
+        this.server.await(connNo, 1000);
+        assertEquals(connNo, this.server.getConnCount());
+
+        for (int c = 0; c < responseData.length; c++) {
+            List receivedPackets = responseData[c];
+            List expectedPackets = testData;
+            assertEquals(receivedPackets.size(), expectedPackets.size());
+            for (int p = 0; p < testData.size(); p++) {
+                byte[] expected = (byte[]) testData.get(p);
+                byte[] received = (byte[]) receivedPackets.get(p);
+                
+                assertEquals(expected.length, received.length);
+                for (int i = 0; i < expected.length; i++) {
+                    assertEquals(expected[i], received[i]);
+                }
+            }
+        }
+        
+    }
+
+    /**
+     * This test case executes a series of simple (non-pipelined) POST requests 
+     * with chunk coded content content over multiple connections. 
+     */
+    public void testSimpleHttpPostsChunked() throws Exception {
+        
+        final int connNo = 3;
+        final int reqNo = 20;
+        
+        Random rnd = new Random();
+        
+        // Prepare some random data
+        final List testData = new ArrayList(reqNo);
+        for (int i = 0; i < reqNo; i++) {
+            int size = rnd.nextInt(20000);
+            byte[] data = new byte[size];
+            rnd.nextBytes(data);
+            testData.add(data);
+        }
+        
+        List[] responseData = new List[connNo];
+        for (int i = 0; i < responseData.length; i++) {
+            responseData[i] = new ArrayList();
+        }
+        
+        // Initialize the server-side request handler
+        this.server.registerHandler("*", new HttpRequestHandler() {
+
+            public void handle(
+                    final HttpRequest request, 
+                    final HttpResponse response, 
+                    final HttpContext context) throws HttpException, IOException {
+                
+                if (request instanceof HttpEntityEnclosingRequest) {
+                    HttpEntity incoming = ((HttpEntityEnclosingRequest) request).getEntity();
+                    byte[] data = EntityUtils.toByteArray(incoming);
+                    ByteArrayEntity outgoing = new ByteArrayEntity(data);
+                    outgoing.setChunked(true);
+                    response.setEntity(outgoing);
+                } else {
+                    StringEntity outgoing = new StringEntity("No content"); 
+                    response.setEntity(outgoing);
+                }
+            }
+            
+        });
+        
+        // Initialize the client side request executor
+        this.client.setHttpRequestExecutionHandler(new HttpRequestExecutionHandler() {
+
+            public void initalizeContext(final HttpContext context, final Object attachment)
{
+                context.setAttribute("LIST", (List) attachment);
+                context.setAttribute("STATUS", "ready");
+            }
+
+            public HttpRequest submitRequest(final HttpContext context) {
+                NHttpConnection conn = (NHttpConnection) context.getAttribute(
+                        HttpExecutionContext.HTTP_CONNECTION);
+                String status = (String) context.getAttribute("STATUS");
+                if (!status.equals("ready")) {
+                    return null;
+                }
+                int index = 0;
+                
+                Integer intobj = (Integer) context.getAttribute("INDEX");
+                if (intobj != null) {
+                    index = intobj.intValue();
+                }
+
+                HttpPost post = null;
+                if (index < reqNo) {
+                    post = new HttpPost("/?" + index);
+                    byte[] data = (byte[]) testData.get(index);
+                    ByteArrayEntity outgoing = new ByteArrayEntity(data);
+                    outgoing.setChunked(true);
+                    post.setEntity(outgoing);
+                    
+                    context.setAttribute("INDEX", new Integer(index + 1));
+                    context.setAttribute("STATUS", "busy");
+                } else {
+                    try {
+                        conn.close();
+                    } catch (IOException ex) {
+                        ex.printStackTrace();
+                    }
+                }
+                
+                return post;
+            }
+            
+            public void handleResponse(final HttpResponse response, final HttpContext context)
{
+                NHttpConnection conn = (NHttpConnection) context.getAttribute(
+                        HttpExecutionContext.HTTP_CONNECTION);
+                
+                List list = (List) context.getAttribute("LIST");
+                
+                try {
+                    HttpEntity entity = response.getEntity();
+                    byte[] data = EntityUtils.toByteArray(entity);
+                    list.add(data);
+                } catch (IOException ex) {
+                    fail(ex.getMessage());
+                }
+
+                context.setAttribute("STATUS", "ready");
+                conn.requestInput();
+            }
+            
+        });
+        
+        this.server.start();
+        this.client.start();
+        
+        InetSocketAddress serverAddress = (InetSocketAddress) this.server.getSocketAddress();
+        
         for (int i = 0; i < responseData.length; i++) {
             this.client.openConnection(
-                    new InetSocketAddress("localhost", 8888), 
+                    new InetSocketAddress("localhost", serverAddress.getPort()), 
                     responseData[i]);
         }
      
         this.client.await(connNo, 1000);
-        assertEquals(3, this.client.getConnCount());
+        assertEquals(connNo, this.client.getConnCount());
         
         this.server.await(connNo, 1000);
-        assertEquals(3, this.server.getConnCount());
+        assertEquals(connNo, this.server.getConnCount());
 
         for (int c = 0; c < responseData.length; c++) {
             List receivedPackets = responseData[c];



Mime
View raw message