Parallel scheduled action execution unit tests (ALF-4505)

Also tweak the execution related unit test transactional code as advised by Derek


git-svn-id: https://svn.alfresco.com/repos/alfresco-enterprise/alfresco/HEAD/root@22102 c4b6b30b-aa2e-2d43-bbcb-ca4b014f7261
This commit is contained in:
Nick Burch
2010-08-31 17:06:26 +00:00
parent 8de08f878f
commit bb53fd517b

View File

@@ -40,6 +40,7 @@ import org.alfresco.service.cmr.repository.NodeService;
import org.alfresco.service.namespace.QName; import org.alfresco.service.namespace.QName;
import org.alfresco.service.namespace.RegexQNamePattern; import org.alfresco.service.namespace.RegexQNamePattern;
import org.alfresco.service.transaction.TransactionService; import org.alfresco.service.transaction.TransactionService;
import org.alfresco.repo.transaction.RetryingTransactionHelper.RetryingTransactionCallback;
import org.alfresco.util.ApplicationContextHelper; import org.alfresco.util.ApplicationContextHelper;
import org.quartz.DateIntervalTrigger; import org.quartz.DateIntervalTrigger;
import org.quartz.Job; import org.quartz.Job;
@@ -701,7 +702,8 @@ public class ScheduledPersistedActionServiceTest extends TestCase
} }
/** /**
* Tests that things actually get run correctly * Tests that things actually get run correctly.
* Each sub-test runs in its own transaction
*/ */
public void testExecution() throws Exception public void testExecution() throws Exception
{ {
@@ -709,25 +711,30 @@ public class ScheduledPersistedActionServiceTest extends TestCase
sleepActionExec.resetTimesExecuted(); sleepActionExec.resetTimesExecuted();
sleepActionExec.setSleepMs(1); sleepActionExec.setSleepMs(1);
ScheduledPersistedAction schedule;
// This test needs the scheduler running properly // This test needs the scheduler running properly
scheduler.start(); scheduler.start();
// Until the schedule is persisted, nothing will happen // Until the schedule is persisted, nothing will happen
schedule = service.createSchedule(testAction); ScheduledPersistedAction schedule = service.createSchedule(testAction);
assertEquals(0, scheduler.getJobNames(ScheduledPersistedActionServiceImpl.SCHEDULER_GROUP).length); assertEquals(0, scheduler.getJobNames(ScheduledPersistedActionServiceImpl.SCHEDULER_GROUP).length);
// A job due to start in 1 second, and run once transactionService.getRetryingTransactionHelper().doInTransaction(new RetryingTransactionCallback<Void>() {
schedule = service.createSchedule(testAction); public Void execute() throws Throwable {
schedule.setScheduleStart(new Date(System.currentTimeMillis() + 1000));
assertNull(schedule.getScheduleInterval()); // A job due to start in 1 second, and run once
assertNull(schedule.getScheduleIntervalCount()); ScheduledPersistedAction schedule = service.createSchedule(testAction);
assertNull(schedule.getScheduleIntervalPeriod()); schedule.setScheduleStart(new Date(System.currentTimeMillis() + 1000));
assertNull(schedule.getScheduleLastExecutedAt()); assertNull(schedule.getScheduleInterval());
assertNull(schedule.getScheduleIntervalCount());
assertNull(schedule.getScheduleIntervalPeriod());
assertNull(schedule.getScheduleLastExecutedAt());
System.out.println("Job starts in 1 second, no repeat..."); System.out.println("Job starts in 1 second, no repeat...");
service.saveSchedule(schedule); service.saveSchedule(schedule);
return null;
}
}, false, true);
// Check it went in // Check it went in
assertEquals(1, scheduler.getJobNames(ScheduledPersistedActionServiceImpl.SCHEDULER_GROUP).length); assertEquals(1, scheduler.getJobNames(ScheduledPersistedActionServiceImpl.SCHEDULER_GROUP).length);
@@ -741,106 +748,188 @@ public class ScheduledPersistedActionServiceTest extends TestCase
// Should have removed itself now the schedule is over // Should have removed itself now the schedule is over
assertEquals(0, scheduler.getJobNames(ScheduledPersistedActionServiceImpl.SCHEDULER_GROUP).length); assertEquals(0, scheduler.getJobNames(ScheduledPersistedActionServiceImpl.SCHEDULER_GROUP).length);
// Ensure it was tagged with when it ran
schedule = service.getSchedule(testAction);
assertEquals((double)System.currentTimeMillis(), (double)schedule.getScheduleLastExecutedAt().getTime(), 2500); // Within 2.5 secs
// Zap it transactionService.getRetryingTransactionHelper().doInTransaction(new RetryingTransactionCallback<Void>() {
service.deleteSchedule(schedule); public Void execute() throws Throwable {
assertEquals(0, scheduler.getJobNames(ScheduledPersistedActionServiceImpl.SCHEDULER_GROUP).length);
// Ensure it was tagged with when it ran
ScheduledPersistedAction schedule = service.getSchedule(testAction);
assertEquals((double)System.currentTimeMillis(), (double)schedule.getScheduleLastExecutedAt().getTime(), 2500); // Within 2.5 secs
// Zap it
service.deleteSchedule(schedule);
assertEquals(0, scheduler.getJobNames(ScheduledPersistedActionServiceImpl.SCHEDULER_GROUP).length);
return null;
}
}, false, true);
// ========================== // ==========================
// A job that runs every 2 seconds, for the next 3.5 seconds
// (Should get to run twice, now and @2 secs) transactionService.getRetryingTransactionHelper().doInTransaction(new RetryingTransactionCallback<Void>() {
schedule = service.createSchedule(testAction); public Void execute() throws Throwable {
schedule.setScheduleStart(new Date(0));
((ScheduledPersistedActionImpl) schedule).setScheduleEnd(new Date(System.currentTimeMillis() + 3500)); // A job that runs every 2 seconds, for the next 3.5 seconds
schedule.setScheduleIntervalCount(2); // (Should get to run twice, now and @2 secs)
schedule.setScheduleIntervalPeriod(IntervalPeriod.Second); ScheduledPersistedAction schedule = service.createSchedule(testAction);
assertEquals("2Second", schedule.getScheduleInterval()); schedule.setScheduleStart(new Date(0));
((ScheduledPersistedActionImpl) schedule).setScheduleEnd(new Date(System.currentTimeMillis() + 3500));
// Reset count schedule.setScheduleIntervalCount(2);
sleepActionExec.resetTimesExecuted(); schedule.setScheduleIntervalPeriod(IntervalPeriod.Second);
assertEquals(0, sleepActionExec.getTimesExecuted()); assertEquals("2Second", schedule.getScheduleInterval());
System.out.println("Job starts now, repeats twice @ 2s"); // Reset count
service.saveSchedule(schedule); sleepActionExec.resetTimesExecuted();
assertEquals(0, sleepActionExec.getTimesExecuted());
Thread.sleep(4200);
System.out.println("Job starts now, repeats twice @ 2s");
// Ensure it did properly run two times service.saveSchedule(schedule);
// (Depending on timing of tests, might actually slip in 3 runs)
if(sleepActionExec.getTimesExecuted() == 3) { return null;
assertEquals(3, sleepActionExec.getTimesExecuted()); }
} else { }, false, true);
assertEquals(2, sleepActionExec.getTimesExecuted()); transactionService.getRetryingTransactionHelper().doInTransaction(new RetryingTransactionCallback<Void>() {
} public Void execute() throws Throwable {
Thread.sleep(4500);
// Zap it
service.deleteSchedule(schedule); ScheduledPersistedAction schedule = service.getSchedule(testAction);
assertEquals(0, scheduler.getJobNames(ScheduledPersistedActionServiceImpl.SCHEDULER_GROUP).length);
// Ensure it did properly run two times
// (Depending on timing of tests, might actually slip in 3 runs)
if(sleepActionExec.getTimesExecuted() == 3) {
assertEquals(3, sleepActionExec.getTimesExecuted());
} else {
assertEquals(2, sleepActionExec.getTimesExecuted());
}
// Zap it
service.deleteSchedule(schedule);
assertEquals(0, scheduler.getJobNames(ScheduledPersistedActionServiceImpl.SCHEDULER_GROUP).length);
return null;
}
}, false, true);
// ========================== // ==========================
// A job that starts in 2 seconds time, and runs
// every second until we kill it transactionService.getRetryingTransactionHelper().doInTransaction(new RetryingTransactionCallback<Void>() {
schedule = service.createSchedule(testAction); public Void execute() throws Throwable {
schedule.setScheduleStart(new Date(System.currentTimeMillis() + 2000));
schedule.setScheduleIntervalCount(1); // A job that starts in 2 seconds time, and runs
schedule.setScheduleIntervalPeriod(IntervalPeriod.Second); // every second until we kill it
assertEquals("1Second", schedule.getScheduleInterval()); ScheduledPersistedAction schedule = service.createSchedule(testAction);
schedule.setScheduleStart(new Date(System.currentTimeMillis() + 2000));
// Reset count schedule.setScheduleIntervalCount(1);
sleepActionExec.resetTimesExecuted(); schedule.setScheduleIntervalPeriod(IntervalPeriod.Second);
assertEquals(0, sleepActionExec.getTimesExecuted()); assertEquals("1Second", schedule.getScheduleInterval());
System.out.println("Job starts in 2s, repeats @ 1s"); // Reset count
service.saveSchedule(schedule); sleepActionExec.resetTimesExecuted();
assertEquals(0, sleepActionExec.getTimesExecuted());
// Let it run a few times
Thread.sleep(5000); System.out.println("Job starts in 2s, repeats @ 1s");
service.saveSchedule(schedule);
// Zap it - should still be live
assertEquals(1, scheduler.getJobNames(ScheduledPersistedActionServiceImpl.SCHEDULER_GROUP).length); return null;
service.deleteSchedule(schedule); }
assertEquals(0, scheduler.getJobNames(ScheduledPersistedActionServiceImpl.SCHEDULER_GROUP).length); }, false, true);
transactionService.getRetryingTransactionHelper().doInTransaction(new RetryingTransactionCallback<Void>() {
// Check it ran an appropriate number of times public Void execute() throws Throwable {
assertEquals("Didn't run enough - " + sleepActionExec.getTimesExecuted(), true, sleepActionExec
.getTimesExecuted() >= 3); // Let it run a few times
assertEquals("Ran too much - " + sleepActionExec.getTimesExecuted(), true, Thread.sleep(5000);
sleepActionExec.getTimesExecuted() < 5);
// Zap it - should still be live
// Ensure it finished shutting down ScheduledPersistedAction schedule = service.getSchedule(testAction);
Thread.sleep(500); assertEquals(1, scheduler.getJobNames(ScheduledPersistedActionServiceImpl.SCHEDULER_GROUP).length);
service.deleteSchedule(schedule);
assertEquals(0, scheduler.getJobNames(ScheduledPersistedActionServiceImpl.SCHEDULER_GROUP).length);
// 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);
} }
/** /**
* Tests that when we have more than one schedule defined and active, then * Tests that when we have more than one schedule defined and active, then
* the correct things run at the correct times, and we never get confused * the correct things run at the correct times, and we never get confused
*/ */
public void DISABLEDtestMultipleExecutions() throws Exception public void testMultipleExecutions() throws Exception
{ {
// This test needs the scheduler running properly final SleepActionExecuter sleepActionExec = (SleepActionExecuter) ctx.getBean(SleepActionExecuter.NAME);
scheduler.start(); sleepActionExec.resetTimesExecuted();
sleepActionExec.setSleepMs(1);
transactionService.getRetryingTransactionHelper().doInTransaction(new RetryingTransactionCallback<Void>() {
public Void execute() throws Throwable {
// Create one that starts running in 2 seconds, runs every 2 seconds // Create one that starts running in 2 seconds, runs every 2 seconds
// until 9 seconds are up (will run 4 times) // until 9 seconds are up (will run 4 times)
// TODO ScheduledPersistedActionImpl scheduleA = (ScheduledPersistedActionImpl)service.createSchedule(testAction);
scheduleA.setScheduleStart(new Date(System.currentTimeMillis()+2000));
scheduleA.setScheduleEnd(new Date(System.currentTimeMillis()+9000));
scheduleA.setScheduleIntervalCount(2);
scheduleA.setScheduleIntervalPeriod(IntervalPeriod.Second);
service.saveSchedule(scheduleA);
// Create one that starts running now, every second until 9.5 seconds // Create one that starts running now, every second until 9.5 seconds
// are up (will run 9-10 times) // are up (will run 9-10 times)
// TODO ScheduledPersistedActionImpl scheduleB = (ScheduledPersistedActionImpl)service.createSchedule(testAction2);
scheduleB.setScheduleStart(new Date(System.currentTimeMillis()));
scheduleB.setScheduleEnd(new Date(System.currentTimeMillis()+9500));
scheduleB.setScheduleIntervalCount(1);
scheduleB.setScheduleIntervalPeriod(IntervalPeriod.Second);
service.saveSchedule(scheduleB);
return null;
}
}, false, true);
// Set them going // Set them going
// TODO scheduler.start();
// Wait // Wait for 10 seconds for them to run and finish
// TODO Thread.sleep(10*1000);
// Check that they really did run properly // Check that they really did run properly
// TODO transactionService.getRetryingTransactionHelper().doInTransaction(new RetryingTransactionCallback<Void>() {
public Void execute() throws Throwable {
ScheduledPersistedAction scheduleA = service.getSchedule(testAction);
ScheduledPersistedAction scheduleB = service.getSchedule(testAction2);
// Both should have last run at some point in the last second or two
assertEquals((double)System.currentTimeMillis(), (double)scheduleA.getScheduleLastExecutedAt().getTime(), 2500);
assertEquals((double)System.currentTimeMillis(), (double)scheduleB.getScheduleLastExecutedAt().getTime(), 2500);
// A should have run ~4 times
// B should have run ~9 times
assertEquals("Didn't run enough - " + sleepActionExec.getTimesExecuted(), true, sleepActionExec
.getTimesExecuted() >= 11);
assertEquals("Ran too much - " + sleepActionExec.getTimesExecuted(), true,
sleepActionExec.getTimesExecuted() < 16);
return null;
}
}, false, true);
} }
// ============================================================================ // ============================================================================