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/12/01 13:04:38 UTC

svn commit: r600097 - in /jakarta/httpcomponents/httpcore/trunk/module-nio/src: main/java/org/apache/http/impl/nio/reactor/ main/java/org/apache/http/nio/reactor/ test/java/org/apache/http/impl/nio/reactor/

Author: olegk
Date: Sat Dec  1 04:04:37 2007
New Revision: 600097

URL: http://svn.apache.org/viewvc?rev=600097&view=rev
Log:
HTTPCORE-127: Test coverage for listener endpoint management code

Added:
    jakarta/httpcomponents/httpcore/trunk/module-nio/src/test/java/org/apache/http/impl/nio/reactor/TestDefaultListeningIOReactor.java   (with props)
Modified:
    jakarta/httpcomponents/httpcore/trunk/module-nio/src/main/java/org/apache/http/impl/nio/reactor/AbstractMultiworkerIOReactor.java
    jakarta/httpcomponents/httpcore/trunk/module-nio/src/main/java/org/apache/http/impl/nio/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/impl/nio/reactor/TestAllImplReactor.java
    jakarta/httpcomponents/httpcore/trunk/module-nio/src/test/java/org/apache/http/impl/nio/reactor/TestDefaultIOReactors.java

Modified: jakarta/httpcomponents/httpcore/trunk/module-nio/src/main/java/org/apache/http/impl/nio/reactor/AbstractMultiworkerIOReactor.java
URL: http://svn.apache.org/viewvc/jakarta/httpcomponents/httpcore/trunk/module-nio/src/main/java/org/apache/http/impl/nio/reactor/AbstractMultiworkerIOReactor.java?rev=600097&r1=600096&r2=600097&view=diff
==============================================================================
--- jakarta/httpcomponents/httpcore/trunk/module-nio/src/main/java/org/apache/http/impl/nio/reactor/AbstractMultiworkerIOReactor.java (original)
+++ jakarta/httpcomponents/httpcore/trunk/module-nio/src/main/java/org/apache/http/impl/nio/reactor/AbstractMultiworkerIOReactor.java Sat Dec  1 04:04:37 2007
@@ -195,19 +195,21 @@
         this.selector.wakeup();
         
         // Close out all channels
-        Set<SelectionKey> keys = this.selector.keys();
-        for (Iterator<SelectionKey> it = keys.iterator(); it.hasNext(); ) {
-            try {
-                SelectionKey key = it.next();
-                Channel channel = key.channel();
-                if (channel != null) {
-                    channel.close();
+        if (this.selector.isOpen()) {
+            Set<SelectionKey> keys = this.selector.keys();
+            for (Iterator<SelectionKey> it = keys.iterator(); it.hasNext(); ) {
+                try {
+                    SelectionKey key = it.next();
+                    Channel channel = key.channel();
+                    if (channel != null) {
+                        channel.close();
+                    }
+                } catch (IOException ignore) {
                 }
-            } catch (IOException ignore) {
             }
+            // Stop dispatching I/O events
+            this.selector.close();
         }
-        // Stop dispatching I/O events
-        this.selector.close();
 
         // Attempt to shut down I/O dispatchers gracefully
         for (int i = 0; i < this.workerCount; i++) {

Modified: jakarta/httpcomponents/httpcore/trunk/module-nio/src/main/java/org/apache/http/impl/nio/reactor/DefaultListeningIOReactor.java
URL: http://svn.apache.org/viewvc/jakarta/httpcomponents/httpcore/trunk/module-nio/src/main/java/org/apache/http/impl/nio/reactor/DefaultListeningIOReactor.java?rev=600097&r1=600096&r2=600097&view=diff
==============================================================================
--- jakarta/httpcomponents/httpcore/trunk/module-nio/src/main/java/org/apache/http/impl/nio/reactor/DefaultListeningIOReactor.java (original)
+++ jakarta/httpcomponents/httpcore/trunk/module-nio/src/main/java/org/apache/http/impl/nio/reactor/DefaultListeningIOReactor.java Sat Dec  1 04:04:37 2007
@@ -37,7 +37,9 @@
 import java.nio.channels.SelectionKey;
 import java.nio.channels.ServerSocketChannel;
 import java.nio.channels.SocketChannel;
+import java.util.ArrayList;
 import java.util.Iterator;
+import java.util.List;
 import java.util.Queue;
 import java.util.Set;
 import java.util.concurrent.ConcurrentLinkedQueue;
@@ -157,6 +159,21 @@
             }
             request.completed(serverChannel.socket().getLocalSocketAddress());
         }
+    }
+    
+    public ListenerEndpoint[] getEndpoints() {
+        List<ListenerEndpoint> list = new ArrayList<ListenerEndpoint>();
+        Set<SelectionKey> keys = this.selector.keys();
+        for (Iterator<SelectionKey> it = keys.iterator(); it.hasNext(); ) {
+            SelectionKey key = it.next();
+            if (key.isValid()) {
+                ListenerEndpoint endpoint = (ListenerEndpoint) key.attachment();
+                if (endpoint != null) {
+                    list.add(endpoint);
+                }
+            }
+        }
+        return list.toArray(new ListenerEndpoint[list.size()]);
     }
     
 }

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?rev=600097&r1=600096&r2=600097&view=diff
==============================================================================
--- 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 Dec  1 04:04:37 2007
@@ -37,4 +37,6 @@
 
     ListenerEndpoint listen(SocketAddress address);
     
