}
@Test
public void testTimerRuleAfterCronReloadSession() throws Exception {
KnowledgeBase kbase = KnowledgeBaseFactory.newKnowledgeBase();
StatefulKnowledgeSession ksession = createSession( kbase );
// must advance time or it won't save.
SessionPseudoClock clock = (SessionPseudoClock) ksession.<SessionClock>getSessionClock();
clock.advanceTime( 300,
TimeUnit.MILLISECONDS );
// if we do not call 'ksession.fireAllRules()', this test will run successfully.
ksession.fireAllRules();
clock = (SessionPseudoClock) ksession.<SessionClock>getSessionClock();
clock.advanceTime( 300,
TimeUnit.MILLISECONDS );
ksession = disposeAndReloadSession( ksession,
kbase );
clock = (SessionPseudoClock) ksession.<SessionClock>getSessionClock();
clock.advanceTime( 300,
TimeUnit.MILLISECONDS );
// build timer rule, if the rule is fired, the list size will increase every 300ms
String timerRule = "package org.drools.test\n" +
"global java.util.List list \n" +
"rule TimerRule \n" +
" timer (cron: * * * * * ?) \n" +
"when \n" + "then \n" +
" list.add(list.size()); \n" +
" end";
Resource resource = ResourceFactory.newByteArrayResource( timerRule.getBytes() );
Collection<KnowledgePackage> kpackages = buildKnowledgePackage( resource,
ResourceType.DRL );
kbase.addKnowledgePackages( kpackages );
List<Integer> list = Collections.synchronizedList( new ArrayList<Integer>() );
ksession.setGlobal( "list",
list );
ksession.setGlobal( "list",
list );
clock.advanceTime( 10,
TimeUnit.MILLISECONDS );
ksession.fireAllRules();
clock = (SessionPseudoClock) ksession.<SessionClock>getSessionClock();
clock.advanceTime( 10,
TimeUnit.MILLISECONDS );
ksession = disposeAndReloadSession( ksession,
kbase );
ksession.setGlobal( "list",
list );
clock = (SessionPseudoClock) ksession.<SessionClock>getSessionClock();
clock.advanceTime( 10,
TimeUnit.MILLISECONDS );
Assert.assertEquals( 1,
list.size() );
clock = (SessionPseudoClock) ksession.<SessionClock>getSessionClock();
clock.advanceTime( 3,
TimeUnit.SECONDS );
Assert.assertEquals( 4,
list.size() );
ksession = disposeAndReloadSession( ksession,
kbase );
ksession.setGlobal( "list",
list );
clock = (SessionPseudoClock) ksession.<SessionClock>getSessionClock();
clock.advanceTime( 2,
TimeUnit.SECONDS );
// if the rule is fired, the list size will greater than one.
Assert.assertEquals( 6,