config.setExpireMilliSec(1000);
config.setCustomTicker(ticker);
configManager.setCacheConfiguration(config);
CachedRuleReader cachedRuleReader = new CachedRuleReader(configManager);
cachedRuleReader.setRealRuleReaderService(realReader);
cachedRuleReader.getCacheInitParams().setSize(100);
cachedRuleReader.getCacheInitParams().setRefreshMilliSec(500);
cachedRuleReader.getCacheInitParams().setExpireMilliSec(1000);
cachedRuleReader.getCacheInitParams().setCustomTicker(ticker);
cachedRuleReader.init();
System.out.println(cachedRuleReader.getStats());
assertEquals(0, cachedRuleReader.getStats().hitCount());
assertEquals(0, cachedRuleReader.getStats().missCount());
assertEquals(0, cachedRuleReader.getStats().evictionCount());
RuleFilter filter1 = new RuleFilter();
filter1.setUser("test_1");
RuleFilter filter2 = new RuleFilter();
filter2.setUser("test_2");
RuleFilter filter3 = new RuleFilter();
filter3.setUser("test_3");
int hitExp = 0;
int missExp = 0;
int evictExp = 0;
// first loading
AccessInfo ai1_1= cachedRuleReader.getAccessInfo(filter1);
System.out.println(cachedRuleReader.getStats());
assertEquals(hitExp, cachedRuleReader.getStats().hitCount());
assertEquals(++missExp, cachedRuleReader.getStats().missCount());
assertEquals(evictExp, cachedRuleReader.getStats().evictionCount());
// second loading with the same rule, should be a hit
ticker.setMillisec(1);
AccessInfo ai1_2= cachedRuleReader.getAccessInfo(filter1);
System.out.println(cachedRuleReader.getStats());
assertEquals(++hitExp, cachedRuleReader.getStats().hitCount());
assertEquals(missExp, cachedRuleReader.getStats().missCount());
assertEquals(evictExp, cachedRuleReader.getStats().evictionCount());
assertEquals(1, cachedRuleReader.getStats().loadSuccessCount());
assertEquals(ai1_1, ai1_2);
// loading the same filter, after the refresh time
ticker.setMillisec(600);
// LOGGER.log(Level.INFO, "We expect a reload() now....");
System.out.println("---> We expect a reload() now....");
AccessInfo ai1_3= cachedRuleReader.getAccessInfo(filter1);
System.out.println(cachedRuleReader.getStats());
assertEquals(++hitExp, cachedRuleReader.getStats().hitCount());
assertEquals(missExp, cachedRuleReader.getStats().missCount());
assertEquals(evictExp, cachedRuleReader.getStats().evictionCount());
// assertEquals(2, cachedRuleReader.getStats().loadSuccessCount()); // dunno if load is made asynch or not
// reloading should have been triggered
ticker.setMillisec(700);
System.out.println("sleeping...");
Thread.sleep(500);
System.out.println(cachedRuleReader.getStats());
assertEquals(hitExp, cachedRuleReader.getStats().hitCount());
assertEquals(missExp, cachedRuleReader.getStats().missCount());
assertEquals(evictExp, cachedRuleReader.getStats().evictionCount());
// assertEquals(2, cachedRuleReader.getStats().loadSuccessCount()); // uhm, this does not work
if(2!=cachedRuleReader.getStats().loadSuccessCount())
LOGGER.log(Level.SEVERE, "*** Bad successCount check, expected 2, found {0}", cachedRuleReader.getStats().loadSuccessCount());
ticker.setMillisec(800);
cachedRuleReader.getAccessInfo(filter1);
System.out.println(cachedRuleReader.getStats());
ticker.setMillisec(2000);
cachedRuleReader.getAccessInfo(filter1);
System.out.println(cachedRuleReader.getStats());
}