@Test public void connectionsEvictedWhenIdleLongEnough() throws Exception { ConnectionPool pool = new ConnectionPool(1, 100L, TimeUnit.NANOSECONDS); pool.setCleanupRunnableForTest(emptyRunnable); RealConnection c1 = new RealConnection(routeA1); c1.idleAtNanos = 50L; c1.socket = new Socket(); assertFalse(c1.socket.isClosed()); pool.put(c1); // Running at time 50, the pool returns that nothing can be evicted until time 150. assertEquals(100L, pool.cleanup(50L)); assertEquals(1, pool.getConnectionCount()); assertFalse(c1.socket.isClosed()); // Running at time 60, the pool returns that nothing can be evicted until time 150. assertEquals(90L, pool.cleanup(60L)); assertEquals(1, pool.getConnectionCount()); assertFalse(c1.socket.isClosed()); // Running at time 149, the pool returns that nothing can be evicted until time 150. assertEquals(1L, pool.cleanup(149L)); assertEquals(1, pool.getConnectionCount()); assertFalse(c1.socket.isClosed()); // Running at time 150, the pool evicts. assertEquals(0, pool.cleanup(150L)); assertEquals(0, pool.getConnectionCount()); assertTrue(c1.socket.isClosed()); // Running again, the pool reports that no further runs are necessary. assertEquals(-1, pool.cleanup(150L)); assertEquals(0, pool.getConnectionCount()); assertTrue(c1.socket.isClosed()); }
@Test public void inUseConnectionsNotEvicted() throws Exception { ConnectionPool pool = new ConnectionPool(1, 100L, TimeUnit.NANOSECONDS); pool.setCleanupRunnableForTest(emptyRunnable); RealConnection c1 = new RealConnection(routeA1); c1.allocationCount = 1; c1.idleAtNanos = 50L; c1.socket = new Socket(); assertFalse(c1.socket.isClosed()); pool.put(c1); // Running at time 50, the pool returns that nothing can be evicted until time 150. assertEquals(100L, pool.cleanup(50L)); assertEquals(1, pool.getConnectionCount()); assertFalse(c1.socket.isClosed()); // Running at time 60, the pool returns that nothing can be evicted until time 160. assertEquals(100L, pool.cleanup(60L)); assertEquals(1, pool.getConnectionCount()); assertFalse(c1.socket.isClosed()); // Running at time 160, the pool returns that nothing can be evicted until time 260. assertEquals(100L, pool.cleanup(160L)); assertEquals(1, pool.getConnectionCount()); assertFalse(c1.socket.isClosed()); }
@Test public void cleanupPrioritizesEarliestEviction() throws Exception { ConnectionPool pool = new ConnectionPool(1, 100L, TimeUnit.NANOSECONDS); pool.setCleanupRunnableForTest(emptyRunnable); RealConnection c1 = new RealConnection(routeA1); c1.idleAtNanos = 75L; c1.socket = new Socket(); pool.put(c1); RealConnection c2 = new RealConnection(routeB1); c2.idleAtNanos = 50L; c2.socket = new Socket(); pool.put(c2); // Running at time 75, the pool returns that nothing can be evicted until time 150. assertEquals(75L, pool.cleanup(75L)); assertEquals(2, pool.getConnectionCount()); // Running at time 149, the pool returns that nothing can be evicted until time 150. assertEquals(1L, pool.cleanup(149L)); assertEquals(2, pool.getConnectionCount()); // Running at time 150, the pool evicts c2. assertEquals(0L, pool.cleanup(150L)); assertEquals(1, pool.getConnectionCount()); assertFalse(c1.socket.isClosed()); assertTrue(c2.socket.isClosed()); // Running at time 150, the pool returns that nothing can be evicted until time 175. assertEquals(25L, pool.cleanup(150L)); assertEquals(1, pool.getConnectionCount()); // Running at time 175, the pool evicts c1. assertEquals(0L, pool.cleanup(175L)); assertEquals(0, pool.getConnectionCount()); assertTrue(c1.socket.isClosed()); assertTrue(c2.socket.isClosed()); }