+    ListenerEndpoint[] getEndpoints();
+    
 }

Modified: jakarta/httpcomponents/httpcore/trunk/module-nio/src/test/java/org/apache/http/impl/nio/reactor/TestAllImplReactor.java
URL: http://svn.apache.org/viewvc/jakarta/httpcomponents/httpcore/trunk/module-nio/src/test/java/org/apache/http/impl/nio/reactor/TestAllImplReactor.java?rev=600097&r1=600096&r2=600097&view=diff
==============================================================================
--- jakarta/httpcomponents/httpcore/trunk/module-nio/src/test/java/org/apache/http/impl/nio/reactor/TestAllImplReactor.java (original)
+++ jakarta/httpcomponents/httpcore/trunk/module-nio/src/test/java/org/apache/http/impl/nio/reactor/TestAllImplReactor.java Sat Dec  1 04:04:37 2007
@@ -42,6 +42,7 @@
         TestSuite suite = new TestSuite();
         suite.addTest(TestSessionInOutBuffers.suite());
         suite.addTest(TestDefaultIOReactors.suite());
+        suite.addTest(TestDefaultListeningIOReactor.suite());
         return suite;
     }
 

Modified: jakarta/httpcomponents/httpcore/trunk/module-nio/src/test/java/org/apache/http/impl/nio/reactor/TestDefaultIOReactors.java
URL: http://svn.apache.org/viewvc/jakarta/httpcomponents/httpcore/trunk/module-nio/src/test/java/org/apache/http/impl/nio/reactor/TestDefaultIOReactors.java?rev=600097&r1=600096&r2=600097&view=diff
==============================================================================
--- jakarta/httpcomponents/httpcore/trunk/module-nio/src/test/java/org/apache/http/impl/nio/reactor/TestDefaultIOReactors.java (original)
+++ jakarta/httpcomponents/httpcore/trunk/module-nio/src/test/java/org/apache/http/impl/nio/reactor/TestDefaultIOReactors.java Sat Dec  1 04:04:37 2007
@@ -51,13 +51,11 @@
 import org.apache.http.nio.reactor.IOReactorExceptionHandler;
 import org.apache.http.nio.reactor.IOReactorStatus;
 import org.apache.http.nio.reactor.ListenerEndpoint;
