use of org.alfresco.repo.action.ActionServiceImplTest.SleepActionExecuter in project alfresco-repository by Alfresco.
the class ActionTrackingServiceImplTest method testWorkingActions.
/**
* Working actions go into the cache, then out
*/
public void testWorkingActions() throws Exception {
final SleepActionExecuter sleepActionExec = (SleepActionExecuter) ctx.getBean(SleepActionExecuter.NAME);
sleepActionExec.resetTimesExecuted();
sleepActionExec.setSleepMs(10000);
// Have it run asynchronously
UserTransaction txn = transactionService.getUserTransaction();
txn.begin();
Action action = createWorkingSleepAction("54321");
assertNull(action.getExecutionStartDate());
assertNull(action.getExecutionEndDate());
assertNull(action.getExecutionFailureMessage());
assertEquals(ActionStatus.New, action.getExecutionStatus());
String key = ActionTrackingServiceImpl.generateCacheKey(action);
assertEquals(null, executingActionsCache.get(key));
this.actionService.executeAction(action, this.nodeRef, false, true);
// End the transaction. Should allow the async action
// to start up, and begin sleeping
txn.commit();
Thread.sleep(150);
// The action should now be running
// It will have got an execution instance id, so a new key
key = ActionTrackingServiceImpl.generateCacheKey(action);
// Check it's in the cache
System.out.println("Checking the cache for " + key);
assertNotNull(executingActionsCache.get(key));
ExecutionSummary s = ActionTrackingServiceImpl.buildExecutionSummary(action);
ExecutionDetails d = actionTrackingService.getExecutionDetails(s);
assertNotNull(d.getExecutionSummary());
assertEquals("sleep-action", d.getActionType());
assertEquals("54321", d.getActionId());
assertEquals(1, d.getExecutionInstance());
assertEquals(null, d.getPersistedActionRef());
assertNotNull(null, d.getStartedAt());
// Tell it to stop sleeping
// Then wait for it to finish
asyncOccurs.awaitExecution(null, sleepActionExec.getExecutingThread(), action.getActionDefinitionName());
// Ensure it went away again
assertEquals(ActionStatus.Completed, action.getExecutionStatus());
assertEquals(null, executingActionsCache.get(key));
d = actionTrackingService.getExecutionDetails(s);
assertEquals(null, d);
}
use of org.alfresco.repo.action.ActionServiceImplTest.SleepActionExecuter in project alfresco-repository by Alfresco.
the class ScheduledPersistedActionServiceTest method testExecution.
/**
* Tests that things actually get run correctly. Each sub-test runs in its own transaction
*/
@Test
public void testExecution() throws Exception {
final SleepActionExecuter sleepActionExec = (SleepActionExecuter) applicationContext.getBean(SleepActionExecuter.NAME);
sleepActionExec.resetTimesExecuted();
sleepActionExec.setSleepMs(1);
// This test needs the scheduler running properly
scheduler.start();
// Until the schedule is persisted, nothing will happen
@SuppressWarnings("unused") ScheduledPersistedAction schedule = service.createSchedule(testAction);
assertEquals(0, scheduler.getJobKeys(GroupMatcher.groupEquals(ScheduledPersistedActionServiceImpl.SCHEDULER_GROUP)).size());
transactionService.getRetryingTransactionHelper().doInTransaction(new RetryingTransactionCallback<Void>() {
public Void execute() throws Throwable {
// A job due to start in 1 second, and run once
ScheduledPersistedAction schedule = service.createSchedule(testAction);
schedule.setScheduleStart(new Date(System.currentTimeMillis() + 1000));
assertNull(schedule.getScheduleInterval());
assertNull(schedule.getScheduleIntervalCount());
assertNull(schedule.getScheduleIntervalPeriod());
assertNull(schedule.getScheduleLastExecutedAt());
System.out.println("Job starts in 1 second, no repeat...");
service.saveSchedule(schedule);
return null;
}
}, false, true);
// Check it went in
assertEquals(1, scheduler.getJobKeys(GroupMatcher.groupEquals(ScheduledPersistedActionServiceImpl.SCHEDULER_GROUP)).size());
// Let it run
Thread.sleep(2000);
// Ensure it did properly run the once
assertEquals(1, sleepActionExec.getTimesExecuted());
// Should have removed itself now the schedule is over
assertEquals(0, scheduler.getJobKeys(GroupMatcher.groupEquals(ScheduledPersistedActionServiceImpl.SCHEDULER_GROUP)).size());
transactionService.getRetryingTransactionHelper().doInTransaction(new RetryingTransactionCallback<Void>() {
public Void execute() throws Throwable {
// Ensure it was tagged with when it ran
ScheduledPersistedAction schedule = service.getSchedule(testAction);
assertEquals((double) System.currentTimeMillis(), (double) schedule.getScheduleLastExecutedAt().getTime(), // Within 2.5 secs
2500);
// Zap it
service.deleteSchedule(schedule);
assertEquals(0, scheduler.getJobKeys(GroupMatcher.groupEquals(ScheduledPersistedActionServiceImpl.SCHEDULER_GROUP)).size());
return null;
}
}, false, true);
// ==========================
transactionService.getRetryingTransactionHelper().doInTransaction(new RetryingTransactionCallback<Void>() {
public Void execute() throws Throwable {
// A job that runs every 2 seconds, for the next 3.5 seconds
// (Should get to run twice, now and @2 secs)
ScheduledPersistedAction schedule = service.createSchedule(testAction);
schedule.setScheduleStart(new Date(System.currentTimeMillis() - 50));
((ScheduledPersistedActionImpl) schedule).setScheduleEnd(new Date(System.currentTimeMillis() + 3500));
schedule.setScheduleIntervalCount(2);
schedule.setScheduleIntervalPeriod(IntervalPeriod.Second);
assertEquals("2Second", schedule.getScheduleInterval());
// Reset count
sleepActionExec.resetTimesExecuted();
assertEquals(0, sleepActionExec.getTimesExecuted());
service.saveSchedule(schedule);
System.out.println("Job " + ((ScheduledPersistedActionImpl) schedule).getPersistedAtNodeRef() + " starts now, repeats twice @ 2s");
return null;
}
}, false, true);
transactionService.getRetryingTransactionHelper().doInTransaction(new RetryingTransactionCallback<Void>() {
public Void execute() throws Throwable {
Thread.sleep(4250);
ScheduledPersistedAction schedule = service.getSchedule(testAction);
// runs)
if (sleepActionExec.getTimesExecuted() == 3) {
assertEquals(3, sleepActionExec.getTimesExecuted());
} else {
assertEquals(2, sleepActionExec.getTimesExecuted());
}
// Zap it
service.deleteSchedule(schedule);
assertEquals(0, scheduler.getJobKeys(GroupMatcher.groupEquals(ScheduledPersistedActionServiceImpl.SCHEDULER_GROUP)).size());
return null;
}
}, false, true);
// ==========================
transactionService.getRetryingTransactionHelper().doInTransaction(new RetryingTransactionCallback<Void>() {
public Void execute() throws Throwable {
// A job that starts in 2 seconds time, and runs
// every second until we kill it
ScheduledPersistedAction schedule = service.createSchedule(testAction);
schedule.setScheduleStart(new Date(System.currentTimeMillis() + 2000));
schedule.setScheduleIntervalCount(1);
schedule.setScheduleIntervalPeriod(IntervalPeriod.Second);
assertEquals("1Second", schedule.getScheduleInterval());
// Reset count
sleepActionExec.resetTimesExecuted();
assertEquals(0, sleepActionExec.getTimesExecuted());
System.out.println("Job starts in 2s, repeats @ 1s");
service.saveSchedule(schedule);
return null;
}
}, false, true);
transactionService.getRetryingTransactionHelper().doInTransaction(new RetryingTransactionCallback<Void>() {
public Void execute() throws Throwable {
// Let it run a few times
Thread.sleep(5000);
// Zap it - should still be live
ScheduledPersistedAction schedule = service.getSchedule(testAction);
assertEquals(1, scheduler.getJobKeys(GroupMatcher.groupEquals(ScheduledPersistedActionServiceImpl.SCHEDULER_GROUP)).size());
service.deleteSchedule(schedule);
assertEquals(0, scheduler.getJobKeys(GroupMatcher.groupEquals(ScheduledPersistedActionServiceImpl.SCHEDULER_GROUP)).size());
// Check it ran an appropriate number of times
assertEquals("Didn't run enough - " + sleepActionExec.getTimesExecuted(), true, sleepActionExec.getTimesExecuted() >= 3);
assertEquals("Ran too much - " + sleepActionExec.getTimesExecuted(), true, sleepActionExec.getTimesExecuted() < 5);
// Ensure it finished shutting down
Thread.sleep(500);
return null;
}
}, false, true);
}
Aggregations