Index: src/test/org/apache/commons/httpclient/TestHttpConnectionManager.java
===================================================================
--- src/test/org/apache/commons/httpclient/TestHttpConnectionManager.java (revision 293568)
+++ src/test/org/apache/commons/httpclient/TestHttpConnectionManager.java (working copy)
@@ -1,5 +1,5 @@
/*
- * $Header: /home/jerenkrantz/tmp/commons/commons-convert/cvs/home/cvs/jakarta-commons//httpclient/src/test/org/apache/commons/httpclient/TestHttpConnectionManager.java,v 1.23 2004/07/17 18:58:33 mbecke Exp $
+ * $HeadURL$
* $Revision$
* $Date$
* ====================================================================
@@ -23,9 +23,6 @@
* individuals on behalf of the Apache Software Foundation. For more
* information on the Apache Software Foundation, please see
* .
- *
- * [Additional notices, if required by prior licensing conditions]
- *
*/
package org.apache.commons.httpclient;
@@ -480,8 +477,8 @@
try {
// this should fail quickly since the connection has not been released
connectionManager.getConnectionWithTimeout(host2, 100);
- fail("a httpConnection should not be available");
- } catch (ConnectTimeoutException e) {
+ fail("ConnectionPoolTimeoutException should not be available");
+ } catch (ConnectionPoolTimeoutException e) {
// this should throw an exception
}
@@ -492,12 +489,68 @@
try {
// there should be a connection available now
connection2 = connectionManager.getConnectionWithTimeout(host2, 100);
- } catch (ConnectTimeoutException e) {
+ } catch (ConnectionPoolTimeoutException e) {
e.printStackTrace();
fail("a httpConnection should have been available: " + e);
}
}
+ /**
+ * Tests the MultiThreadedHttpConnectionManager's ability to restrict the maximum number
+ * of connections per host.
+ */
+ public void testMaxConnectionsPerHost() throws Exception {
+
+ MultiThreadedHttpConnectionManager connectionManager = new MultiThreadedHttpConnectionManager();
+ connectionManager.getParams().setDefaultMaxConnectionsPerHost(1);
+ connectionManager.getParams().setMaxTotalConnections(100);
+
+ HostConfiguration host1 = new HostConfiguration();
+ host1.setHost("host1", -1, "http");
+
+ HostConfiguration host2 = new HostConfiguration();
+ host2.setHost("host2", -1, "http");
+
+ HostConfiguration host3 = new HostConfiguration();
+ host3.setHost("host3", -1, "http");
+
+ connectionManager.getParams().setMaxConnectionsPerHost(host1, 3);
+ connectionManager.getParams().setMaxConnectionsPerHost(host2, 2);
+
+ // Host1
+ HttpConnection connection1 = connectionManager.getConnectionWithTimeout(host1, 1000);
+ HttpConnection connection2 = connectionManager.getConnectionWithTimeout(host1, 1000);
+ HttpConnection connection3 = connectionManager.getConnectionWithTimeout(host1, 1000);
+ try {
+ // this should fail quickly since the connection has not been released
+ connectionManager.getConnectionWithTimeout(host1, 100);
+ fail("ConnectionPoolTimeoutException should not be available");
+ } catch (ConnectionPoolTimeoutException e) {
+ // expected
+ }
+
+ // Host2
+ connection1 = connectionManager.getConnectionWithTimeout(host2, 1000);
+ connection2 = connectionManager.getConnectionWithTimeout(host2, 1000);
+ try {
+ // this should fail quickly since the connection has not been released
+ connectionManager.getConnectionWithTimeout(host2, 100);
+ fail("ConnectionPoolTimeoutException should not be available");
+ } catch (ConnectionPoolTimeoutException e) {
+ // expected
+ }
+
+ // Host3 (should use the default per host value)
+ connection1 = connectionManager.getConnectionWithTimeout(host3, 1000);
+ try {
+ // this should fail quickly since the connection has not been released
+ connectionManager.getConnectionWithTimeout(host3, 100);
+ fail("ConnectionPoolTimeoutException should not be available");
+ } catch (ConnectionPoolTimeoutException e) {
+ // expected
+ }
+ }
+
public void testHostReusePreference() {
final MultiThreadedHttpConnectionManager connectionManager = new MultiThreadedHttpConnectionManager();
Index: src/java/org/apache/commons/httpclient/MultiThreadedHttpConnectionManager.java
===================================================================
--- src/java/org/apache/commons/httpclient/MultiThreadedHttpConnectionManager.java (revision 293568)
+++ src/java/org/apache/commons/httpclient/MultiThreadedHttpConnectionManager.java (working copy)
@@ -734,38 +734,20 @@
* @return a new connection or null if none are available
*/
public synchronized HttpConnection createConnection(HostConfiguration hostConfiguration) {
-
- HttpConnectionWithReference connection = null;
-
HostConnectionPool hostPool = getHostPool(hostConfiguration);
-
- if ((hostPool.numConnections < getMaxConnectionsPerHost())
- && (numConnections < getMaxTotalConnections())) {
-
- if (LOG.isDebugEnabled()) {
- LOG.debug("Allocating new connection, hostConfig=" + hostConfiguration);
- }
- connection = new HttpConnectionWithReference(hostConfiguration);
- connection.getParams().setDefaults(MultiThreadedHttpConnectionManager.this.params);
- connection.setHttpConnectionManager(MultiThreadedHttpConnectionManager.this);
- numConnections++;
- hostPool.numConnections++;
-
- // store a reference to this connection so that it can be cleaned up
- // in the event it is not correctly released
- storeReferenceToConnection(connection, hostConfiguration, this);
-
- } else if (LOG.isDebugEnabled()) {
- if (hostPool.numConnections >= getMaxConnectionsPerHost()) {
- LOG.debug("No connection allocated, host pool has already reached "
- + "maxConnectionsPerHost, hostConfig=" + hostConfiguration
- + ", maxConnectionsPerhost=" + getMaxConnectionsPerHost());
- } else {
- LOG.debug("No connection allocated, maxTotalConnections reached, "
- + "maxTotalConnections=" + getMaxTotalConnections());
- }
+ if (LOG.isDebugEnabled()) {
+ LOG.debug("Allocating new connection, hostConfig=" + hostConfiguration);
}
-
+ HttpConnectionWithReference connection = new HttpConnectionWithReference(
+ hostConfiguration);
+ connection.getParams().setDefaults(MultiThreadedHttpConnectionManager.this.params);
+ connection.setHttpConnectionManager(MultiThreadedHttpConnectionManager.this);
+ numConnections++;
+ hostPool.numConnections++;
+
+ // store a reference to this connection so that it can be cleaned up
+ // in the event it is not correctly released
+ storeReferenceToConnection(connection, hostConfiguration, this);
return connection;
}