You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@hc.apache.org by jo...@apache.org on 2011/07/19 18:02:46 UTC
svn commit: r1148420 - in
/httpcomponents/httpclient/trunk/httpclient/src/test/java/org/apache/http:
impl/conn/tsccm/TestConnPoolByRoute.java localserver/BasicServerTestBase.java
Author: jonm
Date: Tue Jul 19 16:02:45 2011
New Revision: 1148420
URL: http://svn.apache.org/viewvc?rev=1148420&view=rev
Log:
HTTPCLIENT-1108: Adding Mockito unit tests for ConnPoolByRoute. The unit test
down at the bottom is currently @Ignored but is the question I had on the dev
list just now--is this test correct in its assertions?
Modified:
httpcomponents/httpclient/trunk/httpclient/src/test/java/org/apache/http/impl/conn/tsccm/TestConnPoolByRoute.java
httpcomponents/httpclient/trunk/httpclient/src/test/java/org/apache/http/localserver/BasicServerTestBase.java
Modified: httpcomponents/httpclient/trunk/httpclient/src/test/java/org/apache/http/impl/conn/tsccm/TestConnPoolByRoute.java
URL: http://svn.apache.org/viewvc/httpcomponents/httpclient/trunk/httpclient/src/test/java/org/apache/http/impl/conn/tsccm/TestConnPoolByRoute.java?rev=1148420&r1=1148419&r2=1148420&view=diff
==============================================================================
--- httpcomponents/httpclient/trunk/httpclient/src/test/java/org/apache/http/impl/conn/tsccm/TestConnPoolByRoute.java (original)
+++ httpcomponents/httpclient/trunk/httpclient/src/test/java/org/apache/http/impl/conn/tsccm/TestConnPoolByRoute.java Tue Jul 19 16:02:45 2011
@@ -27,20 +27,56 @@
package org.apache.http.impl.conn.tsccm;
+import static org.junit.Assert.*;
+
+import java.util.Random;
import java.util.concurrent.TimeUnit;
import org.apache.http.HttpHost;
import org.apache.http.conn.ClientConnectionOperator;
import org.apache.http.conn.ConnectionPoolTimeoutException;
+import org.apache.http.conn.OperatedClientConnection;
import org.apache.http.conn.params.ConnPerRouteBean;
import org.apache.http.conn.routing.HttpRoute;
import org.apache.http.impl.conn.DefaultClientConnectionOperator;
import org.apache.http.localserver.ServerTestBase;
+import org.apache.http.params.BasicHttpParams;
import org.junit.Assert;
+import org.junit.Before;
+import org.junit.Ignore;
import org.junit.Test;
+import org.junit.runner.RunWith;
+import org.mockito.Mock;
+import org.mockito.runners.MockitoJUnitRunner;
+@RunWith(MockitoJUnitRunner.class)
public class TestConnPoolByRoute extends ServerTestBase {
+ private ConnPoolByRoute impl;
+ private HttpRoute route = new HttpRoute(new HttpHost("localhost"));
+
+ @Mock
+ private OperatedClientConnection mockConnection;
+ @Mock
+ private ClientConnectionOperator mockOperator;
+
+ @Before
+ @Override
+ public void setUp() throws Exception {
+ super.setUp();
+ impl = new ConnPoolByRoute(
+ new DefaultClientConnectionOperator(supportedSchemes),
+ new ConnPerRouteBean(), 1, -1, TimeUnit.MILLISECONDS);
+ }
+
+ private void useMockOperator() {
+ impl = new ConnPoolByRoute(
+ mockOperator,
+ new ConnPerRouteBean(), 1, -1, TimeUnit.MILLISECONDS);
+ when(mockOperator.createConnection()).thenReturn(mockConnection);
+ }
+
+
@Test
public void testStatelessConnections() throws Exception {
final HttpHost target = getServerHttp();
@@ -153,5 +189,146 @@ public class TestConnPoolByRoute extends
connPool.shutdown();
}
}
+
+ @Test(expected=IllegalArgumentException.class)
+ public void nullOperatorIsNotAllowed() {
+ new ConnPoolByRoute(null, new ConnPerRouteBean(), 1, -1, TimeUnit.MILLISECONDS);
+ }
+
+ @Test(expected=IllegalArgumentException.class)
+ public void nullConnPerRouteIsNotAllowed() {
+ new ConnPoolByRoute(new DefaultClientConnectionOperator(supportedSchemes),
+ null, 1, -1, TimeUnit.MILLISECONDS);
+ }
+
+ @Test
+ @SuppressWarnings("deprecation")
+ public void deprecatedConstructorIsStillSupported() {
+ new ConnPoolByRoute(new DefaultClientConnectionOperator(supportedSchemes),
+ new BasicHttpParams());
+ }
+
+ @Test
+ public void emptyPoolHasNoConnections() {
+ assertEquals(0, impl.getConnectionsInPool());
+ }
+
+ @Test
+ public void emptyPoolHasNoRouteSpecificConnections() {
+ assertEquals(0, impl.getConnectionsInPool(route));
+ }
+
+ @Test
+ public void abortingPoolEntryRequestEarlyDoesNotCreateConnection() {
+ PoolEntryRequest req = impl.requestPoolEntry(route, new Object());
+ req.abortRequest();
+ assertEquals(0, impl.getConnectionsInPool(route));
+ }
+
+ @Test(expected=IllegalStateException.class)
+ public void cannotAcquireConnectionIfPoolShutdown() throws Exception {
+ impl.shutdown();
+ impl.requestPoolEntry(route, new Object()).getPoolEntry(-1, TimeUnit.MILLISECONDS);
+ }
+
+ @Test
+ public void multipleShutdownsAreOk() {
+ impl.shutdown();
+ impl.shutdown();
+ }
+
+ @Test
+ public void canAcquirePoolEntry() throws Exception {
+ impl.requestPoolEntry(route, new Object()).getPoolEntry(-1, TimeUnit.MILLISECONDS);
+ }
+
+ @Test
+ public void canRetrieveMaxTotalConnections() {
+ int max = (new Random()).nextInt(10) + 2;
+ impl.setMaxTotalConnections(max);
+ assertEquals(max, impl.getMaxTotalConnections());
+ }
+
+ @Test
+ public void closesFreedConnectionsWhenShutdown() throws Exception {
+ useMockOperator();
+ BasicPoolEntry entry = impl.requestPoolEntry(route, new Object()).getPoolEntry(-1, TimeUnit.MILLISECONDS);
+ impl.shutdown();
+ impl.freeEntry(entry, true, Long.MAX_VALUE, TimeUnit.MILLISECONDS);
+ verify(mockConnection, atLeastOnce()).close();
+ }
+
+ @Test
+ public void deleteClosedConnectionsReclaimsPoolSpace() throws Exception {
+ useMockOperator();
+ BasicPoolEntry entry = impl.requestPoolEntry(route, new Object()).getPoolEntry(-1, TimeUnit.MILLISECONDS);
+ impl.freeEntry(entry, true, Long.MAX_VALUE, TimeUnit.MILLISECONDS);
+ assertFalse(impl.freeConnections.isEmpty());
+ when(mockConnection.isOpen()).thenReturn(false);
+ impl.deleteClosedConnections();
+ assertTrue(impl.freeConnections.isEmpty());
+ assertEquals(0, impl.numConnections);
+ }
+
+ @Test
+ public void deleteClosedConnectionsDoesNotReclaimOpenConnections() throws Exception {
+ useMockOperator();
+ BasicPoolEntry entry = impl.requestPoolEntry(route, new Object()).getPoolEntry(-1, TimeUnit.MILLISECONDS);
+ impl.freeEntry(entry, true, Long.MAX_VALUE, TimeUnit.MILLISECONDS);
+ assertFalse(impl.freeConnections.isEmpty());
+ when(mockConnection.isOpen()).thenReturn(true);
+ impl.deleteClosedConnections();
+ assertFalse(impl.freeConnections.isEmpty());
+ assertEquals(1, impl.numConnections);
+ }
+
+ @Test
+ public void closeIdleConnectionsClosesThoseThatHaveTimedOut() throws Exception {
+ useMockOperator();
+ BasicPoolEntry entry = impl.requestPoolEntry(route, new Object()).getPoolEntry(-1, TimeUnit.MILLISECONDS);
+ impl.freeEntry(entry, true, Long.MAX_VALUE, TimeUnit.MILLISECONDS);
+ Thread.sleep(2);
+ impl.closeIdleConnections(1, TimeUnit.MILLISECONDS);
+ verify(mockConnection, atLeastOnce()).close();
+ }
+
+ @Test
+ public void closeIdleConnectionsDoesNotCloseThoseThatHaveNotTimedOut() throws Exception {
+ useMockOperator();
+ BasicPoolEntry entry = impl.requestPoolEntry(route, new Object()).getPoolEntry(-1, TimeUnit.MILLISECONDS);
+ impl.freeEntry(entry, true, Long.MAX_VALUE, TimeUnit.MILLISECONDS);
+ Thread.sleep(1);
+ impl.closeIdleConnections(3, TimeUnit.MILLISECONDS);
+ verify(mockConnection, never()).close();
+ }
+
+ @Test
+ public void closeExpiredConnectionsClosesExpiredOnes() throws Exception {
+ useMockOperator();
+ BasicPoolEntry entry = impl.requestPoolEntry(route, new Object()).getPoolEntry(-1, TimeUnit.MILLISECONDS);
+ impl.freeEntry(entry, true, 1, TimeUnit.MILLISECONDS);
+ Thread.sleep(2);
+ impl.closeExpiredConnections();
+ verify(mockConnection, atLeastOnce()).close();
+ }
+
+ @Test
+ public void closeExpiredConnectionsDoesNotCloseUnexpiredOnes() throws Exception {
+ useMockOperator();
+ BasicPoolEntry entry = impl.requestPoolEntry(route, new Object()).getPoolEntry(-1, TimeUnit.MILLISECONDS);
+ impl.freeEntry(entry, true, 10, TimeUnit.MILLISECONDS);
+ Thread.sleep(1);
+ impl.closeExpiredConnections();
+ verify(mockConnection, never()).close();
+ }
+
+ @Ignore
+ @Test
+ public void closesNonReusableConnections() throws Exception {
+ useMockOperator();
+ BasicPoolEntry entry = impl.requestPoolEntry(route, new Object()).getPoolEntry(-1, TimeUnit.MILLISECONDS);
+ impl.freeEntry(entry, false, 0, TimeUnit.MILLISECONDS);
+ verify(mockConnection, atLeastOnce()).close();
+ }
}
Modified: httpcomponents/httpclient/trunk/httpclient/src/test/java/org/apache/http/localserver/BasicServerTestBase.java
URL: http://svn.apache.org/viewvc/httpcomponents/httpclient/trunk/httpclient/src/test/java/org/apache/http/localserver/BasicServerTestBase.java?rev=1148420&r1=1148419&r2=1148420&view=diff
==============================================================================
--- httpcomponents/httpclient/trunk/httpclient/src/test/java/org/apache/http/localserver/BasicServerTestBase.java (original)
+++ httpcomponents/httpclient/trunk/httpclient/src/test/java/org/apache/http/localserver/BasicServerTestBase.java Tue Jul 19 16:02:45 2011
@@ -31,12 +31,13 @@ import java.net.InetSocketAddress;
import org.apache.http.HttpHost;
import org.junit.After;
+import org.mockito.Mockito;
/**
* Base class for tests using {@link LocalTestServer}. The server will not be started
* per default.
*/
-public abstract class BasicServerTestBase {
+public abstract class BasicServerTestBase extends Mockito {
/** The local server for testing. */
protected LocalTestServer localServer;