| | |
| | | |
| | | import java.util.LinkedList; |
| | | import java.util.List; |
| | | import java.util.concurrent.Callable; |
| | | import java.util.concurrent.TimeUnit; |
| | | |
| | | import org.forgerock.opendj.ldap.requests.BindRequest; |
| | |
| | | assertThat(scheduler.isScheduled()).isTrue(); |
| | | |
| | | // First populate the pool with idle connections at time 0. |
| | | @SuppressWarnings("unchecked") |
| | | final Callable<Long> timeSource = mock(Callable.class); |
| | | when(timeSource.call()).thenReturn(0L); |
| | | pool.testTimeSource = timeSource; |
| | | pool.timeSource = mockTimeSource(0); |
| | | |
| | | assertThat(pool.currentPoolSize()).isEqualTo(0); |
| | | Connection c1 = pool.getConnection(); |
| | |
| | | assertThat(pool.currentPoolSize()).isEqualTo(4); |
| | | |
| | | // First purge at time 50 is no-op because no connections have expired. |
| | | when(timeSource.call()).thenReturn(50L); |
| | | scheduler.getCommand().run(); |
| | | when(pool.timeSource.currentTimeMillis()).thenReturn(50L); |
| | | scheduler.runFirstTask(); |
| | | assertThat(pool.currentPoolSize()).isEqualTo(4); |
| | | |
| | | // Second purge at time 150 should remove 2 non-core connections. |
| | | when(timeSource.call()).thenReturn(150L); |
| | | scheduler.getCommand().run(); |
| | | when(pool.timeSource.currentTimeMillis()).thenReturn(150L); |
| | | scheduler.runFirstTask(); |
| | | assertThat(pool.currentPoolSize()).isEqualTo(2); |
| | | |
| | | verify(pooledConnection1, times(1)).close(); |
| | |
| | | verify(pooledConnection4, times(0)).close(); |
| | | |
| | | // Regrow the pool at time 200. |
| | | when(timeSource.call()).thenReturn(200L); |
| | | when(pool.timeSource.currentTimeMillis()).thenReturn(200L); |
| | | Connection c5 = pool.getConnection(); // pooledConnection3 |
| | | Connection c6 = pool.getConnection(); // pooledConnection4 |
| | | Connection c7 = pool.getConnection(); // pooledConnection5 |
| | |
| | | assertThat(pool.currentPoolSize()).isEqualTo(4); |
| | | |
| | | // Third purge at time 250 should not remove any connections. |
| | | when(timeSource.call()).thenReturn(250L); |
| | | scheduler.getCommand().run(); |
| | | when(pool.timeSource.currentTimeMillis()).thenReturn(250L); |
| | | scheduler.runFirstTask(); |
| | | assertThat(pool.currentPoolSize()).isEqualTo(4); |
| | | |
| | | // Fourth purge at time 350 should remove 2 non-core connections. |
| | | when(timeSource.call()).thenReturn(350L); |
| | | scheduler.getCommand().run(); |
| | | when(pool.timeSource.currentTimeMillis()).thenReturn(350L); |
| | | scheduler.runFirstTask(); |
| | | assertThat(pool.currentPoolSize()).isEqualTo(2); |
| | | |
| | | verify(pooledConnection3, times(1)).close(); |