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 2007/09/11 21:08:23 UTC

svn commit: r574667 - 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/

Author: olegk
Date: Tue Sep 11 12:08:22 2007
New Revision: 574667

URL: http://svn.apache.org/viewvc?rev=574667&view=rev
Log:
HTTPCORE-109: 
* Made SessionSet synchronized
* Refactored protocol integration test

Added:
    jakarta/httpcomponents/httpcore/trunk/module-nio/src/test/java/org/apache/http/nio/mockup/RequestCount.java   (with props)
    jakarta/httpcomponents/httpcore/trunk/module-nio/src/test/java/org/apache/http/nio/mockup/SimpleEventListener.java
      - copied, changed from r573981, jakarta/httpcomponents/httpcore/trunk/module-nio/src/test/java/org/apache/http/nio/mockup/CountingEventListener.java
Removed:
    jakarta/httpcomponents/httpcore/trunk/module-nio/src/test/java/org/apache/http/nio/mockup/CountingEventListener.java
Modified:
    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/protocol/TestNIOHttp.java

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?rev=574667&r1=574666&r2=574667&view=diff
==============================================================================
--- 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 Tue Sep 11 12:08:22 2007
@@ -46,25 +46,25 @@
         this.set = new HashSet();
     }
 
-    public void add(final IOSession session) {
+    public synchronized void add(final IOSession session) {
         if (session == null) {
             return;
         }
         this.set.add(session);
     }
 
-    public boolean remove(final IOSession session) {
+    public synchronized boolean remove(final IOSession session) {
         if (session == null) {
             return false;
         }
         return this.set.remove(session);
     }
 
-    public void clear() {
+    public synchronized void clear() {
         this.set.clear();
     }
 
-    public boolean isEmpty() {
+    public synchronized boolean isEmpty() {
         return this.set.isEmpty();
     }
     

Added: jakarta/httpcomponents/httpcore/trunk/module-nio/src/test/java/org/apache/http/nio/mockup/RequestCount.java
URL: http://svn.apache.org/viewvc/jakarta/httpcomponents/httpcore/trunk/module-nio/src/test/java/org/apache/http/nio/mockup/RequestCount.java?rev=574667&view=auto
==============================================================================
--- jakarta/httpcomponents/httpcore/trunk/module-nio/src/test/java/org/apache/http/nio/mockup/RequestCount.java (added)
+++ jakarta/httpcomponents/httpcore/trunk/module-nio/src/test/java/org/apache/http/nio/mockup/RequestCount.java Tue Sep 11 12:08:22 2007
@@ -0,0 +1,88 @@
+/*
+ * $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.mockup;
+
+public class RequestCount {
+
+    private volatile boolean aborted;
+    private volatile int value;
+    
+    public RequestCount(int initialValue) {
+        this.value = initialValue;
+        this.aborted = false;
+    }
+    
+    public int getValue() {
+        return this.value;
+    }
+    
+    public void decrement() {
+        synchronized (this) {
+            if (!this.aborted) {
+                this.value--;
+            }
+            notifyAll();
+        }
+    }
+
+    public void abort() {
+        synchronized (this) {
+            this.aborted = true;
+            notifyAll();
+        }
+    }
+
+    public boolean isAborted() {
+        return this.aborted;
+    }
+    
+    public void await(int count, long timeout) throws InterruptedException {
+        synchronized (this) {
+            long deadline = System.currentTimeMillis() + timeout;
+            long remaining = timeout;
+            while (!this.aborted && this.value > count) {
+                wait(remaining);
+                if (timeout > 0) {
+                    remaining = deadline - System.currentTimeMillis();
+                    if (remaining <= 0) {
+                        break;
+                    }
+                }
+            }
+        }
+    }
+    
+    public void await(long timeout) throws InterruptedException {
+        await(0, timeout);
+    }
+    
+}

Propchange: jakarta/httpcomponents/httpcore/trunk/module-nio/src/test/java/org/apache/http/nio/mockup/RequestCount.java
------------------------------------------------------------------------------
    svn:eol-style = native

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

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

Copied: jakarta/httpcomponents/httpcore/trunk/module-nio/src/test/java/org/apache/http/nio/mockup/SimpleEventListener.java (from r573981, jakarta/httpcomponents/httpcore/trunk/module-nio/src/test/java/org/apache/http/nio/mockup/CountingEventListener.java)
URL: http://svn.apache.org/viewvc/jakarta/httpcomponents/httpcore/trunk/module-nio/src/test/java/org/apache/http/nio/mockup/SimpleEventListener.java?p2=jakarta/httpcomponents/httpcore/trunk/module-nio/src/test/java/org/apache/http/nio/mockup/SimpleEventListener.java&p1=jakarta/httpcomponents/httpcore/trunk/module-nio/src/test/java/org/apache/http/nio/mockup/CountingEventListener.java&r1=573981&r2=574667&rev=574667&view=diff
==============================================================================
--- jakarta/httpcomponents/httpcore/trunk/module-nio/src/test/java/org/apache/http/nio/mockup/CountingEventListener.java (original)
+++ jakarta/httpcomponents/httpcore/trunk/module-nio/src/test/java/org/apache/http/nio/mockup/SimpleEventListener.java Tue Sep 11 12:08:22 2007
@@ -37,33 +37,12 @@
 import org.apache.http.nio.NHttpConnection;
 import org.apache.http.nio.protocol.EventListener;
 
-public class CountingEventListener implements EventListener {
+public class SimpleEventListener implements EventListener {
 
-    private volatile int connCount = 0;
-    
-    public CountingEventListener() {
+    public SimpleEventListener() {
         super();
     }
     
-    public int getConnCount() {
-        return this.connCount;
-    }
-    
-    private void incrementConnCount() {
-        synchronized (this) {
-            this.connCount++;
-            this.notifyAll();
-        }
-    }
-    
-    public void await(int connCount, long timeout) throws InterruptedException {
-        synchronized (this) {
-            while (this.connCount < connCount) {
-                this.wait(timeout);
-            }
-        }
-    }
-    
     public void connectionOpen(final NHttpConnection conn) {
     }
 
@@ -71,7 +50,6 @@
     }
 
     public void connectionClosed(final NHttpConnection conn) {
-        incrementConnCount();
     }
 
     public void fatalIOException(final IOException ex, final NHttpConnection conn) {

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?rev=574667&r1=574666&r2=574667&view=diff
==============================================================================
--- 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 Tue Sep 11 12:08:22 2007
@@ -61,7 +61,8 @@
 import org.apache.http.nio.NHttpClientHandler;
 import org.apache.http.nio.NHttpConnection;
 import org.apache.http.nio.NHttpServiceHandler;
-import org.apache.http.nio.mockup.CountingEventListener;
+import org.apache.http.nio.mockup.RequestCount;
+import org.apache.http.nio.mockup.SimpleEventListener;
 import org.apache.http.nio.mockup.SimpleHttpRequestHandlerResolver;
 import org.apache.http.nio.mockup.SimpleThreadPoolExecutor;
 import org.apache.http.nio.mockup.TestHttpClient;
@@ -201,15 +202,14 @@
     }
 
     protected void tearDown() throws Exception {
-        this.executor.shutdown();
         this.server.shutdown();
         this.client.shutdown();
+        this.executor.shutdown();
     }
     
     private NHttpServiceHandler createHttpServiceHandler(
             final HttpRequestHandler requestHandler,
-            final HttpExpectationVerifier expectationVerifier,
-            final EventListener eventListener) {
+            final HttpExpectationVerifier expectationVerifier) {
         BasicHttpProcessor httpproc = new BasicHttpProcessor();
         httpproc.addInterceptor(new ResponseDate());
         httpproc.addInterceptor(new ResponseServer());
@@ -226,7 +226,7 @@
             serviceHandler.setHandlerResolver(
                     new SimpleHttpRequestHandlerResolver(requestHandler));
             serviceHandler.setExpectationVerifier(expectationVerifier);
-            serviceHandler.setEventListener(eventListener);
+            serviceHandler.setEventListener(new SimpleEventListener());
             
             return serviceHandler;
         }
@@ -241,7 +241,7 @@
             serviceHandler.setHandlerResolver(
                     new SimpleHttpRequestHandlerResolver(requestHandler));
             serviceHandler.setExpectationVerifier(expectationVerifier);
-            serviceHandler.setEventListener(eventListener);
+            serviceHandler.setEventListener(new SimpleEventListener());
             
             return serviceHandler;
         }
@@ -249,8 +249,7 @@
     }
     
     private NHttpClientHandler createHttpClientHandler(
-            final HttpRequestExecutionHandler requestExecutionHandler,
-            final EventListener eventListener) {
+            final HttpRequestExecutionHandler requestExecutionHandler) {
         BasicHttpProcessor httpproc = new BasicHttpProcessor();
         httpproc.addInterceptor(new RequestContent());
         httpproc.addInterceptor(new RequestTargetHost());
@@ -265,7 +264,7 @@
                     new DefaultConnectionReuseStrategy(),
                     this.client.getParams());
 
-            clientHandler.setEventListener(eventListener);
+            clientHandler.setEventListener(new SimpleEventListener());
             return clientHandler;
         }
         if (this.clientMode == MODE_THROTTLING) {
@@ -276,7 +275,7 @@
                     this.executor,
                     this.client.getParams());
 
-            clientHandler.setEventListener(eventListener);
+            clientHandler.setEventListener(new SimpleEventListener());
             return clientHandler;
         }
         throw new IllegalStateException();
@@ -290,6 +289,7 @@
         
         final int connNo = 3;
         final int reqNo = 20;
+        final RequestCount requestCount = new RequestCount(connNo * reqNo); 
         
         Random rnd = new Random();
         
@@ -363,33 +363,25 @@
                     HttpEntity entity = response.getEntity();
                     byte[] data = EntityUtils.toByteArray(entity);
                     list.add(data);
+                    requestCount.decrement();
                 } catch (IOException ex) {
-                    fail(ex.getMessage());
+                    requestCount.abort();
+                    return;
                 }
 
                 if (i < reqNo) {
                     conn.requestInput();
-                } else {
-                    try {
-                        conn.close();
-                    } catch (IOException ex) {
-                    }
                 }
             }
             
         };
-        
-        CountingEventListener serverEventListener = new CountingEventListener();
-        CountingEventListener clientEventListener = new CountingEventListener();
-        
+
         NHttpServiceHandler serviceHandler = createHttpServiceHandler(
                 requestHandler, 
-                null,
-                serverEventListener);
+                null);
 
         NHttpClientHandler clientHandler = createHttpClientHandler(
-                requestExecutionHandler, 
-                clientEventListener);
+                requestExecutionHandler);
 
         this.server.start(serviceHandler);
         this.client.start(clientHandler);
@@ -402,8 +394,8 @@
                     responseData[i]);
         }
      
-        clientEventListener.await(connNo, 10000);
-        assertEquals(connNo, clientEventListener.getConnCount());
+        requestCount.await(10000);
+        assertEquals(0, requestCount.getValue());
         
         this.client.shutdown();
         this.server.shutdown();
@@ -433,6 +425,7 @@
         
         final int connNo = 3;
         final int reqNo = 20;
+        final RequestCount requestCount = new RequestCount(connNo * reqNo); 
         
         Random rnd = new Random();
         
@@ -510,33 +503,25 @@
                     HttpEntity entity = response.getEntity();
                     byte[] data = EntityUtils.toByteArray(entity);
                     list.add(data);
+                    requestCount.decrement();
                 } catch (IOException ex) {
-                    fail(ex.getMessage());
+                    requestCount.abort();
+                    return;
                 }
 
                 if (i < reqNo) {
                     conn.requestInput();
-                } else {
-                    try {
-                        conn.close();
-                    } catch (IOException ex) {
-                    }
                 }
             }
             
         };
         
-        CountingEventListener serverEventListener = new CountingEventListener();
-        CountingEventListener clientEventListener = new CountingEventListener();
-        
         NHttpServiceHandler serviceHandler = createHttpServiceHandler(
                 requestHandler, 
-                null,
-                serverEventListener);
+                null);
 
         NHttpClientHandler clientHandler = createHttpClientHandler(
-                requestExecutionHandler, 
-                clientEventListener);
+                requestExecutionHandler);
 
         this.server.start(serviceHandler);
         this.client.start(clientHandler);
@@ -549,8 +534,8 @@
                     responseData[i]);
         }
      
-        clientEventListener.await(connNo, 10000);
-        assertEquals(connNo, clientEventListener.getConnCount());
+        requestCount.await(10000);
+        assertEquals(0, requestCount.getValue());
         
         this.client.shutdown();
         this.server.shutdown();
@@ -580,6 +565,7 @@
         
         final int connNo = 3;
         final int reqNo = 20;
+        final RequestCount requestCount = new RequestCount(connNo * reqNo); 
         
         Random rnd = new Random();
         
@@ -657,33 +643,25 @@
                     HttpEntity entity = response.getEntity();
                     byte[] data = EntityUtils.toByteArray(entity);
                     list.add(data);
+                    requestCount.decrement();
                 } catch (IOException ex) {
-                    fail(ex.getMessage());
+                    requestCount.abort();
+                    return;
                 }
 
                 if (i < reqNo) {
                     conn.requestInput();
-                } else {
-                    try {
-                        conn.close();
-                    } catch (IOException ex) {
-                    }
                 }
             }
             
         };
         
-        CountingEventListener serverEventListener = new CountingEventListener();
-        CountingEventListener clientEventListener = new CountingEventListener();
-        
         NHttpServiceHandler serviceHandler = createHttpServiceHandler(
                 requestHandler, 
-                null,
-                serverEventListener);
+                null);
 
         NHttpClientHandler clientHandler = createHttpClientHandler(
-                requestExecutionHandler, 
-                clientEventListener);
+                requestExecutionHandler);
 
         this.server.start(serviceHandler);
         this.client.start(clientHandler);
@@ -696,8 +674,11 @@
                     responseData[i]);
         }
      
-        clientEventListener.await(connNo, 10000);
-        assertEquals(connNo, clientEventListener.getConnCount());
+        requestCount.await(10000);
+        if (requestCount.isAborted()) {
+            System.out.println("Test case aborted");
+        }
+        assertEquals(0, requestCount.getValue());
         
         this.client.shutdown();
         this.server.shutdown();
@@ -727,6 +708,7 @@
         
         final int connNo = 3;
         final int reqNo = 20;
+        final RequestCount requestCount = new RequestCount(connNo * reqNo); 
         
         Random rnd = new Random();
         
@@ -809,33 +791,25 @@
                     HttpEntity entity = response.getEntity();
                     byte[] data = EntityUtils.toByteArray(entity);
                     list.add(data);
+                    requestCount.decrement();
                 } catch (IOException ex) {
-                    fail(ex.getMessage());
+                    requestCount.abort();
+                    return;
                 }
 
                 if (i < reqNo) {
                     conn.requestInput();
-                } else {
-                    try {
-                        conn.close();
-                    } catch (IOException ex) {
-                    }
                 }
             }
             
         };
         
-        CountingEventListener serverEventListener = new CountingEventListener();
-        CountingEventListener clientEventListener = new CountingEventListener();
-        
         NHttpServiceHandler serviceHandler = createHttpServiceHandler(
                 requestHandler, 
-                null,
-                serverEventListener);
+                null);
 
         NHttpClientHandler clientHandler = createHttpClientHandler(
-                requestExecutionHandler, 
-                clientEventListener);
+                requestExecutionHandler);
 
         this.server.start(serviceHandler);
         this.client.start(clientHandler);
@@ -848,8 +822,8 @@
                     responseData[i]);
         }
      
-        clientEventListener.await(connNo, 10000);
-        assertEquals(connNo, clientEventListener.getConnCount());
+        requestCount.await(10000);
+        assertEquals(0, requestCount.getValue());
         
         this.client.shutdown();
         this.server.shutdown();
@@ -879,6 +853,7 @@
         
         final int connNo = 3;
         final int reqNo = 20;
+        final RequestCount requestCount = new RequestCount(connNo * reqNo); 
         
         Random rnd = new Random();
         
@@ -959,33 +934,25 @@
                     HttpEntity entity = response.getEntity();
                     byte[] data = EntityUtils.toByteArray(entity);
                     list.add(data);
+                    requestCount.decrement();
                 } catch (IOException ex) {
-                    fail(ex.getMessage());
+                    requestCount.abort();
+                    return;
                 }
 
                 if (i < reqNo) {
                     conn.requestInput();
-                } else {
-                    try {
-                        conn.close();
-                    } catch (IOException ex) {
-                    }
                 }
             }
             
         };
         
-        CountingEventListener serverEventListener = new CountingEventListener();
-        CountingEventListener clientEventListener = new CountingEventListener();
-        
         NHttpServiceHandler serviceHandler = createHttpServiceHandler(
                 requestHandler, 
-                null,
-                serverEventListener);
+                null);
 
         NHttpClientHandler clientHandler = createHttpClientHandler(
-                requestExecutionHandler, 
-                clientEventListener);
+                requestExecutionHandler);
 
         this.server.start(serviceHandler);
         this.client.start(clientHandler);
@@ -998,8 +965,8 @@
                     responseData[i]);
         }
      
-        clientEventListener.await(connNo, 10000);
-        assertEquals(connNo, clientEventListener.getConnCount());
+        requestCount.await(10000);
+        assertEquals(0, requestCount.getValue());
         
         this.client.shutdown();
         this.server.shutdown();
@@ -1028,6 +995,7 @@
     public void testHttpPostsWithExpectationVerification() throws Exception {
         
         final int reqNo = 3;
+        final RequestCount requestCount = new RequestCount(reqNo); 
         final List responses = new ArrayList(reqNo);
         
         HttpRequestHandler requestHandler = new HttpRequestHandler() {
@@ -1112,35 +1080,27 @@
                     try {
                         entity.consumeContent();
                     } catch (IOException ex) {
-                        fail(ex.getMessage());
+                        requestCount.abort();
+                        return;
                     }
                 }
                 
                 list.add(response);
+                requestCount.decrement();
 
                 if (i < reqNo) {
                     conn.requestInput();
-                } else {
-                    try {
-                        conn.close();
-                    } catch (IOException ex) {
-                    }
                 }
             }
             
         };
         
-        CountingEventListener serverEventListener = new CountingEventListener();
-        CountingEventListener clientEventListener = new CountingEventListener();
-        
         NHttpServiceHandler serviceHandler = createHttpServiceHandler(
                 requestHandler, 
-                expectationVerifier,
-                serverEventListener);
+                expectationVerifier);
 
         NHttpClientHandler clientHandler = createHttpClientHandler(
-                requestExecutionHandler, 
-                clientEventListener);
+                requestExecutionHandler);
 
         this.server.start(serviceHandler);
         this.client.start(clientHandler);
@@ -1151,7 +1111,7 @@
                 new InetSocketAddress("localhost", serverAddress.getPort()), 
                 responses);
      
-        clientEventListener.await(1, 1000);
+        requestCount.await(1000);
         
         this.client.shutdown();
         this.server.shutdown();
@@ -1173,6 +1133,7 @@
         
         final int connNo = 3;
         final int reqNo = 20;
+        final RequestCount requestCount = new RequestCount(connNo * reqNo * 2); 
         
         final String[] method = new String[1];
         
@@ -1246,30 +1207,21 @@
                 context.setAttribute("RES-COUNT", new Integer(i));
 
                 list.add(response);
+                requestCount.decrement();
 
                 if (i < reqNo) {
                     conn.requestInput();
-                } else {
-                    try {
-                        conn.close();
-                    } catch (IOException ex) {
-                    }
                 }
             }
             
         };
         
-        CountingEventListener serverEventListener = new CountingEventListener();
-        CountingEventListener clientEventListener = new CountingEventListener();
-        
         NHttpServiceHandler serviceHandler = createHttpServiceHandler(
                 requestHandler, 
-                null,
-                serverEventListener);
+                null);
 
         NHttpClientHandler clientHandler = createHttpClientHandler(
-                requestExecutionHandler, 
-                clientEventListener);
+                requestExecutionHandler);
 
         this.server.start(serviceHandler);
         this.client.start(clientHandler);
@@ -1284,8 +1236,8 @@
                     responseData[i]);
         }
      
-        clientEventListener.await(connNo, 10000);
-        assertEquals(connNo, clientEventListener.getConnCount());
+        requestCount.await(connNo * reqNo, 10000);
+        assertEquals(connNo * reqNo, requestCount.getValue());
 
         List[] responseDataGET = responseData; 
 
@@ -1302,8 +1254,8 @@
                     responseData[i]);
         }
      
-        clientEventListener.await(connNo * 2, 10000);
-        assertEquals(connNo * 2, clientEventListener.getConnCount());
+        requestCount.await(10000);
+        assertEquals(0, requestCount.getValue());
         
         this.client.shutdown();
         this.server.shutdown();