hc-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From ol...@apache.org
Subject svn commit: r503301 - in /jakarta/httpcomponents/httpcore/trunk/module-nio/src: main/java/org/apache/http/impl/nio/reactor/ test/java/org/apache/http/nio/mockup/ test/java/org/apache/http/nio/protocol/
Date Sat, 03 Feb 2007 20:39:45 GMT
Author: olegk
Date: Sat Feb  3 12:39:44 2007
New Revision: 503301

URL: http://svn.apache.org/viewvc?view=rev&rev=503301
Log:
* Removed synchronization of the sessions sets as the sessions are added and removed from
the session sets by the reactor thread only
* Added test cases for HTTP/1.0 POSTs

Modified:
    jakarta/httpcomponents/httpcore/trunk/module-nio/src/main/java/org/apache/http/impl/nio/reactor/AbstractIOReactor.java
    jakarta/httpcomponents/httpcore/trunk/module-nio/src/main/java/org/apache/http/impl/nio/reactor/BaseIOReactor.java
    jakarta/httpcomponents/httpcore/trunk/module-nio/src/main/java/org/apache/http/impl/nio/reactor/SessionSet.java
    jakarta/httpcomponents/httpcore/trunk/module-nio/src/test/java/org/apache/http/nio/mockup/TestHttpServiceBase.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/impl/nio/reactor/AbstractIOReactor.java
URL: http://svn.apache.org/viewvc/jakarta/httpcomponents/httpcore/trunk/module-nio/src/main/java/org/apache/http/impl/nio/reactor/AbstractIOReactor.java?view=diff&rev=503301&r1=503300&r2=503301
==============================================================================
--- jakarta/httpcomponents/httpcore/trunk/module-nio/src/main/java/org/apache/http/impl/nio/reactor/AbstractIOReactor.java
(original)
+++ jakarta/httpcomponents/httpcore/trunk/module-nio/src/main/java/org/apache/http/impl/nio/reactor/AbstractIOReactor.java
Sat Feb  3 12:39:44 2007
@@ -221,17 +221,15 @@
     }
 
     private void closeSessions() {
-        synchronized (this.sessions) {
-            for (Iterator it = this.sessions.iterator(); it.hasNext(); ) {
-                IOSession session = (IOSession) it.next();
-                if (!session.isClosed()) {    
+        for (Iterator it = this.sessions.iterator(); it.hasNext(); ) {
+            IOSession session = (IOSession) it.next();
+            if (!session.isClosed()) {    
 
-                    session.close();
-                    this.eventDispatch.disconnected(session);
-                }
+                session.close();
+                this.eventDispatch.disconnected(session);
             }
-            this.sessions.clear();
         }
+        this.sessions.clear();
     }
     
     public void shutdown() throws IOReactorException {

Modified: jakarta/httpcomponents/httpcore/trunk/module-nio/src/main/java/org/apache/http/impl/nio/reactor/BaseIOReactor.java
URL: http://svn.apache.org/viewvc/jakarta/httpcomponents/httpcore/trunk/module-nio/src/main/java/org/apache/http/impl/nio/reactor/BaseIOReactor.java?view=diff&rev=503301&r1=503300&r2=503301
==============================================================================
--- jakarta/httpcomponents/httpcore/trunk/module-nio/src/main/java/org/apache/http/impl/nio/reactor/BaseIOReactor.java
(original)
+++ jakarta/httpcomponents/httpcore/trunk/module-nio/src/main/java/org/apache/http/impl/nio/reactor/BaseIOReactor.java
Sat Feb  3 12:39:44 2007
@@ -94,30 +94,28 @@
                 }
             }
         }
