@Test
public void testCleanup() throws Exception {

  Map<ConnectionPoolId, ConnectionPool> poolMap = connManager.getPools();
  ConnectionPool pool1 = new ConnectionPool(
      conf, TEST_NN_ADDRESS, TEST_USER1, 0, 10, 0.5f, ClientProtocol.class);
  addConnectionsToPool(pool1, 9, 4);
  poolMap.put(
      new ConnectionPoolId(TEST_USER1, TEST_NN_ADDRESS, ClientProtocol.class),
      pool1);
  ConnectionPool pool2 = new ConnectionPool(
      conf, TEST_NN_ADDRESS, TEST_USER2, 0, 10, 0.5f, ClientProtocol.class);
  addConnectionsToPool(pool2, 10, 10);
  poolMap.put(
      new ConnectionPoolId(TEST_USER2, TEST_NN_ADDRESS, ClientProtocol.class),
      pool2);
  checkPoolConnections(TEST_USER1, 9, 4);
  checkPoolConnections(TEST_USER2, 10, 10);
  // Clean up first pool, one connection should be removed, and second pool
  // should remain the same.
  connManager.cleanup(pool1);
  checkPoolConnections(TEST_USER1, 8, 4);
  checkPoolConnections(TEST_USER2, 10, 10);
  // Clean up the first pool again, it should have no effect since it reached
  // the MIN_ACTIVE_RATIO.
  connManager.cleanup(pool1);
  checkPoolConnections(TEST_USER1, 8, 4);
  checkPoolConnections(TEST_USER2, 10, 10);
  // Make sure the number of connections doesn't go below minSize
  ConnectionPool pool3 = new ConnectionPool(
      conf, TEST_NN_ADDRESS, TEST_USER3, 2, 10, 0.5f, ClientProtocol.class);
  addConnectionsToPool(pool3, 8, 0);
  poolMap.put(
      new ConnectionPoolId(TEST_USER3, TEST_NN_ADDRESS, ClientProtocol.class),
      pool3);
  checkPoolConnections(TEST_USER3, 10, 0);
  for (int i = 0; i < 10; i++) {
    connManager.cleanup(pool3);
  }
  checkPoolConnections(TEST_USER3, 2, 0);
  // With active connections added to pool, make sure it honors the
  // MIN_ACTIVE_RATIO again
  addConnectionsToPool(pool3, 8, 2);
  checkPoolConnections(TEST_USER3, 10, 2);
  for (int i = 0; i < 10; i++) {
    connManager.cleanup(pool3);
  }
  checkPoolConnections(TEST_USER3, 4, 2);
}