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.RegexQNamePattern;
import org.alfresco.service.transaction.TransactionService;
import org.alfresco.repo.transaction.RetryingTransactionHelper.RetryingTransactionCallback;
import org.alfresco.util.ApplicationContextHelper;
import org.quartz.DateIntervalTrigger;
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
{
@@ -709,17 +711,18 @@ public class ScheduledPersistedActionServiceTest extends TestCase
sleepActionExec.resetTimesExecuted();
sleepActionExec.setSleepMs(1);
ScheduledPersistedAction schedule;
// This test needs the scheduler running properly
scheduler.start();
// 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);
transactionService.getRetryingTransactionHelper().doInTransaction(new RetryingTransactionCallback<Void>() {
public Void execute() throws Throwable {
// A job due to start in 1 second, and run once
schedule = service.createSchedule(testAction);
ScheduledPersistedAction schedule = service.createSchedule(testAction);
schedule.setScheduleStart(new Date(System.currentTimeMillis() + 1000));
assertNull(schedule.getScheduleInterval());
assertNull(schedule.getScheduleIntervalCount());
@@ -729,6 +732,10 @@ public class ScheduledPersistedActionServiceTest extends TestCase
System.out.println("Job starts in 1 second, no repeat...");
service.saveSchedule(schedule);
return null;
}
}, false, true);
// Check it went in
assertEquals(1, scheduler.getJobNames(ScheduledPersistedActionServiceImpl.SCHEDULER_GROUP).length);
@@ -741,19 +748,32 @@ public class ScheduledPersistedActionServiceTest extends TestCase
// Should have removed itself now the schedule is over
assertEquals(0, scheduler.getJobNames(ScheduledPersistedActionServiceImpl.SCHEDULER_GROUP).length);
transactionService.getRetryingTransactionHelper().doInTransaction(new RetryingTransactionCallback<Void>() {
public Void execute() throws Throwable {
// Ensure it was tagged with when it ran
schedule = service.getSchedule(testAction);
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);
// ==========================
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)
schedule = service.createSchedule(testAction);
ScheduledPersistedAction schedule = service.createSchedule(testAction);
schedule.setScheduleStart(new Date(0));
((ScheduledPersistedActionImpl) schedule).setScheduleEnd(new Date(System.currentTimeMillis() + 3500));
schedule.setScheduleIntervalCount(2);
@@ -767,7 +787,14 @@ public class ScheduledPersistedActionServiceTest extends TestCase
System.out.println("Job starts now, repeats twice @ 2s");
service.saveSchedule(schedule);
Thread.sleep(4200);
return null;
}
}, false, true);
transactionService.getRetryingTransactionHelper().doInTransaction(new RetryingTransactionCallback<Void>() {
public Void execute() throws Throwable {
Thread.sleep(4500);
ScheduledPersistedAction schedule = service.getSchedule(testAction);
// Ensure it did properly run two times
// (Depending on timing of tests, might actually slip in 3 runs)
@@ -781,11 +808,20 @@ public class ScheduledPersistedActionServiceTest extends TestCase
service.deleteSchedule(schedule);
assertEquals(0, scheduler.getJobNames(ScheduledPersistedActionServiceImpl.SCHEDULER_GROUP).length);
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
schedule = service.createSchedule(testAction);
ScheduledPersistedAction schedule = service.createSchedule(testAction);
schedule.setScheduleStart(new Date(System.currentTimeMillis() + 2000));
schedule.setScheduleIntervalCount(1);
schedule.setScheduleIntervalPeriod(IntervalPeriod.Second);
@@ -798,10 +834,17 @@ public class ScheduledPersistedActionServiceTest extends TestCase
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.getJobNames(ScheduledPersistedActionServiceImpl.SCHEDULER_GROUP).length);
service.deleteSchedule(schedule);
assertEquals(0, scheduler.getJobNames(ScheduledPersistedActionServiceImpl.SCHEDULER_GROUP).length);
@@ -814,33 +857,79 @@ public class ScheduledPersistedActionServiceTest extends TestCase
// 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
* 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
scheduler.start();
final SleepActionExecuter sleepActionExec = (SleepActionExecuter) ctx.getBean(SleepActionExecuter.NAME);
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
// 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
// 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
// TODO
scheduler.start();
// Wait for 10 seconds for them to run and finish
Thread.sleep(10*1000);
// Wait
// TODO
// 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);
}
// ============================================================================