1 30 package org.apache.commons.httpclient; 31 32 import junit.framework.Test; 33 import junit.framework.TestCase; 34 import junit.framework.TestSuite; 35 36 import org.apache.commons.httpclient.params.HttpConnectionManagerParams; 37 import org.apache.commons.httpclient.util.IdleConnectionHandler; 38 import org.apache.commons.httpclient.util.IdleConnectionTimeoutThread; 39 40 42 public class TestIdleConnectionTimeout extends TestCase { 43 46 public TestIdleConnectionTimeout() { 47 super(); 48 } 49 52 public TestIdleConnectionTimeout(String arg0) { 53 super(arg0); 54 } 55 56 58 public static Test suite() { 59 return new TestSuite(TestIdleConnectionTimeout.class); 60 } 61 62 65 public void testHandler() { 66 67 TimeoutHttpConnection connection = new TimeoutHttpConnection(); 68 69 IdleConnectionHandler handler = new IdleConnectionHandler(); 70 71 handler.add(connection); 72 73 synchronized(this) { 74 try { 75 this.wait(250); 76 } catch (InterruptedException e) { 77 e.printStackTrace(); 78 } 79 } 80 81 handler.closeIdleConnections(100); 82 83 assertTrue("Connection not closed", connection.isClosed()); 84 85 connection.setClosed(false); 86 87 handler.remove(connection); 88 89 synchronized(this) { 90 try { 91 this.wait(250); 92 } catch (InterruptedException e) { 93 e.printStackTrace(); 94 } 95 } 96 97 handler.closeIdleConnections(100); 98 99 assertFalse("Connection closed", connection.isClosed()); 100 } 101 102 105 public void testTimeoutThread() { 106 107 TimeoutHttpConnectionManager cm = new TimeoutHttpConnectionManager(); 108 109 IdleConnectionTimeoutThread timeoutThread = new IdleConnectionTimeoutThread(); 110 timeoutThread.addConnectionManager(cm); 111 timeoutThread.setTimeoutInterval(100); 112 timeoutThread.start(); 113 114 synchronized(this) { 115 try { 116 this.wait(250); 117 } catch (InterruptedException e) { 118 e.printStackTrace(); 119 } 120 } 121 122 assertTrue("closeIdleConnections() not called", cm.closed); 123 124 timeoutThread.removeConnectionManager(cm); 125 cm.closed = false; 126 127 synchronized(this) { 128 try { 129 this.wait(250); 130 } catch (InterruptedException e) { 131 e.printStackTrace(); 132 } 133 } 134 135 assertFalse("closeIdleConnections() called", cm.closed); 136 137 timeoutThread.shutdown(); 138 } 139 140 private static class TimeoutHttpConnectionManager implements HttpConnectionManager { 141 142 public boolean closed = false; 143 144 public void closeIdleConnections(long idleTimeout) { 145 this.closed = true; 146 } 147 148 151 public HttpConnection getConnection(HostConfiguration hostConfiguration, long timeout) 152 throws HttpException { 153 return null; 154 } 155 156 public HttpConnection getConnection(HostConfiguration hostConfiguration) { 157 return null; 158 } 159 160 public HttpConnection getConnectionWithTimeout(HostConfiguration hostConfiguration, 161 long timeout) throws ConnectionPoolTimeoutException { 162 return null; 163 } 164 165 public HttpConnectionManagerParams getParams() { 166 return null; 167 } 168 169 public void releaseConnection(HttpConnection conn) { 170 } 171 172 public void setParams(HttpConnectionManagerParams params) { 173 } 174 } 175 176 private static class TimeoutHttpConnection extends HttpConnection { 177 178 private boolean closed = false;; 179 180 public TimeoutHttpConnection() { 181 super("fake-host", 80); 182 } 183 184 187 public boolean isClosed() { 188 return closed; 189 } 190 193 public void setClosed(boolean closed) { 194 this.closed = closed; 195 } 196 197 200 public void close() { 201 closed = true; 202 } 203 } 204 205 } 206 | Popular Tags |