-        synchronized (this.bufferingSessions) {
-            if (!this.bufferingSessions.isEmpty()) {
-                for (Iterator it = this.bufferingSessions.iterator(); it.hasNext(); ) {
-                    IOSession session = (IOSession) it.next();
-                    SessionBufferStatus bufStatus = session.getBufferStatus();
-                    if (bufStatus != null) {
-                        if (!bufStatus.hasBufferedInput()) {
-                            it.remove();
-                            continue;
-                        }
+        if (!this.bufferingSessions.isEmpty()) {
+            for (Iterator it = this.bufferingSessions.iterator(); it.hasNext(); ) {
+                IOSession session = (IOSession) it.next();
+                SessionBufferStatus bufStatus = session.getBufferStatus();
+                if (bufStatus != null) {
+                    if (!bufStatus.hasBufferedInput()) {
+                        it.remove();
+                        continue;
                     }
-                    try {
-                        int ops = session.getEventMask();
-                        if ((ops & EventMask.READ) > 0) {
-                            this.eventDispatch.inputReady(session);
-                            if (bufStatus != null) {
-                                if (!bufStatus.hasBufferedInput()) {
-                                    it.remove();
-                                }
+                }
+                try {
+                    int ops = session.getEventMask();
+                    if ((ops & EventMask.READ) > 0) {
+                        this.eventDispatch.inputReady(session);
+                        if (bufStatus != null) {
+                            if (!bufStatus.hasBufferedInput()) {
+                                it.remove();
                             }
                         }
-                    } catch (CancelledKeyException ex) {
-                        it.remove();
                     }
+                } catch (CancelledKeyException ex) {
+                    it.remove();
                 }
             }
         }

Modified: jakarta/httpcomponents/httpcore/trunk/module-nio/src/main/java/org/apache/http/impl/nio/reactor/SessionSet.java
URL: http://svn.apache.org/viewvc/jakarta/httpcomponents/httpcore/trunk/module-nio/src/main/java/org/apache/http/impl/nio/reactor/SessionSet.java?view=diff&rev=503301&r1=503300&r2=503301
==============================================================================
--- jakarta/httpcomponents/httpcore/trunk/module-nio/src/main/java/org/apache/http/impl/nio/reactor/SessionSet.java
(original)
+++ jakarta/httpcomponents/httpcore/trunk/module-nio/src/main/java/org/apache/http/impl/nio/reactor/SessionSet.java
Sat Feb  3 12:39:44 2007
@@ -46,25 +46,25 @@
         this.set = new HashSet();
     }
 
-    public synchronized void add(final IOSession session) {
+    public void add(final IOSession session) {
         if (session == null) {
             return;
         }
         this.set.add(session);
     }
 
-    public synchronized boolean remove(final IOSession session) {
+    public boolean remove(final IOSession session) {
         if (session == null) {
             return false;
         }
         return this.set.remove(session);
     }
 
-    public synchronized void clear() {
+    public void clear() {
         this.set.clear();
     }
 
-    public synchronized boolean isEmpty() {
+    public boolean isEmpty() {
         return this.set.isEmpty();
     }
     

Modified: jakarta/httpcomponents/httpcore/trunk/module-nio/src/test/java/org/apache/http/nio/mockup/TestHttpServiceBase.java
URL: http://svn.apache.org/viewvc/jakarta/httpcomponents/httpcore/trunk/module-nio/src/test/java/org/apache/http/nio/mockup/TestHttpServiceBase.java?view=diff&rev=503301&r1=503300&r2=503301
==============================================================================
--- jakarta/httpcomponents/httpcore/trunk/module-nio/src/test/java/org/apache/http/nio/mockup/TestHttpServiceBase.java
(original)
+++ jakarta/httpcomponents/httpcore/trunk/module-nio/src/test/java/org/apache/http/nio/mockup/TestHttpServiceBase.java
Sat Feb  3 12:39:44 2007
@@ -74,6 +74,10 @@
         return this.connCount;
     }
     
+    public HttpParams getParams() {
+        return this.params;
+    }
+    
     private void incrementConnCount() {
         synchronized (this.mutex) {
             this.connCount++;

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=503301&r1=503300&r2=503301
==============================================================================
--- 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 Feb  3 12:39:44 2007
@@ -43,6 +43,7 @@
 import org.apache.http.HttpException;
 import org.apache.http.HttpRequest;
 import org.apache.http.HttpResponse;
+import org.apache.http.HttpVersion;
 import org.apache.http.entity.ByteArrayEntity;
 import org.apache.http.entity.StringEntity;
 import org.apache.http.message.HttpGet;
@@ -51,6 +52,7 @@
 import org.apache.http.nio.mockup.TestHttpClient;
 import org.apache.http.nio.mockup.TestHttpServer;
 import org.apache.http.nio.protocol.HttpRequestExecutionHandler;
+import org.apache.http.params.HttpProtocolParams;
 import org.apache.http.protocol.HttpContext;
 import org.apache.http.protocol.HttpExecutionContext;
 import org.apache.http.protocol.HttpRequestHandler;
@@ -524,5 +526,153 @@
         }
         
     }
+
+    /**
+     * This test case executes a series of simple (non-pipelined) HTTP/1.0 
+     * POST requests over multiple persistent connections. 
+     */
+    public void testSimpleHttpPostsHTTP10() 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
+        // Set protocol level to HTTP/1.0
+        this.client.getParams().setParameter(
+                HttpProtocolParams.PROTOCOL_VERSION, HttpVersion.HTTP_1_0);
+        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]);
+                }
+            }
+        }
+        
+    }
+
     
 }



Mime
View raw message