hc-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From ol...@apache.org
Subject svn commit: r575383 - 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/ test/java/org/apache/http/impl/nio/reactor/ test/java/org/apac...
Date Thu, 13 Sep 2007 17:55:00 GMT
Author: olegk
Date: Thu Sep 13 10:54:59 2007
New Revision: 575383

URL: http://svn.apache.org/viewvc?rev=575383&view=rev
Log:
HTTPCORE-109:
* Handle condition at which an I/O reactor has been instantiated but not yet activated
* Added a test case to test proper closure of active connection upon the graceful shutdown
of the I/O reactor 

Added:
    jakarta/httpcomponents/httpcore/trunk/module-nio/src/test/java/org/apache/http/HttpCoreNIOTestBase.java
  (with props)
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/AbstractMultiworkerIOReactor.java
    jakarta/httpcomponents/httpcore/trunk/module-nio/src/main/java/org/apache/http/nio/reactor/IOReactor.java
    jakarta/httpcomponents/httpcore/trunk/module-nio/src/test/java/org/apache/http/impl/nio/reactor/TestDefaultIOReactors.java
    jakarta/httpcomponents/httpcore/trunk/module-nio/src/test/java/org/apache/http/mockup/TestHttpClient.java
    jakarta/httpcomponents/httpcore/trunk/module-nio/src/test/java/org/apache/http/mockup/TestHttpServer.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?rev=575383&r1=575382&r2=575383&view=diff
==============================================================================
--- 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
Thu Sep 13 10:54:59 2007
@@ -75,7 +75,7 @@
             throw new IOReactorException("Failure opening selector", ex);
         }
         this.shutdownMutex = new Object();
-        this.status = ACTIVE;
+        this.status = INACTIVE;
     }
 
     protected abstract void acceptable(SelectionKey key);
@@ -112,7 +112,9 @@
             throw new IllegalArgumentException("Event dispatcher may not be null");
         }
         this.eventDispatch = eventDispatch;
