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 2011/10/06 14:33:12 UTC
svn commit: r1179593 - in /httpcomponents/httpclient/trunk/httpclient/src:
main/java/org/apache/http/impl/conn/ test/java/org/apache/http/impl/client/
test/java/org/apache/http/impl/conn/
Author: olegk
Date: Thu Oct 6 12:33:11 2011
New Revision: 1179593
URL: http://svn.apache.org/viewvc?rev=1179593&view=rev
Log:
HTTPCLIENT-1120: moved test cases for abort handling to TestAbortHandling
Added:
httpcomponents/httpclient/trunk/httpclient/src/test/java/org/apache/http/impl/conn/TestAbortHandling.java
- copied, changed from r1178648, httpcomponents/httpclient/trunk/httpclient/src/test/java/org/apache/http/impl/client/TestDefaultClientRequestDirector.java
Removed:
httpcomponents/httpclient/trunk/httpclient/src/test/java/org/apache/http/impl/client/TestAbortHandling.java
httpcomponents/httpclient/trunk/httpclient/src/test/java/org/apache/http/impl/conn/ClientConnAdapterMockup.java
Modified:
httpcomponents/httpclient/trunk/httpclient/src/main/java/org/apache/http/impl/conn/AbstractClientConnAdapter.java
httpcomponents/httpclient/trunk/httpclient/src/test/java/org/apache/http/impl/client/TestDefaultClientRequestDirector.java
httpcomponents/httpclient/trunk/httpclient/src/test/java/org/apache/http/impl/conn/TestTSCCMNoServer.java
Modified: httpcomponents/httpclient/trunk/httpclient/src/main/java/org/apache/http/impl/conn/AbstractClientConnAdapter.java
URL: http://svn.apache.org/viewvc/httpcomponents/httpclient/trunk/httpclient/src/main/java/org/apache/http/impl/conn/AbstractClientConnAdapter.java?rev=1179593&r1=1179592&r2=1179593&view=diff
==============================================================================
--- httpcomponents/httpclient/trunk/httpclient/src/main/java/org/apache/http/impl/conn/AbstractClientConnAdapter.java (original)
+++ httpcomponents/httpclient/trunk/httpclient/src/main/java/org/apache/http/impl/conn/AbstractClientConnAdapter.java Thu Oct 6 12:33:11 2011
@@ -66,7 +66,10 @@ import org.apache.http.protocol.HttpCont
* expected to tolerate multiple calls to the release method.
*
* @since 4.0
+ *
+ * @deprecated do not use
*/
+@Deprecated
@NotThreadSafe
public abstract class AbstractClientConnAdapter implements ManagedClientConnection, HttpContext {
Modified: httpcomponents/httpclient/trunk/httpclient/src/test/java/org/apache/http/impl/client/TestDefaultClientRequestDirector.java
URL: http://svn.apache.org/viewvc/httpcomponents/httpclient/trunk/httpclient/src/test/java/org/apache/http/impl/client/TestDefaultClientRequestDirector.java?rev=1179593&r1=1179592&r2=1179593&view=diff
==============================================================================
--- httpcomponents/httpclient/trunk/httpclient/src/test/java/org/apache/http/impl/client/TestDefaultClientRequestDirector.java (original)
+++ httpcomponents/httpclient/trunk/httpclient/src/test/java/org/apache/http/impl/client/TestDefaultClientRequestDirector.java Thu Oct 6 12:33:11 2011
@@ -27,10 +27,6 @@ package org.apache.http.impl.client;
import java.io.ByteArrayInputStream;
import java.io.IOException;
-import java.net.ConnectException;
-import java.util.concurrent.CountDownLatch;
-import java.util.concurrent.TimeUnit;
-import java.util.concurrent.atomic.AtomicReference;
import org.apache.http.Header;
import org.apache.http.HttpClientConnection;
@@ -40,33 +36,16 @@ import org.apache.http.HttpRequest;
import org.apache.http.HttpRequestInterceptor;
import org.apache.http.HttpResponse;
import org.apache.http.HttpStatus;
-import org.apache.http.ProtocolVersion;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.HttpRequestRetryHandler;
import org.apache.http.client.NonRepeatableRequestException;
-import org.apache.http.client.methods.AbortableHttpRequest;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.client.params.ClientPNames;
-import org.apache.http.conn.ClientConnectionManager;
-import org.apache.http.conn.ClientConnectionRequest;
-import org.apache.http.conn.ConnectionPoolTimeoutException;
-import org.apache.http.conn.ConnectionReleaseTrigger;
-import org.apache.http.conn.ManagedClientConnection;
-import org.apache.http.conn.routing.HttpRoute;
-import org.apache.http.conn.scheme.PlainSocketFactory;
-import org.apache.http.conn.scheme.Scheme;
-import org.apache.http.conn.scheme.SchemeRegistry;
import org.apache.http.entity.InputStreamEntity;
import org.apache.http.entity.StringEntity;
-import org.apache.http.impl.conn.ClientConnAdapterMockup;
-import org.apache.http.impl.conn.PoolingClientConnectionManager;
import org.apache.http.localserver.BasicServerTestBase;
import org.apache.http.localserver.LocalTestServer;
-import org.apache.http.message.BasicHeader;
-import org.apache.http.mockup.SocketFactoryMockup;
-import org.apache.http.params.BasicHttpParams;
-import org.apache.http.params.HttpParams;
import org.apache.http.protocol.BasicHttpContext;
import org.apache.http.protocol.ExecutionContext;
import org.apache.http.protocol.HttpContext;
@@ -90,441 +69,6 @@ public class TestDefaultClientRequestDir
this.httpclient = new DefaultHttpClient();
}
- /**
- * Tests that if abort is called on an {@link AbortableHttpRequest} while
- * {@link DefaultRequestDirector} is allocating a connection, that the
- * connection is properly aborted.
- */
- @Test
- public void testAbortInAllocate() throws Exception {
- CountDownLatch connLatch = new CountDownLatch(1);
- CountDownLatch awaitLatch = new CountDownLatch(1);
- final ConMan conMan = new ConMan(connLatch, awaitLatch);
- final AtomicReference<Throwable> throwableRef = new AtomicReference<Throwable>();
- final CountDownLatch getLatch = new CountDownLatch(1);
- final DefaultHttpClient client = new DefaultHttpClient(conMan, new BasicHttpParams());
- final HttpContext context = new BasicHttpContext();
- final HttpGet httpget = new HttpGet("http://www.example.com/a");
-
- new Thread(new Runnable() {
- public void run() {
- try {
- client.execute(httpget, context);
- } catch(Throwable t) {
- throwableRef.set(t);
- } finally {
- getLatch.countDown();
- }
- }
- }).start();
-
- Assert.assertTrue("should have tried to get a connection", connLatch.await(1, TimeUnit.SECONDS));
-
- httpget.abort();
-
- Assert.assertTrue("should have finished get request", getLatch.await(1, TimeUnit.SECONDS));
- Assert.assertTrue("should be instanceof IOException, was: " + throwableRef.get(),
- throwableRef.get() instanceof IOException);
- Assert.assertTrue("cause should be InterruptedException, was: " + throwableRef.get().getCause(),
- throwableRef.get().getCause() instanceof InterruptedException);
- }
-
- /**
- * Tests that an abort called after the connection has been retrieved
- * but before a release trigger is set does still abort the request.
- */
- @Test
- public void testAbortAfterAllocateBeforeRequest() throws Exception {
- this.localServer.register("*", new BasicService());
-
- CountDownLatch releaseLatch = new CountDownLatch(1);
- SchemeRegistry registry = new SchemeRegistry();
- registry.register(new Scheme("http", 80, PlainSocketFactory.getSocketFactory()));
-
- PoolingClientConnectionManager conMan = new PoolingClientConnectionManager(registry);
- final AtomicReference<Throwable> throwableRef = new AtomicReference<Throwable>();
- final CountDownLatch getLatch = new CountDownLatch(1);
- final DefaultHttpClient client = new DefaultHttpClient(conMan, new BasicHttpParams());
- final HttpContext context = new BasicHttpContext();
- final HttpGet httpget = new CustomGet("a", releaseLatch);
-
- new Thread(new Runnable() {
- public void run() {
- try {
- client.execute(getServerHttp(), httpget, context);
- } catch(Throwable t) {
- throwableRef.set(t);
- } finally {
- getLatch.countDown();
- }
- }
- }).start();
-
- Thread.sleep(100); // Give it a little time to proceed to release...
-
- httpget.abort();
-
- releaseLatch.countDown();
-
- Assert.assertTrue("should have finished get request", getLatch.await(1, TimeUnit.SECONDS));
- Assert.assertTrue("should be instanceof IOException, was: " + throwableRef.get(),
- throwableRef.get() instanceof IOException);
- }
-
- /**
- * Tests that an abort called completely before execute
- * still aborts the request.
- */
- @Test
- public void testAbortBeforeExecute() throws Exception {
- this.localServer.register("*", new BasicService());
-
- SchemeRegistry registry = new SchemeRegistry();
- registry.register(new Scheme("http", 80, PlainSocketFactory.getSocketFactory()));
-
- PoolingClientConnectionManager conMan = new PoolingClientConnectionManager(registry);
- final AtomicReference<Throwable> throwableRef = new AtomicReference<Throwable>();
- final CountDownLatch getLatch = new CountDownLatch(1);
- final CountDownLatch startLatch = new CountDownLatch(1);
- final DefaultHttpClient client = new DefaultHttpClient(conMan, new BasicHttpParams());
- final HttpContext context = new BasicHttpContext();
- final HttpGet httpget = new HttpGet("a");
-
- new Thread(new Runnable() {
- public void run() {
- try {
- try {
- if(!startLatch.await(1, TimeUnit.SECONDS))
- throw new RuntimeException("Took too long to start!");
- } catch(InterruptedException interrupted) {
- throw new RuntimeException("Never started!", interrupted);
- }
- client.execute(getServerHttp(), httpget, context);
- } catch(Throwable t) {
- throwableRef.set(t);
- } finally {
- getLatch.countDown();
- }
- }
- }).start();
-
- httpget.abort();
- startLatch.countDown();
-
- Assert.assertTrue("should have finished get request", getLatch.await(1, TimeUnit.SECONDS));
- Assert.assertTrue("should be instanceof IOException, was: " + throwableRef.get(),
- throwableRef.get() instanceof IOException);
- }
-
- /**
- * Tests that an abort called after a redirect has found a new host
- * still aborts in the correct place (while trying to get the new
- * host's route, not while doing the subsequent request).
- */
- @Test
- public void testAbortAfterRedirectedRoute() throws Exception {
- final int port = this.localServer.getServiceAddress().getPort();
- this.localServer.register("*", new BasicRedirectService(port));
-
- SchemeRegistry registry = new SchemeRegistry();
- registry.register(new Scheme("http", 80, PlainSocketFactory.getSocketFactory()));
-
- CountDownLatch connLatch = new CountDownLatch(1);
- CountDownLatch awaitLatch = new CountDownLatch(1);
- ConnMan4 conMan = new ConnMan4(registry, connLatch, awaitLatch);
- final AtomicReference<Throwable> throwableRef = new AtomicReference<Throwable>();
- final CountDownLatch getLatch = new CountDownLatch(1);
- final DefaultHttpClient client = new DefaultHttpClient(conMan, new BasicHttpParams());
- final HttpContext context = new BasicHttpContext();
- final HttpGet httpget = new HttpGet("a");
-
- new Thread(new Runnable() {
- public void run() {
- try {
- HttpHost host = new HttpHost("127.0.0.1", port);
- client.execute(host, httpget, context);
- } catch(Throwable t) {
- throwableRef.set(t);
- } finally {
- getLatch.countDown();
- }
- }
- }).start();
-
- Assert.assertTrue("should have tried to get a connection", connLatch.await(1, TimeUnit.SECONDS));
-
- httpget.abort();
-
- Assert.assertTrue("should have finished get request", getLatch.await(1, TimeUnit.SECONDS));
- Assert.assertTrue("should be instanceof IOException, was: " + throwableRef.get(),
- throwableRef.get() instanceof IOException);
- Assert.assertTrue("cause should be InterruptedException, was: " + throwableRef.get().getCause(),
- throwableRef.get().getCause() instanceof InterruptedException);
- }
-
-
- /**
- * Tests that if a socket fails to connect, the allocated connection is
- * properly released back to the connection manager.
- */
- @Test
- public void testSocketConnectFailureReleasesConnection() throws Exception {
- final ConnMan2 conMan = new ConnMan2();
- final DefaultHttpClient client = new DefaultHttpClient(conMan, new BasicHttpParams());
- final HttpContext context = new BasicHttpContext();
- final HttpGet httpget = new HttpGet("http://www.example.com/a");
-
- try {
- client.execute(httpget, context);
- Assert.fail("expected IOException");
- } catch(IOException expected) {}
-
- Assert.assertNotNull(conMan.allocatedConnection);
- Assert.assertSame(conMan.allocatedConnection, conMan.releasedConnection);
- }
-
- private static class BasicService implements HttpRequestHandler {
- public void handle(final HttpRequest request,
- final HttpResponse response,
- final HttpContext context) throws HttpException, IOException {
- response.setStatusCode(200);
- response.setEntity(new StringEntity("Hello World"));
- }
- }
-
- private static class BasicRedirectService implements HttpRequestHandler {
- private int statuscode = HttpStatus.SC_SEE_OTHER;
- private int port;
-
- public BasicRedirectService(int port) {
- this.port = port;
- }
-
- public void handle(final HttpRequest request,
- final HttpResponse response, final HttpContext context)
- throws HttpException, IOException {
- ProtocolVersion ver = request.getRequestLine().getProtocolVersion();
- response.setStatusLine(ver, this.statuscode);
- response.addHeader(new BasicHeader("Location", "http://localhost:"
- + this.port + "/newlocation/"));
- response.addHeader(new BasicHeader("Connection", "close"));
- }
- }
-
- private static class ConnMan4 extends PoolingClientConnectionManager {
- private final CountDownLatch connLatch;
- private final CountDownLatch awaitLatch;
-
- public ConnMan4(SchemeRegistry schreg,
- CountDownLatch connLatch, CountDownLatch awaitLatch) {
- super(schreg);
- this.connLatch = connLatch;
- this.awaitLatch = awaitLatch;
- }
-
- @Override
- public ClientConnectionRequest requestConnection(HttpRoute route, Object state) {
- // If this is the redirect route, stub the return value
- // so-as to pretend the host is waiting on a slot...
- if(route.getTargetHost().getHostName().equals("localhost")) {
- final Thread currentThread = Thread.currentThread();
-
- return new ClientConnectionRequest() {
-
- public void abortRequest() {
- currentThread.interrupt();
- }
-
- public ManagedClientConnection getConnection(
- long timeout, TimeUnit tunit)
- throws InterruptedException,
- ConnectionPoolTimeoutException {
- connLatch.countDown(); // notify waiter that we're getting a connection
-
- // zero usually means sleep forever, but CountDownLatch doesn't interpret it that way.
- if(timeout == 0)
- timeout = Integer.MAX_VALUE;
-
- if(!awaitLatch.await(timeout, tunit))
- throw new ConnectionPoolTimeoutException();
-
- return new ClientConnAdapterMockup(ConnMan4.this);
- }
- };
- } else {
- return super.requestConnection(route, state);
- }
- }
- }
-
-
- static class ConnMan2 implements ClientConnectionManager {
-
- private ManagedClientConnection allocatedConnection;
- private ManagedClientConnection releasedConnection;
-
- public ConnMan2() {
- }
-
- public void closeIdleConnections(long idletime, TimeUnit tunit) {
- throw new UnsupportedOperationException("just a mockup");
- }
-
- public void closeExpiredConnections() {
- throw new UnsupportedOperationException("just a mockup");
- }
-
- public ManagedClientConnection getConnection(HttpRoute route) {
- throw new UnsupportedOperationException("just a mockup");
- }
-
- public ManagedClientConnection getConnection(HttpRoute route,
- long timeout, TimeUnit tunit) {
- throw new UnsupportedOperationException("just a mockup");
- }
-
- public ClientConnectionRequest requestConnection(
- final HttpRoute route,
- final Object state) {
-
- return new ClientConnectionRequest() {
-
- public void abortRequest() {
- throw new UnsupportedOperationException("just a mockup");
- }
-
- public ManagedClientConnection getConnection(
- long timeout, TimeUnit unit)
- throws InterruptedException,
- ConnectionPoolTimeoutException {
- allocatedConnection = new ClientConnAdapterMockup(ConnMan2.this) {
- @Override
- public void open(HttpRoute route, HttpContext context,
- HttpParams params) throws IOException {
- throw new ConnectException();
- }
- };
- return allocatedConnection;
- }
- };
- }
-
- public HttpParams getParams() {
- throw new UnsupportedOperationException("just a mockup");
- }
-
- public SchemeRegistry getSchemeRegistry() {
- SchemeRegistry registry = new SchemeRegistry();
- registry.register(new Scheme("http", 80, new SocketFactoryMockup(null)));
- return registry;
- }
-
- public void releaseConnection(ManagedClientConnection conn, long validDuration, TimeUnit timeUnit) {
- this.releasedConnection = conn;
- }
-
- public void shutdown() {
- throw new UnsupportedOperationException("just a mockup");
- }
- }
-
- static class ConMan implements ClientConnectionManager {
- private final CountDownLatch connLatch;
- private final CountDownLatch awaitLatch;
-
- public ConMan(CountDownLatch connLatch, CountDownLatch awaitLatch) {
- this.connLatch = connLatch;
- this.awaitLatch = awaitLatch;
- }
-
- public void closeIdleConnections(long idletime, TimeUnit tunit) {
- throw new UnsupportedOperationException("just a mockup");
- }
-
- public void closeExpiredConnections() {
- throw new UnsupportedOperationException("just a mockup");
- }
-
- public ManagedClientConnection getConnection(HttpRoute route) {
- throw new UnsupportedOperationException("just a mockup");
- }
-
- public ManagedClientConnection getConnection(HttpRoute route,
- long timeout, TimeUnit tunit) {
- throw new UnsupportedOperationException("just a mockup");
- }
-
- public ClientConnectionRequest requestConnection(
- final HttpRoute route,
- final Object state) {
-
- final Thread currentThread = Thread.currentThread();
-
- return new ClientConnectionRequest() {
-
- public void abortRequest() {
- currentThread.interrupt();
- }
-
- public ManagedClientConnection getConnection(
- long timeout, TimeUnit tunit)
- throws InterruptedException,
- ConnectionPoolTimeoutException {
- connLatch.countDown(); // notify waiter that we're getting a connection
-
- // zero usually means sleep forever, but CountDownLatch doesn't interpret it that way.
- if(timeout == 0)
- timeout = Integer.MAX_VALUE;
-
- if(!awaitLatch.await(timeout, tunit))
- throw new ConnectionPoolTimeoutException();
-
- return new ClientConnAdapterMockup(ConMan.this);
- }
- };
- }
-
- public HttpParams getParams() {
- throw new UnsupportedOperationException("just a mockup");
- }
-
- public SchemeRegistry getSchemeRegistry() {
- SchemeRegistry registry = new SchemeRegistry();
- registry.register(new Scheme("http", 80, new SocketFactoryMockup(null)));
- return registry;
- }
-
- public void releaseConnection(ManagedClientConnection conn, long validDuration, TimeUnit timeUnit) {
- throw new UnsupportedOperationException("just a mockup");
- }
-
- public void shutdown() {
- throw new UnsupportedOperationException("just a mockup");
- }
- }
-
- private static class CustomGet extends HttpGet {
- private final CountDownLatch releaseTriggerLatch;
-
- public CustomGet(String uri, CountDownLatch releaseTriggerLatch) {
- super(uri);
- this.releaseTriggerLatch = releaseTriggerLatch;
- }
-
- @Override
- public void setReleaseTrigger(ConnectionReleaseTrigger releaseTrigger) throws IOException {
- try {
- if(!releaseTriggerLatch.await(1, TimeUnit.SECONDS))
- throw new RuntimeException("Waited too long...");
- } catch(InterruptedException ie) {
- throw new RuntimeException(ie);
- }
-
- super.setReleaseTrigger(releaseTrigger);
- }
-
- }
-
private static class SimpleService implements HttpRequestHandler {
public SimpleService() {
Copied: httpcomponents/httpclient/trunk/httpclient/src/test/java/org/apache/http/impl/conn/TestAbortHandling.java (from r1178648, httpcomponents/httpclient/trunk/httpclient/src/test/java/org/apache/http/impl/client/TestDefaultClientRequestDirector.java)
URL: http://svn.apache.org/viewvc/httpcomponents/httpclient/trunk/httpclient/src/test/java/org/apache/http/impl/conn/TestAbortHandling.java?p2=httpcomponents/httpclient/trunk/httpclient/src/test/java/org/apache/http/impl/conn/TestAbortHandling.java&p1=httpcomponents/httpclient/trunk/httpclient/src/test/java/org/apache/http/impl/client/TestDefaultClientRequestDirector.java&r1=1178648&r2=1179593&rev=1179593&view=diff
==============================================================================
--- httpcomponents/httpclient/trunk/httpclient/src/test/java/org/apache/http/impl/client/TestDefaultClientRequestDirector.java (original)
+++ httpcomponents/httpclient/trunk/httpclient/src/test/java/org/apache/http/impl/conn/TestAbortHandling.java Thu Oct 6 12:33:11 2011
@@ -21,33 +21,25 @@
* 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.client;
+package org.apache.http.impl.conn;
-import java.io.ByteArrayInputStream;
import java.io.IOException;
import java.net.ConnectException;
import java.util.concurrent.CountDownLatch;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.atomic.AtomicReference;
-import org.apache.http.Header;
-import org.apache.http.HttpClientConnection;
import org.apache.http.HttpException;
import org.apache.http.HttpHost;
import org.apache.http.HttpRequest;
-import org.apache.http.HttpRequestInterceptor;
import org.apache.http.HttpResponse;
import org.apache.http.HttpStatus;
import org.apache.http.ProtocolVersion;
-import org.apache.http.client.ClientProtocolException;
-import org.apache.http.client.HttpRequestRetryHandler;
-import org.apache.http.client.NonRepeatableRequestException;
import org.apache.http.client.methods.AbortableHttpRequest;
import org.apache.http.client.methods.HttpGet;
-import org.apache.http.client.methods.HttpPost;
-import org.apache.http.client.params.ClientPNames;
import org.apache.http.conn.ClientConnectionManager;
import org.apache.http.conn.ClientConnectionRequest;
import org.apache.http.conn.ConnectionPoolTimeoutException;
@@ -57,10 +49,10 @@ import org.apache.http.conn.routing.Http
import org.apache.http.conn.scheme.PlainSocketFactory;
import org.apache.http.conn.scheme.Scheme;
import org.apache.http.conn.scheme.SchemeRegistry;
-import org.apache.http.entity.InputStreamEntity;
import org.apache.http.entity.StringEntity;
-import org.apache.http.impl.conn.ClientConnAdapterMockup;
-import org.apache.http.impl.conn.PoolingClientConnectionManager;
+import org.apache.http.impl.client.DefaultHttpClient;
+import org.apache.http.impl.client.DefaultRequestDirector;
+import org.apache.http.impl.client.RequestWrapper;
import org.apache.http.localserver.BasicServerTestBase;
import org.apache.http.localserver.LocalTestServer;
import org.apache.http.message.BasicHeader;
@@ -70,17 +62,16 @@ import org.apache.http.params.HttpParams
import org.apache.http.protocol.BasicHttpContext;
import org.apache.http.protocol.ExecutionContext;
import org.apache.http.protocol.HttpContext;
-import org.apache.http.protocol.HttpRequestExecutor;
import org.apache.http.protocol.HttpRequestHandler;
-import org.apache.http.util.EntityUtils;
import org.junit.Assert;
import org.junit.Before;
import org.junit.Test;
+import org.mockito.Mockito;
/**
- * Unit tests for {@link DefaultRequestDirector}
+ * Tests for Abort handling.
*/
-public class TestDefaultClientRequestDirector extends BasicServerTestBase {
+public class TestAbortHandling extends BasicServerTestBase {
@Before
public void setUp() throws Exception {
@@ -90,6 +81,53 @@ public class TestDefaultClientRequestDir
this.httpclient = new DefaultHttpClient();
}
+ @Test
+ public void testAbortRetry_HTTPCLIENT_1120() throws Exception {
+ int port = this.localServer.getServiceAddress().getPort();
+ final CountDownLatch wait = new CountDownLatch(1);
+
+ this.localServer.register("*", new HttpRequestHandler(){
+ public void handle(HttpRequest request, HttpResponse response,
+ HttpContext context) throws HttpException, IOException {
+ try {
+ wait.countDown(); // trigger abort
+ Thread.sleep(2000); // allow time for abort to happen
+ response.setStatusCode(HttpStatus.SC_OK);
+ StringEntity entity = new StringEntity("Whatever");
+ response.setEntity(entity);
+ } catch (Exception e) {
+ response.setStatusCode(HttpStatus.SC_REQUEST_TIMEOUT);
+ }
+ }});
+
+ String s = "http://localhost:" + port + "/path";
+ final HttpGet httpget = new HttpGet(s);
+
+ Thread t = new Thread() {
+ @Override
+ public void run(){
+ try {
+ wait.await();
+ } catch (InterruptedException e) {
+ }
+ httpget.abort();
+ }
+ };
+
+ t.start();
+
+ HttpContext context = new BasicHttpContext();
+ try {
+ this.httpclient.execute(getServerHttp(), httpget, context);
+ } catch (IllegalStateException e) {
+ } catch (IOException e) {
+ }
+
+ HttpRequest reqWrapper = (HttpRequest) context.getAttribute(ExecutionContext.HTTP_REQUEST);
+ Assert.assertNotNull("Request should exist",reqWrapper);
+ Assert.assertEquals(1,((RequestWrapper) reqWrapper).getExecCount());
+ }
+
/**
* Tests that if abort is called on an {@link AbortableHttpRequest} while
* {@link DefaultRequestDirector} is allocating a connection, that the
@@ -269,8 +307,23 @@ public class TestDefaultClientRequestDir
*/
@Test
public void testSocketConnectFailureReleasesConnection() throws Exception {
- final ConnMan2 conMan = new ConnMan2();
- final DefaultHttpClient client = new DefaultHttpClient(conMan, new BasicHttpParams());
+ ManagedClientConnection conn = Mockito.mock(ManagedClientConnection.class);
+ Mockito.doThrow(new ConnectException()).when(conn).open(
+ Mockito.any(HttpRoute.class),
+ Mockito.any(HttpContext.class),
+ Mockito.any(HttpParams.class));
+ ClientConnectionRequest connrequest = Mockito.mock(ClientConnectionRequest.class);
+ Mockito.when(connrequest.getConnection(
+ Mockito.anyInt(), Mockito.any(TimeUnit.class))).thenReturn(conn);
+ ClientConnectionManager connmgr = Mockito.mock(ClientConnectionManager.class);
+
+ SchemeRegistry schemeRegistry = SchemeRegistryFactory.createDefault();
+
+ Mockito.when(connmgr.requestConnection(
+ Mockito.any(HttpRoute.class), Mockito.any())).thenReturn(connrequest);
+ Mockito.when(connmgr.getSchemeRegistry()).thenReturn(schemeRegistry);
+
+ final DefaultHttpClient client = new DefaultHttpClient(connmgr, new BasicHttpParams());
final HttpContext context = new BasicHttpContext();
final HttpGet httpget = new HttpGet("http://www.example.com/a");
@@ -279,8 +332,7 @@ public class TestDefaultClientRequestDir
Assert.fail("expected IOException");
} catch(IOException expected) {}
- Assert.assertNotNull(conMan.allocatedConnection);
- Assert.assertSame(conMan.allocatedConnection, conMan.releasedConnection);
+ Mockito.verify(conn).abortConnection();
}
private static class BasicService implements HttpRequestHandler {
@@ -348,7 +400,7 @@ public class TestDefaultClientRequestDir
if(!awaitLatch.await(timeout, tunit))
throw new ConnectionPoolTimeoutException();
- return new ClientConnAdapterMockup(ConnMan4.this);
+ return Mockito.mock(ManagedClientConnection.class);
}
};
} else {
@@ -358,76 +410,6 @@ public class TestDefaultClientRequestDir
}
- static class ConnMan2 implements ClientConnectionManager {
-
- private ManagedClientConnection allocatedConnection;
- private ManagedClientConnection releasedConnection;
-
- public ConnMan2() {
- }
-
- public void closeIdleConnections(long idletime, TimeUnit tunit) {
- throw new UnsupportedOperationException("just a mockup");
- }
-
- public void closeExpiredConnections() {
- throw new UnsupportedOperationException("just a mockup");
- }
-
- public ManagedClientConnection getConnection(HttpRoute route) {
- throw new UnsupportedOperationException("just a mockup");
- }
-
- public ManagedClientConnection getConnection(HttpRoute route,
- long timeout, TimeUnit tunit) {
- throw new UnsupportedOperationException("just a mockup");
- }
-
- public ClientConnectionRequest requestConnection(
- final HttpRoute route,
- final Object state) {
-
- return new ClientConnectionRequest() {
-
- public void abortRequest() {
- throw new UnsupportedOperationException("just a mockup");
- }
-
- public ManagedClientConnection getConnection(
- long timeout, TimeUnit unit)
- throws InterruptedException,
- ConnectionPoolTimeoutException {
- allocatedConnection = new ClientConnAdapterMockup(ConnMan2.this) {
- @Override
- public void open(HttpRoute route, HttpContext context,
- HttpParams params) throws IOException {
- throw new ConnectException();
- }
- };
- return allocatedConnection;
- }
- };
- }
-
- public HttpParams getParams() {
- throw new UnsupportedOperationException("just a mockup");
- }
-
- public SchemeRegistry getSchemeRegistry() {
- SchemeRegistry registry = new SchemeRegistry();
- registry.register(new Scheme("http", 80, new SocketFactoryMockup(null)));
- return registry;
- }
-
- public void releaseConnection(ManagedClientConnection conn, long validDuration, TimeUnit timeUnit) {
- this.releasedConnection = conn;
- }
-
- public void shutdown() {
- throw new UnsupportedOperationException("just a mockup");
- }
- }
-
static class ConMan implements ClientConnectionManager {
private final CountDownLatch connLatch;
private final CountDownLatch awaitLatch;
@@ -479,7 +461,7 @@ public class TestDefaultClientRequestDir
if(!awaitLatch.await(timeout, tunit))
throw new ConnectionPoolTimeoutException();
- return new ClientConnAdapterMockup(ConMan.this);
+ return Mockito.mock(ManagedClientConnection.class);
}
};
}
@@ -525,269 +507,4 @@ public class TestDefaultClientRequestDir
}
- private static class SimpleService implements HttpRequestHandler {
-
- public SimpleService() {
- super();
- }
-
- public void handle(
- final HttpRequest request,
- final HttpResponse response,
- final HttpContext context) throws HttpException, IOException {
- response.setStatusCode(HttpStatus.SC_OK);
- StringEntity entity = new StringEntity("Whatever");
- response.setEntity(entity);
- }
- }
-
- @Test
- public void testDefaultHostAtClientLevel() throws Exception {
- int port = this.localServer.getServiceAddress().getPort();
- this.localServer.register("*", new SimpleService());
-
- HttpHost target = new HttpHost("localhost", port);
-
- this.httpclient.getParams().setParameter(ClientPNames.DEFAULT_HOST, target);
-
- String s = "/path";
- HttpGet httpget = new HttpGet(s);
-
- HttpResponse response = this.httpclient.execute(httpget);
- EntityUtils.consume(response.getEntity());
- Assert.assertEquals(HttpStatus.SC_OK, response.getStatusLine().getStatusCode());
- }
-
- @Test
- public void testDefaultHostHeader() throws Exception {
- int port = this.localServer.getServiceAddress().getPort();
- String hostname = getServerHttp().getHostName();
- this.localServer.register("*", new SimpleService());
-
- HttpContext context = new BasicHttpContext();
-
- String s = "http://localhost:" + port;
- HttpGet httpget = new HttpGet(s);
-
- HttpResponse response = this.httpclient.execute(getServerHttp(), httpget, context);
- EntityUtils.consume(response.getEntity());
-
- HttpRequest reqWrapper = (HttpRequest) context.getAttribute(
- ExecutionContext.HTTP_REQUEST);
-
- Assert.assertEquals(HttpStatus.SC_OK, response.getStatusLine().getStatusCode());
- // Check that Host header is generated as expected
- Header[] headers = reqWrapper.getHeaders("host");
- Assert.assertNotNull(headers);
- Assert.assertEquals(1, headers.length);
- Assert.assertEquals(hostname + ":" + port, headers[0].getValue());
- }
-
- @Test
- // HTTPCLIENT-1092
- public void testVirtualHostHeader() throws Exception {
- int port = this.localServer.getServiceAddress().getPort();
- this.localServer.register("*", new SimpleService());
-
- HttpContext context = new BasicHttpContext();
-
- String s = "http://localhost:" + port;
- HttpGet httpget = new HttpGet(s);
-
- String virtHost = "virtual";
- httpget.getParams().setParameter(ClientPNames.VIRTUAL_HOST, new HttpHost(virtHost, port));
- HttpResponse response = this.httpclient.execute(getServerHttp(), httpget, context);
- EntityUtils.consume(response.getEntity());
-
- HttpRequest reqWrapper = (HttpRequest) context.getAttribute(
- ExecutionContext.HTTP_REQUEST);
-
- Assert.assertEquals(HttpStatus.SC_OK, response.getStatusLine().getStatusCode());
- // Check that Host header is generated as expected
- Header[] headers = reqWrapper.getHeaders("host");
- Assert.assertNotNull(headers);
- Assert.assertEquals(1, headers.length);
- Assert.assertEquals(virtHost+":"+port,headers[0].getValue());
- }
-
- @Test
- // Test that virtual port is propagated if provided
- // This is not expected to be used much, if ever
- // HTTPCLIENT-1092
- public void testVirtualHostPortHeader() throws Exception {
- int port = this.localServer.getServiceAddress().getPort();
- this.localServer.register("*", new SimpleService());
-
- HttpContext context = new BasicHttpContext();
-
- String s = "http://localhost:" + port;
- HttpGet httpget = new HttpGet(s);
-
- String virtHost = "virtual";
- int virtPort = 9876;
- httpget.getParams().setParameter(ClientPNames.VIRTUAL_HOST, new HttpHost(virtHost, virtPort));
- HttpResponse response = this.httpclient.execute(getServerHttp(), httpget, context);
- EntityUtils.consume(response.getEntity());
-
- HttpRequest reqWrapper = (HttpRequest) context.getAttribute(
- ExecutionContext.HTTP_REQUEST);
-
- Assert.assertEquals(HttpStatus.SC_OK, response.getStatusLine().getStatusCode());
- // Check that Host header is generated as expected
- Header[] headers = reqWrapper.getHeaders("host");
- Assert.assertNotNull(headers);
- Assert.assertEquals(1, headers.length);
- Assert.assertEquals(virtHost+":"+virtPort,headers[0].getValue());
- }
-
- @Test
- public void testDefaultHostAtRequestLevel() throws Exception {
- int port = this.localServer.getServiceAddress().getPort();
- this.localServer.register("*", new SimpleService());
-
- HttpHost target1 = new HttpHost("whatever", 80);
- HttpHost target2 = new HttpHost("localhost", port);
-
- this.httpclient.getParams().setParameter(ClientPNames.DEFAULT_HOST, target1);
-
- String s = "/path";
- HttpGet httpget = new HttpGet(s);
- httpget.getParams().setParameter(ClientPNames.DEFAULT_HOST, target2);
-
- HttpResponse response = this.httpclient.execute(httpget);
- EntityUtils.consume(response.getEntity());
- Assert.assertEquals(HttpStatus.SC_OK, response.getStatusLine().getStatusCode());
- }
-
- private static class FaultyHttpRequestExecutor extends HttpRequestExecutor {
-
- private static final String MARKER = "marker";
-
- private final String failureMsg;
-
- public FaultyHttpRequestExecutor(String failureMsg) {
- this.failureMsg = failureMsg;
- }
-
- @Override
- public HttpResponse execute(
- final HttpRequest request,
- final HttpClientConnection conn,
- final HttpContext context) throws IOException, HttpException {
-
- HttpResponse response = super.execute(request, conn, context);
- Object marker = context.getAttribute(MARKER);
- if (marker == null) {
- context.setAttribute(MARKER, Boolean.TRUE);
- throw new IOException(failureMsg);
- }
- return response;
- }
-
- }
-
- private static class FaultyHttpClient extends DefaultHttpClient {
-
- private final String failureMsg;
-
- public FaultyHttpClient() {
- this("Oppsie");
- }
-
- public FaultyHttpClient(String failureMsg) {
- this.failureMsg = failureMsg;
- }
-
- @Override
- protected HttpRequestExecutor createRequestExecutor() {
- return new FaultyHttpRequestExecutor(failureMsg);
- }
-
- }
-
- @Test
- public void testAutoGeneratedHeaders() throws Exception {
- int port = this.localServer.getServiceAddress().getPort();
- this.localServer.register("*", new SimpleService());
-
- FaultyHttpClient client = new FaultyHttpClient();
-
- client.addRequestInterceptor(new HttpRequestInterceptor() {
-
- public void process(
- final HttpRequest request,
- final HttpContext context) throws HttpException, IOException {
- request.addHeader("my-header", "stuff");
- }
-
- }) ;
-
- client.setHttpRequestRetryHandler(new HttpRequestRetryHandler() {
-
- public boolean retryRequest(
- final IOException exception,
- int executionCount,
- final HttpContext context) {
- return true;
- }
-
- });
-
- HttpContext context = new BasicHttpContext();
-
- String s = "http://localhost:" + port;
- HttpGet httpget = new HttpGet(s);
-
- HttpResponse response = client.execute(getServerHttp(), httpget, context);
- EntityUtils.consume(response.getEntity());
-
- HttpRequest reqWrapper = (HttpRequest) context.getAttribute(
- ExecutionContext.HTTP_REQUEST);
-
- Assert.assertEquals(HttpStatus.SC_OK, response.getStatusLine().getStatusCode());
-
- Assert.assertTrue(reqWrapper instanceof RequestWrapper);
- Header[] myheaders = reqWrapper.getHeaders("my-header");
- Assert.assertNotNull(myheaders);
- Assert.assertEquals(1, myheaders.length);
- }
-
- @Test(expected=ClientProtocolException.class)
- public void testNonRepeatableEntity() throws Exception {
- int port = this.localServer.getServiceAddress().getPort();
- this.localServer.register("*", new SimpleService());
-
- String failureMsg = "a message showing that this failed";
-
- FaultyHttpClient client = new FaultyHttpClient(failureMsg);
- client.setHttpRequestRetryHandler(new HttpRequestRetryHandler() {
-
- public boolean retryRequest(
- final IOException exception,
- int executionCount,
- final HttpContext context) {
- return true;
- }
-
- });
- HttpContext context = new BasicHttpContext();
-
- String s = "http://localhost:" + port;
- HttpPost httppost = new HttpPost(s);
- httppost.setEntity(new InputStreamEntity(
- new ByteArrayInputStream(
- new byte[] { 1, 2, 3, 4, 5, 6, 7, 8, 9 } ),
- -1));
-
- try {
- client.execute(getServerHttp(), httppost, context);
- } catch (ClientProtocolException ex) {
- Assert.assertTrue(ex.getCause() instanceof NonRepeatableRequestException);
- NonRepeatableRequestException nonRepeat = (NonRepeatableRequestException)ex.getCause();
- Assert.assertTrue(nonRepeat.getCause() instanceof IOException);
- Assert.assertEquals(failureMsg, nonRepeat.getCause().getMessage());
- throw ex;
- }
- }
-
}
Modified: httpcomponents/httpclient/trunk/httpclient/src/test/java/org/apache/http/impl/conn/TestTSCCMNoServer.java
URL: http://svn.apache.org/viewvc/httpcomponents/httpclient/trunk/httpclient/src/test/java/org/apache/http/impl/conn/TestTSCCMNoServer.java?rev=1179593&r1=1179592&r2=1179593&view=diff
==============================================================================
--- httpcomponents/httpclient/trunk/httpclient/src/test/java/org/apache/http/impl/conn/TestTSCCMNoServer.java (original)
+++ httpcomponents/httpclient/trunk/httpclient/src/test/java/org/apache/http/impl/conn/TestTSCCMNoServer.java Thu Oct 6 12:33:11 2011
@@ -41,6 +41,7 @@ import org.apache.http.params.HttpParams
import org.apache.http.params.HttpProtocolParams;
import org.junit.Assert;
import org.junit.Test;
+import org.mockito.Mockito;
/**
* Tests for <code>ThreadSafeClientConnManager</code> that do not require
@@ -211,7 +212,8 @@ public class TestTSCCMNoServer {
// expected
}
try {
- mgr.releaseConnection(new ClientConnAdapterMockup(null), -1, null);
+ ManagedClientConnection conn = Mockito.mock(ManagedClientConnection.class);
+ mgr.releaseConnection(conn, -1, null);
Assert.fail("foreign connection adapter not detected");
} catch (IllegalArgumentException iax) {
// expected