AutonomicApplicationContext { public List<Rule> findRulesToExecute() { this.logger.fine( "Looking for rules to execute after an event was recorded for application " + this.app ); List<Rule> result = new ArrayList<> (); long now = System.nanoTime(); for( Rule rule : this.ruleNameToRule.values()) { long validPeriodStart = now - TimeUnit.SECONDS.toNanos( rule.getDelayBetweenSucceedingInvocations()); Long lastExecutionTime = this.ruleNameToLastExecution.get( rule.getRuleName()); if( lastExecutionTime != null && lastExecutionTime - validPeriodStart > 0 ) { this.logger.finer( "Ignoring the rule " + rule.getRuleName() + " since the execution delay has not yet expired." ); continue; } Long lastRecord = this.eventNameToLastRecordTime.get( rule.getEventName()); if( lastRecord == null ) continue; StringBuilder sbTrigger = new StringBuilder(); sbTrigger.append( rule.getEventName()); sbTrigger.append( lastRecord ); String lastTrigger = this.ruleNameToLastTrigger.get( rule.getRuleName()); if( Objects.equals( lastTrigger, sbTrigger.toString())) { this.logger.finer( "Ignoring the rule " + rule.getRuleName() + " since no new event occurred since its last execution." ); continue; } validPeriodStart = now - TimeUnit.SECONDS.toNanos( rule.getTimingWindow()); if( rule.getTimingWindow() != Rule.NO_TIMING_WINDOW && lastRecord - validPeriodStart < 0 ) { this.logger.finer( "Ignoring the rule " + rule.getRuleName() + " since no new event occurred since its last execution." ); continue; } this.logger.finer( "Rule " + rule.getRuleName() + " was found following the occurrence of the " + rule.getEventName() + " event." ); this.ruleNameToLastTrigger.put( rule.getRuleName(), sbTrigger.toString()); result.add( rule ); } return result; } AutonomicApplicationContext( Application app ); @Override String toString(); AtomicInteger getVmCount(); void registerEvent( String eventName ); void recordPreExecution( String ruleName ); List<Rule> findRulesToExecute(); final Map<String,Rule> ruleNameToRule; }
@Test public void testFindRulesToExecute_noRule() throws Exception { TestApplication app = new TestApplication(); AutonomicApplicationContext ctx = new AutonomicApplicationContext( app ); Assert.assertEquals( 0, ctx.ruleNameToRule.size()); Assert.assertEquals( 0, ctx.eventNameToLastRecordTime.size()); Assert.assertEquals( 0, ctx.ruleNameToLastExecution.size()); Assert.assertEquals( 0, ctx.ruleNameToLastTrigger.size()); Assert.assertEquals( 0, ctx.vmCount.get()); Assert.assertEquals( 0, ctx.findRulesToExecute().size()); Assert.assertEquals( 0, ctx.ruleNameToRule.size()); Assert.assertEquals( 0, ctx.eventNameToLastRecordTime.size()); Assert.assertEquals( 0, ctx.ruleNameToLastExecution.size()); Assert.assertEquals( 0, ctx.ruleNameToLastTrigger.size()); Assert.assertEquals( 0, ctx.vmCount.get()); }