Return-Path: Delivered-To: apmail-jakarta-commons-dev-archive@apache.org Received: (qmail 10648 invoked from network); 27 Feb 2003 13:38:30 -0000 Received: from exchange.sun.com (192.18.33.10) by daedalus.apache.org with SMTP; 27 Feb 2003 13:38:30 -0000 Received: (qmail 29173 invoked by uid 97); 27 Feb 2003 13:40:05 -0000 Delivered-To: qmlist-jakarta-archive-commons-dev@nagoya.betaversion.org Received: (qmail 29166 invoked from network); 27 Feb 2003 13:40:05 -0000 Received: from daedalus.apache.org (HELO apache.org) (208.185.179.12) by nagoya.betaversion.org with SMTP; 27 Feb 2003 13:40:05 -0000 Received: (qmail 10347 invoked by uid 500); 27 Feb 2003 13:38:26 -0000 Mailing-List: contact commons-dev-help@jakarta.apache.org; run by ezmlm Precedence: bulk List-Unsubscribe: List-Subscribe: List-Help: List-Post: List-Id: "Jakarta Commons Developers List" Reply-To: "Jakarta Commons Developers List" Delivered-To: mailing list commons-dev@jakarta.apache.org Received: (qmail 10335 invoked by uid 500); 27 Feb 2003 13:38:26 -0000 Received: (qmail 10328 invoked from network); 27 Feb 2003 13:38:26 -0000 Received: from icarus.apache.org (208.185.179.13) by daedalus.apache.org with SMTP; 27 Feb 2003 13:38:26 -0000 Received: (qmail 87865 invoked by uid 1633); 27 Feb 2003 13:38:25 -0000 Date: 27 Feb 2003 13:38:25 -0000 Message-ID: <20030227133825.87863.qmail@icarus.apache.org> From: mbecke@apache.org To: jakarta-commons-cvs@apache.org Subject: cvs commit: jakarta-commons/httpclient/src/java/org/apache/commons/httpclient HttpConnection.java X-Spam-Rating: daedalus.apache.org 1.6.2 0/1000/N X-Spam-Rating: daedalus.apache.org 1.6.2 0/1000/N mbecke 2003/02/27 05:38:24 Modified: httpclient/src/test/org/apache/commons/httpclient TestHttpConnection.java httpclient/src/java/org/apache/commons/httpclient HttpConnection.java Log: Connection timeout test now works correctly. PR: 16419 Reviewed by: Jeff Dever, Oleg Kalnichevski and Adrian Sutton Revision Changes Path 1.7 +57 -3 jakarta-commons/httpclient/src/test/org/apache/commons/httpclient/TestHttpConnection.java Index: TestHttpConnection.java =================================================================== RCS file: /home/cvs/jakarta-commons/httpclient/src/test/org/apache/commons/httpclient/TestHttpConnection.java,v retrieving revision 1.6 retrieving revision 1.7 diff -u -r1.6 -r1.7 --- TestHttpConnection.java 23 Jan 2003 22:48:27 -0000 1.6 +++ TestHttpConnection.java 27 Feb 2003 13:38:23 -0000 1.7 @@ -65,10 +65,16 @@ import java.io.IOException; import java.io.InputStream; import java.io.OutputStream; +import java.net.InetAddress; +import java.net.Socket; +import java.net.UnknownHostException; import junit.framework.Test; import junit.framework.TestSuite; +import org.apache.commons.httpclient.protocol.Protocol; +import org.apache.commons.httpclient.protocol.ProtocolSocketFactory; + /** * * Unit tests for {@link HttpConnection}. @@ -108,10 +114,19 @@ } public void testConnTimeout() { - HttpConnection conn = new HttpConnection(host,port); + + // create a custom protocol that will delay for 500 milliseconds + Protocol testProtocol = new Protocol( + "timeout", + new DelayedProtocolSocketFactory( + 500, + Protocol.getProtocol("http").getSocketFactory() + ), + port + ); + + HttpConnection conn = new HttpConnection(host, port, testProtocol); // 1 ms is short enough to make this fail - // (not always. we should probably add some sort of delayServlet - // the test webapp to force this to fail - rlw) conn.setConnectionTimeout(1); try { conn.open(); @@ -158,6 +173,45 @@ fail("getResponseInputStream() did not throw the expected exception"); } + } + + /** + * A ProtocolSocketFactory that delays before creating a socket. + */ + class DelayedProtocolSocketFactory implements ProtocolSocketFactory { + + private int delay; + private ProtocolSocketFactory realFactory; + + public DelayedProtocolSocketFactory(int delay, ProtocolSocketFactory realFactory) { + this.delay = delay; + this.realFactory = realFactory; + } + + public Socket createSocket( + String host, + int port, + InetAddress clientHost, + int clientPort + ) throws IOException, UnknownHostException { + + synchronized (this) { + try { + this.wait(delay); + } catch (InterruptedException e) {} + } + return realFactory.createSocket(host, port, clientHost, clientPort); + } + + public Socket createSocket(String host, int port) + throws IOException, UnknownHostException { + synchronized (this) { + try { + this.wait(delay); + } catch (InterruptedException e) {} + } + return realFactory.createSocket(host, port); + } } 1.47 +9 -8 jakarta-commons/httpclient/src/java/org/apache/commons/httpclient/HttpConnection.java Index: HttpConnection.java =================================================================== RCS file: /home/cvs/jakarta-commons/httpclient/src/java/org/apache/commons/httpclient/HttpConnection.java,v retrieving revision 1.46 retrieving revision 1.47 diff -u -r1.46 -r1.47 --- HttpConnection.java 26 Feb 2003 15:08:27 -0000 1.46 +++ HttpConnection.java 27 Feb 2003 13:38:24 -0000 1.47 @@ -487,11 +487,12 @@ usingSecureSocket = isSecure() && !isProxied(); + // use the protocol's socket factory unless this is a secure + // proxied connection final ProtocolSocketFactory socketFactory = - (isSecure() - && !isProxied() - ? protocolInUse.getSocketFactory() - : new DefaultProtocolSocketFactory()); + (isSecure() && isProxied() + ? new DefaultProtocolSocketFactory() + : protocolInUse.getSocketFactory()); if (connectTimeout == 0) { socket = socketFactory.createSocket(host, port); --------------------------------------------------------------------- To unsubscribe, e-mail: commons-dev-unsubscribe@jakarta.apache.org For additional commands, e-mail: commons-dev-help@jakarta.apache.org