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 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
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
197
198
199 public void close() {
200 closed = true;
201 }
202 }
203
204 }