CacheableIPList implements IPList { @Override public boolean isIn(String ipAddress) { if (cacheExpiryTimeStamp >= 0 && cacheExpiryTimeStamp < System.currentTimeMillis()) { synchronized(this) { if (cacheExpiryTimeStamp < System.currentTimeMillis()) { reset(); } } } return ipList.isIn(ipAddress); } CacheableIPList(FileBasedIPList ipList, long cacheTimeout); void refresh(); @Override boolean isIn(String ipAddress);  }
@Test public void testAddWithSleepForCacheTimeout() throws IOException, InterruptedException { String[] ips = {"10.119.103.112", "10.221.102.0/23", "10.113.221.221"}; TestFileBasedIPList.createFileWithEntries ("ips.txt", ips); CacheableIPList cipl = new CacheableIPList( new FileBasedIPList("ips.txt"),100); assertFalse("10.113.221.222 is in the list", cipl.isIn("10.113.221.222")); assertFalse ("10.222.103.121 is in the list", cipl.isIn("10.222.103.121")); TestFileBasedIPList.removeFile("ips.txt"); String[]ips2 = {"10.119.103.112", "10.221.102.0/23", "10.222.0.0/16", "10.113.221.221", "10.113.221.222"}; TestFileBasedIPList.createFileWithEntries ("ips.txt", ips2); Thread.sleep(101); assertTrue("10.113.221.222 is not in the list", cipl.isIn("10.113.221.222")); assertTrue ("10.222.103.121 is not in the list", cipl.isIn("10.222.103.121")); TestFileBasedIPList.removeFile("ips.txt"); }
@Test public void testRemovalWithSleepForCacheTimeout() throws IOException, InterruptedException { String[] ips = {"10.119.103.112", "10.221.102.0/23", "10.222.0.0/16", "10.113.221.221", "10.113.221.222"}; TestFileBasedIPList.createFileWithEntries ("ips.txt", ips); CacheableIPList cipl = new CacheableIPList( new FileBasedIPList("ips.txt"),100); assertTrue("10.113.221.222 is not in the list", cipl.isIn("10.113.221.222")); assertTrue ("10.222.103.121 is not in the list", cipl.isIn("10.222.103.121")); TestFileBasedIPList.removeFile("ips.txt"); String[]ips2 = {"10.119.103.112", "10.221.102.0/23", "10.113.221.221"}; TestFileBasedIPList.createFileWithEntries ("ips.txt", ips2); Thread.sleep(1005); assertFalse("10.113.221.222 is in the list", cipl.isIn("10.113.221.222")); assertFalse ("10.222.103.121 is in the list", cipl.isIn("10.222.103.121")); TestFileBasedIPList.removeFile("ips.txt"); }