From c6355ba41209f18540cf998889cc5a01b9827bf5 Mon Sep 17 00:00:00 2001 From: Francois-Xavier Bonnet Date: Wed, 27 Feb 2013 15:32:09 +0000 Subject: [PATCH] HTTPCLIENT-1328: socketTimeout parameter is ignored git-svn-id: https://svn.apache.org/repos/asf/httpcomponents/httpclient/trunk@1450810 13f79535-47bb-0310-9956-ffa450edef68 --- .../http/impl/execchain/MainClientExec.java | 9 +- .../impl/execchain/TestMainClientExec.java | 129 ++++++++++++++++++ 2 files changed, 133 insertions(+), 5 deletions(-) create mode 100755 httpclient/src/test/java/org/apache/http/impl/execchain/TestMainClientExec.java diff --git a/httpclient/src/main/java/org/apache/http/impl/execchain/MainClientExec.java b/httpclient/src/main/java/org/apache/http/impl/execchain/MainClientExec.java index c4d05b618..3815bb93d 100644 --- a/httpclient/src/main/java/org/apache/http/impl/execchain/MainClientExec.java +++ b/httpclient/src/main/java/org/apache/http/impl/execchain/MainClientExec.java @@ -225,11 +225,10 @@ public class MainClientExec implements ClientExecChain { response = ex.getResponse(); break; } - } else { - final int timeout = config.getSocketTimeout(); - if (timeout >= 0) { - managedConn.setSocketTimeout(timeout); - } + } + final int timeout = config.getSocketTimeout(); + if (timeout >= 0) { + managedConn.setSocketTimeout(timeout); } if (execAware != null && execAware.isAborted()) { diff --git a/httpclient/src/test/java/org/apache/http/impl/execchain/TestMainClientExec.java b/httpclient/src/test/java/org/apache/http/impl/execchain/TestMainClientExec.java new file mode 100755 index 000000000..599f12c21 --- /dev/null +++ b/httpclient/src/test/java/org/apache/http/impl/execchain/TestMainClientExec.java @@ -0,0 +1,129 @@ +/* + * ==================================================================== + * 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 + * . + * + */ +package org.apache.http.impl.execchain; + +import java.util.concurrent.TimeUnit; + +import org.apache.http.ConnectionReuseStrategy; +import org.apache.http.HttpClientConnection; +import org.apache.http.HttpHost; +import org.apache.http.HttpRequest; +import org.apache.http.HttpResponse; +import org.apache.http.client.AuthenticationStrategy; +import org.apache.http.client.UserTokenHandler; +import org.apache.http.client.config.RequestConfig; +import org.apache.http.client.methods.HttpExecutionAware; +import org.apache.http.client.methods.HttpGet; +import org.apache.http.client.methods.HttpRequestWrapper; +import org.apache.http.client.protocol.HttpClientContext; +import org.apache.http.conn.ConnectionKeepAliveStrategy; +import org.apache.http.conn.ConnectionRequest; +import org.apache.http.conn.HttpClientConnectionManager; +import org.apache.http.conn.routing.HttpRoute; +import org.apache.http.protocol.HttpRequestExecutor; +import org.junit.Before; +import org.junit.Test; +import org.mockito.Mockito; + +public class TestMainClientExec { + + private MainClientExec mainClientExec; + private HttpRequestExecutor requestExecutor; + private HttpClientConnectionManager connManager; + private ConnectionReuseStrategy reuseStrategy; + private ConnectionKeepAliveStrategy keepAliveStrategy; + private AuthenticationStrategy targetAuthStrategy; + private AuthenticationStrategy proxyAuthStrategy; + private UserTokenHandler userTokenHandler; + private HttpClientContext context; + private HttpGet request; + private HttpExecutionAware execAware; + private HttpRoute route; + private ConnectionRequest connRequest; + private HttpClientConnection managedConn; + private HttpResponse response; + private RequestConfig config; + + @Before + public void setup() throws Exception { + requestExecutor = Mockito.mock(HttpRequestExecutor.class); + connManager = Mockito.mock(HttpClientConnectionManager.class); + reuseStrategy = Mockito.mock(ConnectionReuseStrategy.class); + keepAliveStrategy = Mockito.mock(ConnectionKeepAliveStrategy.class); + targetAuthStrategy = Mockito.mock(AuthenticationStrategy.class); + proxyAuthStrategy = Mockito.mock(AuthenticationStrategy.class); + userTokenHandler = Mockito.mock(UserTokenHandler.class); + mainClientExec = new MainClientExec(requestExecutor, connManager, reuseStrategy, + keepAliveStrategy, targetAuthStrategy, proxyAuthStrategy, userTokenHandler); + route = new HttpRoute(new HttpHost("foo", 8080)); + context = new HttpClientContext(); + config = RequestConfig.custom().setSocketTimeout(3000).build(); + context.setRequestConfig(config); + execAware = Mockito.mock(HttpExecutionAware.class); + connRequest = Mockito.mock(ConnectionRequest.class); + managedConn = Mockito.mock(HttpClientConnection.class); + response = Mockito.mock(HttpResponse.class); + Mockito.when( + connManager.requestConnection(Mockito.any(HttpRoute.class), Mockito.any(Object.class))) + .thenReturn(connRequest); + Mockito.when(connRequest.get(Mockito.anyLong(), Mockito.any(TimeUnit.class))).thenReturn( + managedConn); + managedConn.setSocketTimeout(Mockito.eq(3000)); + Mockito.when( + requestExecutor.execute(Mockito.any(HttpRequest.class), + Mockito.any(HttpClientConnection.class), Mockito.any(HttpClientContext.class))) + .thenReturn(response); + } + + @Test + public void testSocketTimeoutNewConnection() throws Exception { + request = new HttpGet("http://bar/test"); + request.setConfig(config); + mainClientExec.execute(route, HttpRequestWrapper.wrap(request), context, execAware); + Mockito.verify(managedConn).setSocketTimeout(3000); + } + + @Test + public void testSocketTimeoutExistingConnection() throws Exception { + request = new HttpGet("http://bar/test"); + request.setConfig(config); + Mockito.when(managedConn.isOpen()).thenReturn(true); + mainClientExec.execute(route, HttpRequestWrapper.wrap(request), context, execAware); + Mockito.verify(managedConn).setSocketTimeout(3000); + } + + @Test + public void testSocketTimeoutReset() throws Exception { + request = new HttpGet("http://bar/test"); + config = RequestConfig.custom().build(); + request.setConfig(config); + context.setRequestConfig(config); + Mockito.when(managedConn.isOpen()).thenReturn(true); + mainClientExec.execute(route, HttpRequestWrapper.wrap(request), context, execAware); + Mockito.verify(managedConn).setSocketTimeout(0); + } +}