1   /*
2    * $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 $
3    * $Revision: 155418 $
4    * $Date: 2005-02-26 08:01:52 -0500 (Sat, 26 Feb 2005) $
5    *
6    * ====================================================================
7    *
8    *  Copyright 2004 The Apache Software Foundation
9    *
10   *  Licensed under the Apache License, Version 2.0 (the "License");
11   *  you may not use this file except in compliance with the License.
12   *  You may obtain a copy of the License at
13   *
14   *      http://www.apache.org/licenses/LICENSE-2.0
15   *
16   *  Unless required by applicable law or agreed to in writing, software
17   *  distributed under the License is distributed on an "AS IS" BASIS,
18   *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
19   *  See the License for the specific language governing permissions and
20   *  limitations under the License.
21   * ====================================================================
22   *
23   * This software consists of voluntary contributions made by many
24   * individuals on behalf of the Apache Software Foundation.  For more
25   * information on the Apache Software Foundation, please see
26   * <http://www.apache.org/>.
27   *
28   */
29  package org.apache.commons.httpclient;
30  
31  import junit.framework.Test;
32  import junit.framework.TestCase;
33  import junit.framework.TestSuite;
34  
35  import org.apache.commons.httpclient.params.HttpConnectionManagerParams;
36  import org.apache.commons.httpclient.util.IdleConnectionHandler;
37  import org.apache.commons.httpclient.util.IdleConnectionTimeoutThread;
38  
39  /***
40   */
41  public class TestIdleConnectionTimeout extends TestCase {
42      /***
43       * 
44       */
45      public TestIdleConnectionTimeout() {
46          super();
47      }
48      /***
49       * @param arg0
50       */
51      public TestIdleConnectionTimeout(String arg0) {
52          super(arg0);
53      }
54      
55      // ------------------------------------------------------- TestCase Methods
56  
57      public static Test suite() {
58          return new TestSuite(TestIdleConnectionTimeout.class);
59      }
60  
61      /***
62       * Tests that the IdleConnectionHandler correctly closes connections.
63       */
64      public void testHandler() {
65          
66          TimeoutHttpConnection connection = new TimeoutHttpConnection();
67          
68          IdleConnectionHandler handler = new IdleConnectionHandler();
69          
70          handler.add(connection);
71          
72          synchronized(this) {
73              try {
74                  this.wait(250);
75              } catch (InterruptedException e) {
76                  e.printStackTrace();
77              }
78          }
79          
80          handler.closeIdleConnections(100);
81          
82          assertTrue("Connection not closed", connection.isClosed());
83  
84          connection.setClosed(false);
85          
86          handler.remove(connection);
87          
88          synchronized(this) {
89              try {
90                  this.wait(250);
91              } catch (InterruptedException e) {
92                  e.printStackTrace();
93              }
94          }
95  
96          handler.closeIdleConnections(100);
97          
98          assertFalse("Connection closed", connection.isClosed());
99      }
100 
101     /***
102      * Tests that the IdleConnectionTimeoutThread works correctly.
103      */
104     public void testTimeoutThread() {
105         
106         TimeoutHttpConnectionManager cm = new TimeoutHttpConnectionManager();        
107         
108         IdleConnectionTimeoutThread timeoutThread = new IdleConnectionTimeoutThread();
109         timeoutThread.addConnectionManager(cm);
110         timeoutThread.setTimeoutInterval(100);
111         timeoutThread.start();
112         
113         synchronized(this) {
114             try {
115                 this.wait(250);
116             } catch (InterruptedException e) {
117                 e.printStackTrace();
118             }
119         }
120         
121         assertTrue("closeIdleConnections() not called", cm.closed);
122 
123         timeoutThread.removeConnectionManager(cm);
124         cm.closed = false;
125         
126         synchronized(this) {
127             try {
128                 this.wait(250);
129             } catch (InterruptedException e) {
130                 e.printStackTrace();
131             }
132         }
133 
134         assertFalse("closeIdleConnections() called", cm.closed);
135         
136         timeoutThread.shutdown();
137     }    
138     
139     private static class TimeoutHttpConnectionManager implements HttpConnectionManager {
140         
141         public boolean closed = false;
142         
143         public void closeIdleConnections(long idleTimeout) {
144             this.closed = true;
145         }
146 
147         /***
148          * @deprecated
149          */
150         public HttpConnection getConnection(HostConfiguration hostConfiguration, long timeout)
151             throws HttpException {
152             return null;
153         }
154 
155         public HttpConnection getConnection(HostConfiguration hostConfiguration) {
156             return null;
157         }
158 
159         public HttpConnection getConnectionWithTimeout(HostConfiguration hostConfiguration,
160             long timeout) throws ConnectionPoolTimeoutException {
161             return null;
162         }
163 
164         public HttpConnectionManagerParams getParams() {
165             return null;
166         }
167 
168         public void releaseConnection(HttpConnection conn) {
169         }
170 
171         public void setParams(HttpConnectionManagerParams params) {
172         }
173 }
174     
175     private static class TimeoutHttpConnection extends HttpConnection {
176         
177         private boolean closed = false;;
178         
179         public TimeoutHttpConnection() {
180             super("fake-host", 80);
181         }
182         
183         /***
184          * @return Returns the closed.
185          */
186         public boolean isClosed() {
187             return closed;
188         }
189         /***
190          * @param closed The closed to set.
191          */
192         public void setClosed(boolean closed) {
193             this.closed = closed;
194         }
195         
196         /* (non-Javadoc)
197          * @see org.apache.commons.httpclient.HttpConnection#close()
198          */
199         public void close() {
200             closed = true;
201         }
202     }
203     
204 }