1   /*
2    * $Header: /home/cvs/jakarta-commons/httpclient/src/test/org/apache/commons/httpclient/TestIdleConnectionTimeout.java,v 1.2 2004/05/04 21:24:51 olegk Exp $
3    * $Revision: 1.2 $
4    * $Date: 2004/05/04 21:24:51 $
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 org.apache.commons.httpclient.params.HttpConnectionManagerParams;
32  import org.apache.commons.httpclient.util.IdleConnectionHandler;
33  import org.apache.commons.httpclient.util.IdleConnectionTimeoutThread;
34  
35  /***
36   */
37  public class TestIdleConnectionTimeout extends TestNoHostBase {
38      /***
39       * 
40       */
41      public TestIdleConnectionTimeout() {
42          super();
43      }
44      /***
45       * @param arg0
46       */
47      public TestIdleConnectionTimeout(String arg0) {
48          super(arg0);
49      }
50      
51      /***
52       * Tests that the IdleConnectionHandler correctly closes connections.
53       */
54      public void testHandler() {
55          
56          TimeoutHttpConnection connection = new TimeoutHttpConnection();
57          
58          IdleConnectionHandler handler = new IdleConnectionHandler();
59          
60          handler.add(connection);
61          
62          synchronized(this) {
63              try {
64                  this.wait(250);
65              } catch (InterruptedException e) {
66                  e.printStackTrace();
67              }
68          }
69          
70          handler.closeIdleConnections(100);
71          
72          assertTrue("Connection not closed", connection.isClosed());
73  
74          connection.setClosed(false);
75          
76          handler.remove(connection);
77          
78          synchronized(this) {
79              try {
80                  this.wait(250);
81              } catch (InterruptedException e) {
82                  e.printStackTrace();
83              }
84          }
85  
86          handler.closeIdleConnections(100);
87          
88          assertFalse("Connection closed", connection.isClosed());
89      }
90  
91      /***
92       * Tests that the IdleConnectionTimeoutThread works correctly.
93       */
94      public void testTimeoutThread() {
95          
96          TimeoutHttpConnectionManager cm = new TimeoutHttpConnectionManager();        
97          
98          IdleConnectionTimeoutThread timeoutThread = new IdleConnectionTimeoutThread();
99          timeoutThread.addConnectionManager(cm);
100         timeoutThread.setTimeoutInterval(100);
101         timeoutThread.start();
102         
103         synchronized(this) {
104             try {
105                 this.wait(250);
106             } catch (InterruptedException e) {
107                 e.printStackTrace();
108             }
109         }
110         
111         assertTrue("closeIdleConnections() not called", cm.closed);
112 
113         timeoutThread.removeConnectionManager(cm);
114         cm.closed = false;
115         
116         synchronized(this) {
117             try {
118                 this.wait(250);
119             } catch (InterruptedException e) {
120                 e.printStackTrace();
121             }
122         }
123 
124         assertFalse("closeIdleConnections() called", cm.closed);
125         
126         timeoutThread.shutdown();
127     }    
128     
129     private static class TimeoutHttpConnectionManager implements HttpConnectionManager {
130         
131         public boolean closed = false;
132         
133         public void closeIdleConnections(long idleTimeout) {
134             this.closed = true;
135         }
136 
137         /***
138          * @deprecated
139          */
140         public HttpConnection getConnection(HostConfiguration hostConfiguration, long timeout)
141             throws HttpException {
142             return null;
143         }
144 
145         public HttpConnection getConnection(HostConfiguration hostConfiguration) {
146             return null;
147         }
148 
149         public HttpConnection getConnectionWithTimeout(HostConfiguration hostConfiguration,
150             long timeout) throws ConnectTimeoutException {
151             return null;
152         }
153 
154         public HttpConnectionManagerParams getParams() {
155             return null;
156         }
157 
158         public void releaseConnection(HttpConnection conn) {
159         }
160 
161         public void setParams(HttpConnectionManagerParams params) {
162         }
163 }
164     
165     private static class TimeoutHttpConnection extends HttpConnection {
166         
167         private boolean closed = false;;
168         
169         public TimeoutHttpConnection() {
170             super("fake-host", 80);
171         }
172         
173         /***
174          * @return Returns the closed.
175          */
176         public boolean isClosed() {
177             return closed;
178         }
179         /***
180          * @param closed The closed to set.
181          */
182         public void setClosed(boolean closed) {
183             this.closed = closed;
184         }
185         
186         /* (non-Javadoc)
187          * @see org.apache.commons.httpclient.HttpConnection#close()
188          */
189         public void close() {
190             closed = true;
191         }
192     }
193     
194 }