1
0
mirror of https://github.com/square/okhttp.git synced 2026-01-25 16:01:38 +03:00

Merge pull request #124 from square/jw/eviction

Add evictAll method to connection pool.
This commit is contained in:
Jesse Wilson
2013-02-27 08:59:17 -08:00
2 changed files with 25 additions and 0 deletions

View File

@@ -241,4 +241,17 @@ public class ConnectionPool {
}
}
}
/** Close and remove all connections in the pool. */
public void evictAll() {
List<Connection> connections;
synchronized (this) {
connections = new ArrayList<Connection>(this.connections);
this.connections.clear();
}
for (Connection connection : connections) {
Util.closeQuietly(connection);
}
}
}

View File

@@ -385,6 +385,18 @@ public final class ConnectionPoolTest {
assertEquals(0, pool.getSpdyConnectionCount());
}
@Test public void evictAllConnections() {
ConnectionPool pool = new ConnectionPool(10, KEEP_ALIVE_DURATION_MS);
pool.recycle(httpA);
Util.closeQuietly(httpA); // Include a closed connection in the pool.
pool.recycle(httpB);
pool.maybeShare(spdyA);
assertEquals(3, pool.getConnectionCount());
pool.evictAll();
assertEquals(0, pool.getConnectionCount());
}
private void assertPooled(ConnectionPool pool, Connection... connections) throws Exception {
assertEquals(Arrays.asList(connections), pool.getConnections());
}