-import org.apache.http.params.BasicHttpParams;
-import org.apache.http.params.HttpParams;
 import org.apache.http.protocol.HttpContext;
 import org.apache.http.protocol.HttpRequestHandler;
 
 /**
- * Simple tests for {@link DefaultListeningIOReactor}.
+ * Tests for basic I/O functionality.
  *
  * @author <a href="mailto:oleg at ural.ru">Oleg Kalnichevski</a>
  * 
@@ -82,18 +80,6 @@
         return new TestSuite(TestDefaultIOReactors.class);
     }
 
-    public void testRestartListeningIOReactor() throws Exception {
-        HttpParams params = new BasicHttpParams();
-        
-        DefaultListeningIOReactor ioReactor = new DefaultListeningIOReactor(1, params);
-        ioReactor.listen(new InetSocketAddress(9999));
-        ioReactor.shutdown();
-        
-        ioReactor = new DefaultListeningIOReactor(1, params);
-        ioReactor.listen(new InetSocketAddress(9999));
-        ioReactor.shutdown();         
-    }
-    
     public void testGracefulShutdown() throws Exception {
 
         // Open some connection and make sure 
@@ -436,5 +422,5 @@
         this.client.shutdown();
         this.server.shutdown();
     }
-    
+
 }

Added: jakarta/httpcomponents/httpcore/trunk/module-nio/src/test/java/org/apache/http/impl/nio/reactor/TestDefaultListeningIOReactor.java
URL: http://svn.apache.org/viewvc/jakarta/httpcomponents/httpcore/trunk/module-nio/src/test/java/org/apache/http/impl/nio/reactor/TestDefaultListeningIOReactor.java?rev=600097&view=auto
==============================================================================
--- jakarta/httpcomponents/httpcore/trunk/module-nio/src/test/java/org/apache/http/impl/nio/reactor/TestDefaultListeningIOReactor.java (added)
+++ jakarta/httpcomponents/httpcore/trunk/module-nio/src/test/java/org/apache/http/impl/nio/reactor/TestDefaultListeningIOReactor.java Sat Dec  1 04:04:37 2007
@@ -0,0 +1,146 @@
+/*
+ * $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.impl.nio.reactor;
+
+import java.io.IOException;
+import java.net.InetSocketAddress;
+
+import junit.framework.Test;
+import junit.framework.TestCase;
+import junit.framework.TestSuite;
+
+import org.apache.http.impl.DefaultConnectionReuseStrategy;
+import org.apache.http.impl.DefaultHttpResponseFactory;
+import org.apache.http.impl.nio.DefaultServerIOEventDispatch;
+import org.apache.http.nio.protocol.BufferingHttpServiceHandler;
+import org.apache.http.nio.reactor.IOEventDispatch;
+import org.apache.http.nio.reactor.IOReactorStatus;
+import org.apache.http.nio.reactor.ListenerEndpoint;
+import org.apache.http.nio.reactor.ListeningIOReactor;
+import org.apache.http.params.BasicHttpParams;
+import org.apache.http.params.HttpParams;
+import org.apache.http.protocol.BasicHttpProcessor;
+import org.apache.http.protocol.ResponseConnControl;
+import org.apache.http.protocol.ResponseContent;
+import org.apache.http.protocol.ResponseDate;
+import org.apache.http.protocol.ResponseServer;
+
+/**
+ * Basic tests for {@link DefaultListeningIOReactor}.
+ *
+ * @author <a href="mailto:oleg at ural.ru">Oleg Kalnichevski</a>
+ * 
+ * @version $Id$
+ */
+public class TestDefaultListeningIOReactor extends TestCase {
+
+    // ------------------------------------------------------------ Constructor
+    public TestDefaultListeningIOReactor(String testName) {
+        super(testName);
+    }
+
+    // ------------------------------------------------------------------- Main
+    public static void main(String args[]) {
+        String[] testCaseName = { TestDefaultListeningIOReactor.class.getName() };
+        junit.textui.TestRunner.main(testCaseName);
+    }
+
+    // ------------------------------------------------------- TestCase Methods
+
+    public static Test suite() {
+        return new TestSuite(TestDefaultListeningIOReactor.class);
+    }
+    
+    public void testEndpointUpAndDown() throws Exception {
+        
+        HttpParams params = new BasicHttpParams();
+        
+        BasicHttpProcessor httpproc = new BasicHttpProcessor();
+        httpproc.addInterceptor(new ResponseDate());
+        httpproc.addInterceptor(new ResponseServer());
+        httpproc.addInterceptor(new ResponseContent());
+        httpproc.addInterceptor(new ResponseConnControl());
+
+        final BufferingHttpServiceHandler serviceHandler = new BufferingHttpServiceHandler(
+                httpproc,
+                new DefaultHttpResponseFactory(),
+                new DefaultConnectionReuseStrategy(),
+                params);
+        
+        final IOEventDispatch eventDispatch = new DefaultServerIOEventDispatch(
+                serviceHandler, 
+                params);
+        
+        final ListeningIOReactor ioreactor = new DefaultListeningIOReactor(1, params);
+        
+        Thread t = new Thread(new Runnable() {
+            
+            public void run() {
+                try {
+                    ioreactor.execute(eventDispatch);
+                } catch (IOException ex) {
+                    ex.printStackTrace();
+                }
+            }
+            
+        });
+        
+        t.start();
+        
+        ListenerEndpoint[] endpoints = ioreactor.getEndpoints();
+        assertNotNull(endpoints);
+        assertEquals(0, endpoints.length);
+        
+        ListenerEndpoint port9998 = ioreactor.listen(new InetSocketAddress(9998));
+        port9998.waitFor();
+
+        ListenerEndpoint port9999 = ioreactor.listen(new InetSocketAddress(9999));
+        port9999.waitFor();
+
+        endpoints = ioreactor.getEndpoints();
+        assertNotNull(endpoints);
+        assertEquals(2, endpoints.length);
+        
+        port9998.close();
+
+        endpoints = ioreactor.getEndpoints();
+        assertNotNull(endpoints);
+        assertEquals(1, endpoints.length);
+        
+        assertEquals(9999, ((InetSocketAddress) endpoints[0].getAddress()).getPort());
+        
+        ioreactor.shutdown(1000);
+        t.join(1000);
+        
+        assertEquals(IOReactorStatus.SHUT_DOWN, ioreactor.getStatus());
+    }
+
+}

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

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

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