hc-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From ol...@apache.org
Subject svn commit: r659198 - in /httpcomponents/httpclient/trunk/module-client/src: main/java/org/apache/http/impl/conn/SingleClientConnManager.java test/java/org/apache/http/impl/conn/TestSCMWithServer.java
Date Thu, 22 May 2008 18:42:01 GMT
Author: olegk
Date: Thu May 22 11:42:00 2008
New Revision: 659198

URL: http://svn.apache.org/viewvc?rev=659198&view=rev
Log:
HTTPCLIENT-777: SingleClientConnectionManager Needs to Recreate UniquePoolEntry

Contributed by Sam Berlin <sberlin at gmail.com>
Reviewed by Oleg Kalnichevski



Added:
    httpcomponents/httpclient/trunk/module-client/src/test/java/org/apache/http/impl/conn/TestSCMWithServer.java
Modified:
    httpcomponents/httpclient/trunk/module-client/src/main/java/org/apache/http/impl/conn/SingleClientConnManager.java

Modified: httpcomponents/httpclient/trunk/module-client/src/main/java/org/apache/http/impl/conn/SingleClientConnManager.java
URL: http://svn.apache.org/viewvc/httpcomponents/httpclient/trunk/module-client/src/main/java/org/apache/http/impl/conn/SingleClientConnManager.java?rev=659198&r1=659197&r2=659198&view=diff
==============================================================================
--- httpcomponents/httpclient/trunk/module-client/src/main/java/org/apache/http/impl/conn/SingleClientConnManager.java
(original)
+++ httpcomponents/httpclient/trunk/module-client/src/main/java/org/apache/http/impl/conn/SingleClientConnManager.java
Thu May 22 11:42:00 2008
@@ -41,6 +41,7 @@
 import org.apache.http.conn.ClientConnectionRequest;
 import org.apache.http.conn.ManagedClientConnection;
 import org.apache.http.conn.routing.HttpRoute;
+import org.apache.http.conn.routing.RouteTracker;
 import org.apache.http.conn.scheme.SchemeRegistry;
 import org.apache.http.params.HttpParams;
 
@@ -215,21 +216,33 @@
             revokeConnection();
 
         // check re-usability of the connection
+        boolean recreate = false;
+        boolean shutdown = false;
+        
         if (uniquePoolEntry.connection.isOpen()) {
-            final boolean shutdown =
-                ((uniquePoolEntry.tracker == null) || // how could that happen?
-                 !uniquePoolEntry.tracker.toRoute().equals(route));
+            RouteTracker tracker = uniquePoolEntry.tracker;
+            shutdown = (tracker == null || // can happen if method is aborted
+                        !tracker.toRoute().equals(route));
+        } else {
+            // If the connection is not open, create a new PoolEntry,
+            // as the connection may have been marked not reusable,
+            // due to aborts -- and the PoolEntry should not be reused
+            // either.  There's no harm in recreating an entry if
+            // the connection is closed.
+            recreate = true;
+        }
 
-            if (shutdown) {
-                try {
-                    uniquePoolEntry.shutdown();
-                } catch (IOException iox) {
-                    LOG.debug("Problem shutting down connection.", iox);
-                    // create a new connection, just to be sure
-                    uniquePoolEntry = new PoolEntry();
-                }
+        if (shutdown) {
+            recreate = true;
+            try {
+                uniquePoolEntry.shutdown();
+            } catch (IOException iox) {
+                LOG.debug("Problem shutting down connection.", iox);
             }
         }
+        
+        if (recreate)
+            uniquePoolEntry = new PoolEntry();
 
         managedConn = new ConnAdapter(uniquePoolEntry, route);
 

Added: httpcomponents/httpclient/trunk/module-client/src/test/java/org/apache/http/impl/conn/TestSCMWithServer.java
URL: http://svn.apache.org/viewvc/httpcomponents/httpclient/trunk/module-client/src/test/java/org/apache/http/impl/conn/TestSCMWithServer.java?rev=659198&view=auto
==============================================================================
--- httpcomponents/httpclient/trunk/module-client/src/test/java/org/apache/http/impl/conn/TestSCMWithServer.java
(added)
+++ httpcomponents/httpclient/trunk/module-client/src/test/java/org/apache/http/impl/conn/TestSCMWithServer.java
Thu May 22 11:42:00 2008
@@ -0,0 +1,103 @@
+/*
+ * $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.conn;
+
+import junit.framework.Test;
+import junit.framework.TestSuite;
+
+import org.apache.http.HttpHost;
+import org.apache.http.conn.ManagedClientConnection;
+import org.apache.http.conn.params.ConnManagerParams;
+import org.apache.http.conn.routing.HttpRoute;
+import org.apache.http.conn.scheme.SchemeRegistry;
+import org.apache.http.localserver.ServerTestBase;
+import org.apache.http.params.HttpParams;
+
+public class TestSCMWithServer extends ServerTestBase {
+
+    public TestSCMWithServer(String testName) {
+        super(testName);
+    }
+
+    public static void main(String args[]) {
+        String[] testCaseName = { TestSCMWithServer.class.getName() };
+        junit.textui.TestRunner.main(testCaseName);
+    }
+
+    public static Test suite() {
+        return new TestSuite(TestSCMWithServer.class);
+    }
+    
+    /**
+     * Helper to instantiate a <code>SingleClientConnManager</code>.
+     *
+     * @param params    the parameters, or
+     *                  <code>null</code> to use defaults
+     * @param schreg    the scheme registry, or
+     *                  <code>null</code> to use defaults
+     *
+     * @return  a connection manager to test
+     */
+    public SingleClientConnManager createSCCM(HttpParams params,
+                                              SchemeRegistry schreg) {
+        if (params == null)
+            params = defaultParams;
+        if (schreg == null)
+            schreg = supportedSchemes;
+
+        return new SingleClientConnManager(params, schreg);
+    }
+    
+    /**
+     * Tests that SCM can still connect to the same host after
+     * a connection was aborted.
+     */
+    public void testOpenAfterAbort() throws Exception {
+        HttpParams mgrpar = defaultParams.copy();
+        ConnManagerParams.setMaxTotalConnections(mgrpar, 1);
+
+        SingleClientConnManager mgr = createSCCM(mgrpar, null);
+
+        final HttpHost target = getServerHttp();
+        final HttpRoute route = new HttpRoute(target, null, false);
+
+        ManagedClientConnection conn = mgr.getConnection(route, null);
+        assertTrue(conn instanceof AbstractClientConnAdapter);
+        ((AbstractClientConnAdapter) conn).abortConnection();
+        
+        conn = mgr.getConnection(route, null);
+        assertFalse("connection should have been closed", conn.isOpen());
+        conn.open(route, httpContext, defaultParams);
+
+        mgr.releaseConnection(conn);
+        mgr.shutdown();
+    }
+}



Mime
View raw message