@Test
public void testResetExecutingTokenStateMethod()
throws Exception
{
String localHost = new DefaultSystemNameProvider().getSystemName();
TokenContextService tokenContextService = getProcessServer().getTokenContextService();
// Create all kinds of contexts and simulate several states
TokenContext tc1 = startSimpleSignalSetProcess("TestSignal1", "Done", 0);
tc1.setLifecycleState(LifecycleState.SELECTED);
tc1.setLifecycleRequest(LifecycleRequest.NONE);
tc1.setNodeId(localHost);
tokenContextService.saveContext(tc1);
TokenContext tc2 = startSimpleSignalSetProcess("TestSignal2", "Done", 0);
tc2.setLifecycleState(LifecycleState.SELECTED);
tc2.setLifecycleRequest(LifecycleRequest.NONE);
tc2.setNodeId("DifferentHost");
tokenContextService.saveContext(tc2);
TokenContext tc3 = startSimpleSignalSetProcess("TestSignal3", "Done", 0);
tc3.setLifecycleState(LifecycleState.RUNNING);
tc3.setLifecycleRequest(LifecycleRequest.NONE);
tc3.setNodeId(localHost);
tokenContextService.saveContext(tc3);
TokenContext tc4 = startSimpleSignalSetProcess("TestSignal4", "Done", 0);
tc4.setLifecycleState(LifecycleState.ERROR);
tc4.setLifecycleRequest(LifecycleRequest.NONE);
tc4.setNodeId(localHost);
tokenContextService.saveContext(tc4);
TokenContext tc5 = startSimpleSignalSetProcess("TestSignal5", "Done", 0);
tokenContextService.saveContext(tc5);
getProcessServer().getEngine().commit();
// Execute those that can be executed
getProcessFacade().executePendingContextsInThisThread();
// Check the state of the executions
assertEquals(null, TestCaseSyncMgr.getInstance().getSignal(null, "TestSignal1"));
assertEquals(null, TestCaseSyncMgr.getInstance().getSignal(null, "TestSignal2"));
assertEquals(null, TestCaseSyncMgr.getInstance().getSignal(null, "TestSignal3"));
assertEquals(null, TestCaseSyncMgr.getInstance().getSignal(null, "TestSignal4"));
assertEquals("Done", TestCaseSyncMgr.getInstance().getSignal(null, "TestSignal5"));
// *** Now reset all contexts that seem to be currently executed by this node ***
// (i. e. simulate system startup)
int ret = getProcessFacade().resetExecutingTokenState(null);
assertEquals(2, ret);
// Clear cache so we will not be fooled by cached context objects
tokenContextService.clearCache();
// Execute those that can be executed
getProcessFacade().executePendingContextsInThisThread();
// Check the state of the executions
// 2 Contexts should still be there (tc2: Different host, tc4: Error state)
assertEquals("Done", TestCaseSyncMgr.getInstance().getSignal(null, "TestSignal1"));
assertEquals(null, TestCaseSyncMgr.getInstance().getSignal(null, "TestSignal2"));
assertEquals("Done", TestCaseSyncMgr.getInstance().getSignal(null, "TestSignal3"));
assertEquals(null, TestCaseSyncMgr.getInstance().getSignal(null, "TestSignal4"));
assertEquals("Done", TestCaseSyncMgr.getInstance().getSignal(null, "TestSignal5"));
// Cleanup the last ones
tc2 = tokenContextService.getContextById(tc2.getId());
tc2.setLifecycleState(LifecycleState.SUSPENDED);
tc2.setLifecycleRequest(LifecycleRequest.RESUME);
tc2.setNodeId(localHost);
tokenContextService.saveContext(tc2);
tc4 = tokenContextService.getContextById(tc4.getId());
tc4.setLifecycleState(LifecycleState.SUSPENDED);
tc4.setLifecycleRequest(LifecycleRequest.RESUME);
tokenContextService.saveContext(tc4);
tokenContextService.commit();
getProcessFacade().executePendingContextsInThisThread();
assertEquals("Done", TestCaseSyncMgr.getInstance().getSignal(null, "TestSignal2"));
assertEquals("Done", TestCaseSyncMgr.getInstance().getSignal(null, "TestSignal4"));
}