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 [EMAIL PROTECTED] 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: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]