-        
+
+        this.status = ACTIVE;
+
         try {
             for (;;) {
                 
@@ -312,10 +314,12 @@
     }
         
     public void shutdown(long gracePeriod) throws IOReactorException {
-        gracefulShutdown();
-        try {
-            awaitShutdown(gracePeriod);
-        } catch (InterruptedException ignore) {
+        if (this.status != INACTIVE) {
+            gracefulShutdown();
+            try {
+                awaitShutdown(gracePeriod);
+            } catch (InterruptedException ignore) {
+            }
         }
         if (this.status != SHUT_DOWN) {
             hardShutdown();

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=575383&r1=575382&r2=575383&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
Thu Sep 13 10:54:59 2007
@@ -72,7 +72,7 @@
         }
         this.workers = new Worker[workerCount];
         this.threads = new Thread[workerCount];
-        this.status = ACTIVE;
+        this.status = INACTIVE;
     }
 
     public int getStatus() {
@@ -84,6 +84,7 @@
     }
     
     protected void startWorkers(final IOEventDispatch eventDispatch) {
+        this.status = ACTIVE;
         for (int i = 0; i < this.workerCount; i++) {
             BaseIOReactor dispatcher = this.dispatchers[i];
             this.workers[i] = new Worker(dispatcher, eventDispatch);
@@ -109,7 +110,9 @@
         // in time
         for (int i = 0; i < this.workerCount; i++) {
             BaseIOReactor dispatcher = this.dispatchers[i];
-            dispatcher.awaitShutdown(timeout);
+            if (dispatcher.getStatus() != INACTIVE) {
+                dispatcher.awaitShutdown(timeout);
+            }
             if (dispatcher.getStatus() != SHUT_DOWN) {
                 dispatcher.hardShutdown();
             }

Modified: jakarta/httpcomponents/httpcore/trunk/module-nio/src/main/java/org/apache/http/nio/reactor/IOReactor.java
URL: http://svn.apache.org/viewvc/jakarta/httpcomponents/httpcore/trunk/module-nio/src/main/java/org/apache/http/nio/reactor/IOReactor.java?rev=575383&r1=575382&r2=575383&view=diff
==============================================================================
--- jakarta/httpcomponents/httpcore/trunk/module-nio/src/main/java/org/apache/http/nio/reactor/IOReactor.java
(original)
+++ jakarta/httpcomponents/httpcore/trunk/module-nio/src/main/java/org/apache/http/nio/reactor/IOReactor.java
Thu Sep 13 10:54:59 2007
@@ -35,9 +35,10 @@
 
 public interface IOReactor {
 
-    public static final int ACTIVE           = 0;
-    public static final int SHUTTING_DOWN    = 1;
-    public static final int SHUT_DOWN        = 2;
+    public static final int INACTIVE         = 0;
+    public static final int ACTIVE           = 1;
+    public static final int SHUTTING_DOWN    = 2;
+    public static final int SHUT_DOWN        = 3;
      
     int getStatus();
     

Added: jakarta/httpcomponents/httpcore/trunk/module-nio/src/test/java/org/apache/http/HttpCoreNIOTestBase.java
URL: http://svn.apache.org/viewvc/jakarta/httpcomponents/httpcore/trunk/module-nio/src/test/java/org/apache/http/HttpCoreNIOTestBase.java?rev=575383&view=auto
==============================================================================
--- jakarta/httpcomponents/httpcore/trunk/module-nio/src/test/java/org/apache/http/HttpCoreNIOTestBase.java
(added)
+++ jakarta/httpcomponents/httpcore/trunk/module-nio/src/test/java/org/apache/http/HttpCoreNIOTestBase.java
Thu Sep 13 10:54:59 2007
@@ -0,0 +1,152 @@
+/*
+ * $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;
+
+import junit.framework.TestCase;
+
+import org.apache.http.impl.DefaultConnectionReuseStrategy;
+import org.apache.http.impl.DefaultHttpResponseFactory;
+import org.apache.http.mockup.SimpleHttpRequestHandlerResolver;
+import org.apache.http.mockup.TestHttpClient;
+import org.apache.http.mockup.TestHttpServer;
+import org.apache.http.nio.NHttpClientHandler;
+import org.apache.http.nio.NHttpServiceHandler;
+import org.apache.http.nio.protocol.BufferingHttpClientHandler;
+import org.apache.http.nio.protocol.BufferingHttpServiceHandler;
+import org.apache.http.nio.protocol.EventListener;
+import org.apache.http.nio.protocol.HttpRequestExecutionHandler;
+import org.apache.http.params.BasicHttpParams;
+import org.apache.http.params.HttpConnectionParams;
+import org.apache.http.params.HttpParams;
+import org.apache.http.params.HttpProtocolParams;
+import org.apache.http.protocol.BasicHttpProcessor;
+import org.apache.http.protocol.HttpExpectationVerifier;
+import org.apache.http.protocol.HttpRequestHandler;
+import org.apache.http.protocol.RequestConnControl;
+import org.apache.http.protocol.RequestContent;
+import org.apache.http.protocol.RequestExpectContinue;
+import org.apache.http.protocol.RequestTargetHost;
+import org.apache.http.protocol.RequestUserAgent;
+import org.apache.http.protocol.ResponseConnControl;
+import org.apache.http.protocol.ResponseContent;
+import org.apache.http.protocol.ResponseDate;
+import org.apache.http.protocol.ResponseServer;
+
+/**
+ * Base class for all HttpCore NIO tests
+ *
+ * @author <a href="mailto:oleg at ural.ru">Oleg Kalnichevski</a>
+ */
+public class HttpCoreNIOTestBase extends TestCase {
+
+    public HttpCoreNIOTestBase(String testName) {
+        super(testName);
+    }
+
+    protected TestHttpServer server;
+    protected TestHttpClient client;
+    
+    protected void setUp() throws Exception {
+        HttpParams serverParams = new BasicHttpParams();
+        serverParams
+            .setIntParameter(HttpConnectionParams.SO_TIMEOUT, 5000)
+            .setIntParameter(HttpConnectionParams.SOCKET_BUFFER_SIZE, 8 * 1024)
+            .setBooleanParameter(HttpConnectionParams.STALE_CONNECTION_CHECK, false)
+            .setBooleanParameter(HttpConnectionParams.TCP_NODELAY, true)
+            .setParameter(HttpProtocolParams.ORIGIN_SERVER, "TEST-SERVER/1.1");
+        
+        this.server = new TestHttpServer(serverParams);
+        
+        HttpParams clientParams = new BasicHttpParams();
+        clientParams
+            .setIntParameter(HttpConnectionParams.SO_TIMEOUT, 5000)
+            .setIntParameter(HttpConnectionParams.CONNECTION_TIMEOUT, 2000)
+            .setIntParameter(HttpConnectionParams.SOCKET_BUFFER_SIZE, 8 * 1024)
+            .setBooleanParameter(HttpConnectionParams.STALE_CONNECTION_CHECK, false)
+            .setBooleanParameter(HttpConnectionParams.TCP_NODELAY, true)
+            .setParameter(HttpProtocolParams.USER_AGENT, "TEST-CLIENT/1.1");
+        
+        this.client = new TestHttpClient(clientParams);
+    }
+
+    protected void tearDown() throws Exception {
+        this.server.shutdown();
+        this.client.shutdown();
+    }
+    
+    protected NHttpServiceHandler createHttpServiceHandler(
+            final HttpRequestHandler requestHandler,
+            final HttpExpectationVerifier expectationVerifier,
+            final EventListener eventListener) {
+        
+        BasicHttpProcessor httpproc = new BasicHttpProcessor();
+        httpproc.addInterceptor(new ResponseDate());
+        httpproc.addInterceptor(new ResponseServer());
+        httpproc.addInterceptor(new ResponseContent());
+        httpproc.addInterceptor(new ResponseConnControl());
+
+        BufferingHttpServiceHandler serviceHandler = new BufferingHttpServiceHandler(
+                httpproc,
+                new DefaultHttpResponseFactory(),
+                new DefaultConnectionReuseStrategy(),
+                this.server.getParams());
+
+        serviceHandler.setHandlerResolver(
+                new SimpleHttpRequestHandlerResolver(requestHandler));
+        serviceHandler.setExpectationVerifier(expectationVerifier);
+        serviceHandler.setEventListener(eventListener);
+        
+        return serviceHandler;
+    }
+    
+    protected NHttpClientHandler createHttpClientHandler(
+            final HttpRequestExecutionHandler requestExecutionHandler,
+            final EventListener eventListener) {
+        
+        BasicHttpProcessor httpproc = new BasicHttpProcessor();
+        httpproc.addInterceptor(new RequestContent());
+        httpproc.addInterceptor(new RequestTargetHost());
+        httpproc.addInterceptor(new RequestConnControl());
+        httpproc.addInterceptor(new RequestUserAgent());
+        httpproc.addInterceptor(new RequestExpectContinue());
+
+        BufferingHttpClientHandler clientHandler = new BufferingHttpClientHandler(
+                httpproc,
+                requestExecutionHandler,
+                new DefaultConnectionReuseStrategy(),
+                this.client.getParams());
+
+        clientHandler.setEventListener(eventListener);
+        
+        return clientHandler;
+    }
+    
+}

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

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

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

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=575383&r1=575382&r2=575383&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
Thu Sep 13 10:54:59 2007
@@ -30,15 +30,29 @@
 
 package org.apache.http.impl.nio.reactor;
 
+import java.io.IOException;
 import java.net.InetSocketAddress;
 
-import org.apache.http.params.BasicHttpParams;
-import org.apache.http.params.HttpParams;
-
 import junit.framework.Test;
-import junit.framework.TestCase;
 import junit.framework.TestSuite;
 
+import org.apache.http.HttpCoreNIOTestBase;
+import org.apache.http.HttpException;
+import org.apache.http.HttpRequest;
+import org.apache.http.HttpResponse;
+import org.apache.http.message.BasicHttpRequest;
+import org.apache.http.mockup.RequestCount;
+import org.apache.http.mockup.SimpleEventListener;
+import org.apache.http.nio.NHttpClientHandler;
+import org.apache.http.nio.NHttpConnection;
+import org.apache.http.nio.NHttpServiceHandler;
+import org.apache.http.nio.protocol.EventListener;
+import org.apache.http.nio.protocol.HttpRequestExecutionHandler;
+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}.
  *
@@ -46,7 +60,7 @@
  * 
  * @version $Id$
  */
-public class TestDefaultIOReactors extends TestCase {
+public class TestDefaultIOReactors extends HttpCoreNIOTestBase {
 
     // ------------------------------------------------------------ Constructor
     public TestDefaultIOReactors(String testName) {
@@ -65,7 +79,7 @@
         return new TestSuite(TestDefaultIOReactors.class);
     }
 
-    public void testRestart() throws Exception {
+    public void testRestartListeningIOReactor() throws Exception {
         HttpParams params = new BasicHttpParams();
         
         DefaultListeningIOReactor ioReactor = new DefaultListeningIOReactor(1, params);
@@ -75,6 +89,102 @@
         ioReactor = new DefaultListeningIOReactor(1, params);
         ioReactor.listen(new InetSocketAddress(9999));
         ioReactor.shutdown();         
+    }
+    
+    public void testGracefulShutdown() throws Exception {
+
+        // Open some connection and make sure 
+        // they get cleanly closed upon shutdown
+        
+        final int connNo = 10;
+        final RequestCount requestConns = new RequestCount(connNo); 
+        final RequestCount closedServerConns = new RequestCount(connNo); 
+        final RequestCount closedClientConns = new RequestCount(connNo); 
+        
+        HttpRequestHandler requestHandler = new HttpRequestHandler() {
+
+            public void handle(
+                    final HttpRequest request, 
+                    final HttpResponse response, 
+                    final HttpContext context) throws HttpException, IOException {
+            }
+            
+        };
+        
+        HttpRequestExecutionHandler requestExecutionHandler = new HttpRequestExecutionHandler()
{
+
+            public void initalizeContext(final HttpContext context, final Object attachment)
{
+            }
+
+            public void finalizeContext(final HttpContext context) {
+            }
+
+            public HttpRequest submitRequest(final HttpContext context) {
+                Boolean b = ((Boolean) context.getAttribute("done"));
+                if (b == null) {
+                    BasicHttpRequest get = new BasicHttpRequest("GET", "/");
+                    context.setAttribute("done", Boolean.TRUE);
+                    return get;
+                } else {
+                    return null;
+                }
+            }
+            
+            public void handleResponse(final HttpResponse response, final HttpContext context)
{
+                requestConns.decrement();                    
+            }
+            
+        };
+     
+        EventListener serverEventListener = new SimpleEventListener() {
+
+            public void connectionClosed(NHttpConnection conn) {
+                closedServerConns.decrement();
+                super.connectionClosed(conn);
+            }
+            
+        };
+        
+        NHttpServiceHandler serviceHandler = createHttpServiceHandler(
+                requestHandler, 
+                null,
+                serverEventListener);
+        
+        EventListener clientEventListener = new SimpleEventListener() {
+
+            public void connectionClosed(NHttpConnection conn) {
+                closedClientConns.decrement();
+                super.connectionClosed(conn);
+            }
+            
+        };
+        
+        NHttpClientHandler clientHandler = createHttpClientHandler(
+                requestExecutionHandler,
+                clientEventListener);
+
+        this.server.start(serviceHandler);
+        this.client.start(clientHandler);
+        
+        InetSocketAddress serverAddress = (InetSocketAddress) this.server.getSocketAddress();
+        
+        for (int i = 0; i < connNo; i++) {
+            this.client.openConnection(
+                    new InetSocketAddress("localhost", serverAddress.getPort()), 
+                    null);
+        }
+     
+        requestConns.await(10000);
+        assertEquals(0, requestConns.getValue());
+     
+        this.client.shutdown();
+        this.server.shutdown();
+        
+        closedClientConns.await(10000);
+        assertEquals(0, closedClientConns.getValue());
+     
+        closedServerConns.await(10000);
+        assertEquals(0, closedServerConns.getValue());
     }
     
 }

Modified: jakarta/httpcomponents/httpcore/trunk/module-nio/src/test/java/org/apache/http/mockup/TestHttpClient.java
URL: http://svn.apache.org/viewvc/jakarta/httpcomponents/httpcore/trunk/module-nio/src/test/java/org/apache/http/mockup/TestHttpClient.java?rev=575383&r1=575382&r2=575383&view=diff
==============================================================================
--- jakarta/httpcomponents/httpcore/trunk/module-nio/src/test/java/org/apache/http/mockup/TestHttpClient.java
(original)
+++ jakarta/httpcomponents/httpcore/trunk/module-nio/src/test/java/org/apache/http/mockup/TestHttpClient.java
Thu Sep 13 10:54:59 2007
@@ -1,7 +1,7 @@
 /*
- * $HeadURL$
- * $Revision$
- * $Date$
+ * $HeadURL:https://svn.apache.org/repos/asf/jakarta/httpcomponents/httpcore/trunk/module-nio/src/test/java/org/apache/http/mockup/TestHttpClient.java
$
+ * $Revision:575207 $
+ * $Date:2007-09-13 09:57:05 +0200 (Thu, 13 Sep 2007) $
  *
  * ====================================================================
  * Licensed to the Apache Software Foundation (ASF) under one
@@ -78,7 +78,9 @@
     public void shutdown() throws IOException {
         this.ioReactor.shutdown();
         try {
-            this.thread.join(500);
+            if (this.thread != null) {
+                this.thread.join(500);
+            }
         } catch (InterruptedException ignore) {
         }
     }

Modified: jakarta/httpcomponents/httpcore/trunk/module-nio/src/test/java/org/apache/http/mockup/TestHttpServer.java
URL: http://svn.apache.org/viewvc/jakarta/httpcomponents/httpcore/trunk/module-nio/src/test/java/org/apache/http/mockup/TestHttpServer.java?rev=575383&r1=575382&r2=575383&view=diff
==============================================================================
--- jakarta/httpcomponents/httpcore/trunk/module-nio/src/test/java/org/apache/http/mockup/TestHttpServer.java
(original)
+++ jakarta/httpcomponents/httpcore/trunk/module-nio/src/test/java/org/apache/http/mockup/TestHttpServer.java
Thu Sep 13 10:54:59 2007
@@ -1,7 +1,7 @@
 /*
- * $HeadURL$
- * $Revision$
- * $Date$
+ * $HeadURL:https://svn.apache.org/repos/asf/jakarta/httpcomponents/httpcore/trunk/module-nio/src/test/java/org/apache/http/mockup/TestHttpServer.java
$
+ * $Revision:575207 $
+ * $Date:2007-09-13 09:57:05 +0200 (Thu, 13 Sep 2007) $
  *
  * ====================================================================
  * Licensed to the Apache Software Foundation (ASF) under one
@@ -97,7 +97,9 @@
     public void shutdown() throws IOException {
         this.ioReactor.shutdown();
         try {
-            this.thread.join(500);
+            if (this.thread != null) {
+                this.thread.join(500);
+            }
         } catch (InterruptedException ignore) {
         }
     }



Mime
View raw message