// Get clientpool from server.
assertTrue(connector.getServerInvoker() instanceof SocketServerInvoker);
SocketServerInvoker serverInvoker = (SocketServerInvoker) connector.getServerInvoker();
Field field = SocketServerInvoker.class.getDeclaredField("clientpool");
field.setAccessible(true);
LRUPool clientpool = (LRUPool) field.get(serverInvoker);
// Use up server's threadpool.
ConnectThread ct1 = new ConnectThread(client, 16000, "ct1");
ConnectThread ct2 = new ConnectThread(client, 16000, "ct2");
ConnectThread ct3 = new ConnectThread(client, 16000, "ct3");
ConnectThread ct4 = new ConnectThread(client, 0, "ct4");
ct1.start();
Thread.sleep(2000); // +2000
List threads = clientpool.getContentsByAscendingAge();
assertEquals(1, threads.size());
Thread thread0 = (Thread) threads.get(0);
ct2.start();
Thread.sleep(2000); // +4000
threads = clientpool.getContentsByAscendingAge();
assertEquals(2, threads.size());
assertEquals(thread0, threads.get(1));
Thread thread1 = (Thread) threads.get(0);
ct3.start();
Thread.sleep(2000); // +6000
ct4.start();
Thread.sleep(2000); // +8000
threads = clientpool.getContentsByAscendingAge();
assertEquals(4, threads.size());
assertEquals(thread0, threads.get(3));
assertEquals(thread1, threads.get(2));
Thread thread2 = (Thread) threads.get(1);
// Verify newest thread gets evicted.
Thread.sleep(4000); // +12000
clientpool.evict();
Thread.sleep(2000); // +14000
threads = clientpool.getContentsByAscendingAge();
assertEquals(3, threads.size());
assertEquals(thread0, threads.get(2));
assertEquals(thread1, threads.get(1));
assertEquals(thread2, threads.get(0));
// Verify none of the other threads is ready to be evicted.
clientpool.evict();
threads = clientpool.getContentsByAscendingAge();
assertEquals(3, threads.size());
assertEquals(thread0, threads.get(2));
assertEquals(thread1, threads.get(1));
assertEquals(thread2, threads.get(0));
// Wait until all of the threads are done and verify that
// the oldest one gets evicted first.
Thread.sleep(12000); // +26000
threads = clientpool.getContentsByAscendingAge();
assertEquals(2, threads.size());
assertEquals(thread1, threads.get(1));
assertEquals(thread2, threads.get(0));
clientpool.evict();
Thread.sleep(2000); // +29000
threads = clientpool.getContentsByAscendingAge();
assertEquals(1, threads.size());
assertEquals(thread2, threads.get(0));
clientpool.evict();
Thread.sleep(2000); // +30000
threads = clientpool.getContentsByAscendingAge();
assertEquals(0, threads.size());
client.disconnect();
shutdownServer();
log.info(getName() + " PASSES");