001: /*
002: * $Header: /home/jerenkrantz/tmp/commons/commons-convert/cvs/home/cvs/jakarta-commons//httpclient/src/test/org/apache/commons/httpclient/TestIdleConnectionTimeout.java,v 1.5 2004/11/07 12:31:42 olegk Exp $
003: * $Revision: 480424 $
004: * $Date: 2006-11-29 06:56:49 +0100 (Wed, 29 Nov 2006) $
005: *
006: * ====================================================================
007: *
008: * Licensed to the Apache Software Foundation (ASF) under one or more
009: * contributor license agreements. See the NOTICE file distributed with
010: * this work for additional information regarding copyright ownership.
011: * The ASF licenses this file to You under the Apache License, Version 2.0
012: * (the "License"); you may not use this file except in compliance with
013: * the License. You may obtain a copy of the License at
014: *
015: * http://www.apache.org/licenses/LICENSE-2.0
016: *
017: * Unless required by applicable law or agreed to in writing, software
018: * distributed under the License is distributed on an "AS IS" BASIS,
019: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
020: * See the License for the specific language governing permissions and
021: * limitations under the License.
022: * ====================================================================
023: *
024: * This software consists of voluntary contributions made by many
025: * individuals on behalf of the Apache Software Foundation. For more
026: * information on the Apache Software Foundation, please see
027: * <http://www.apache.org/>.
028: *
029: */
030: package org.apache.commons.httpclient;
031:
032: import junit.framework.Test;
033: import junit.framework.TestCase;
034: import junit.framework.TestSuite;
035:
036: import org.apache.commons.httpclient.params.HttpConnectionManagerParams;
037: import org.apache.commons.httpclient.util.IdleConnectionHandler;
038: import org.apache.commons.httpclient.util.IdleConnectionTimeoutThread;
039:
040: /**
041: */
042: public class TestIdleConnectionTimeout extends TestCase {
043: /**
044: *
045: */
046: public TestIdleConnectionTimeout() {
047: super ();
048: }
049:
050: /**
051: * @param arg0
052: */
053: public TestIdleConnectionTimeout(String arg0) {
054: super (arg0);
055: }
056:
057: // ------------------------------------------------------- TestCase Methods
058:
059: public static Test suite() {
060: return new TestSuite(TestIdleConnectionTimeout.class);
061: }
062:
063: /**
064: * Tests that the IdleConnectionHandler correctly closes connections.
065: */
066: public void testHandler() {
067:
068: TimeoutHttpConnection connection = new TimeoutHttpConnection();
069:
070: IdleConnectionHandler handler = new IdleConnectionHandler();
071:
072: handler.add(connection);
073:
074: synchronized (this ) {
075: try {
076: this .wait(250);
077: } catch (InterruptedException e) {
078: e.printStackTrace();
079: }
080: }
081:
082: handler.closeIdleConnections(100);
083:
084: assertTrue("Connection not closed", connection.isClosed());
085:
086: connection.setClosed(false);
087:
088: handler.remove(connection);
089:
090: synchronized (this ) {
091: try {
092: this .wait(250);
093: } catch (InterruptedException e) {
094: e.printStackTrace();
095: }
096: }
097:
098: handler.closeIdleConnections(100);
099:
100: assertFalse("Connection closed", connection.isClosed());
101: }
102:
103: /**
104: * Tests that the IdleConnectionTimeoutThread works correctly.
105: */
106: public void testTimeoutThread() {
107:
108: TimeoutHttpConnectionManager cm = new TimeoutHttpConnectionManager();
109:
110: IdleConnectionTimeoutThread timeoutThread = new IdleConnectionTimeoutThread();
111: timeoutThread.addConnectionManager(cm);
112: timeoutThread.setTimeoutInterval(100);
113: timeoutThread.start();
114:
115: synchronized (this ) {
116: try {
117: this .wait(250);
118: } catch (InterruptedException e) {
119: e.printStackTrace();
120: }
121: }
122:
123: assertTrue("closeIdleConnections() not called", cm.closed);
124:
125: timeoutThread.removeConnectionManager(cm);
126: cm.closed = false;
127:
128: synchronized (this ) {
129: try {
130: this .wait(250);
131: } catch (InterruptedException e) {
132: e.printStackTrace();
133: }
134: }
135:
136: assertFalse("closeIdleConnections() called", cm.closed);
137:
138: timeoutThread.shutdown();
139: }
140:
141: private static class TimeoutHttpConnectionManager implements
142: HttpConnectionManager {
143:
144: public boolean closed = false;
145:
146: public void closeIdleConnections(long idleTimeout) {
147: this .closed = true;
148: }
149:
150: /**
151: * @deprecated
152: */
153: public HttpConnection getConnection(
154: HostConfiguration hostConfiguration, long timeout)
155: throws HttpException {
156: return null;
157: }
158:
159: public HttpConnection getConnection(
160: HostConfiguration hostConfiguration) {
161: return null;
162: }
163:
164: public HttpConnection getConnectionWithTimeout(
165: HostConfiguration hostConfiguration, long timeout)
166: throws ConnectionPoolTimeoutException {
167: return null;
168: }
169:
170: public HttpConnectionManagerParams getParams() {
171: return null;
172: }
173:
174: public void releaseConnection(HttpConnection conn) {
175: }
176:
177: public void setParams(HttpConnectionManagerParams params) {
178: }
179: }
180:
181: private static class TimeoutHttpConnection extends HttpConnection {
182:
183: private boolean closed = false;;
184:
185: public TimeoutHttpConnection() {
186: super ("fake-host", 80);
187: }
188:
189: /**
190: * @return Returns the closed.
191: */
192: public boolean isClosed() {
193: return closed;
194: }
195:
196: /**
197: * @param closed The closed to set.
198: */
199: public void setClosed(boolean closed) {
200: this .closed = closed;
201: }
202:
203: /* (non-Javadoc)
204: * @see org.apache.commons.httpclient.HttpConnection#close()
205: */
206: public void close() {
207: closed = true;
208: }
209: }
210:
211: }
|