1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
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
187
188
189 public void close() {
190 closed = true;
191 }
192 }
193
194 }