public void testNotifications() throws Exception {
final List<String> cancelled = Collections.synchronizedList(new ArrayList<String>());
final List<String> failed = Collections.synchronizedList(new ArrayList<String>());
final List<String> finished = Collections.synchronizedList(new ArrayList<String>());
final List<String> started = Collections.synchronizedList(new ArrayList<String>());
final ServiceRegistration jcReg = this.registerJobConsumer(TOPIC,
new JobConsumer() {
@Override
public JobResult process(Job job) {
// events 1 and 4 finish the first time
final String id = (String)job.getProperty("id");
if ( "1".equals(id) || "4".equals(id) ) {
return JobResult.OK;
// 5 fails always
} else if ( "5".equals(id) ) {
return JobResult.FAILED;
} else {
int retry = 0;
if ( job.getProperty(Job.PROPERTY_JOB_RETRY_COUNT) != null ) {
retry = (Integer)job.getProperty(Job.PROPERTY_JOB_RETRY_COUNT);
}
// 2 fails the first time
if ( "2".equals(id) ) {
if ( retry == 0 ) {
return JobResult.FAILED;
} else {
return JobResult.OK;
}
}
// 3 fails the first and second time
if ( "3".equals(id) ) {
if ( retry == 0 || retry == 1 ) {
return JobResult.FAILED;
} else {
return JobResult.OK;
}
}
}
return JobResult.FAILED;
}
});
final ServiceRegistration eh1Reg = this.registerEventHandler(NotificationConstants.TOPIC_JOB_CANCELLED,
new EventHandler() {
@Override
public void handleEvent(Event event) {
final String id = (String)event.getProperty("id");
cancelled.add(id);
}
});
final ServiceRegistration eh2Reg = this.registerEventHandler(NotificationConstants.TOPIC_JOB_FAILED,
new EventHandler() {
@Override
public void handleEvent(Event event) {
final String id = (String)event.getProperty("id");
failed.add(id);
}
});
final ServiceRegistration eh3Reg = this.registerEventHandler(NotificationConstants.TOPIC_JOB_FINISHED,
new EventHandler() {
@Override
public void handleEvent(Event event) {
final String id = (String)event.getProperty("id");
finished.add(id);
}
});
final ServiceRegistration eh4Reg = this.registerEventHandler(NotificationConstants.TOPIC_JOB_STARTED,
new EventHandler() {
@Override
public void handleEvent(Event event) {
final String id = (String)event.getProperty("id");
started.add(id);
}
});
final JobManager jobManager = this.getJobManager();
try {
jobManager.addJob(TOPIC, Collections.singletonMap("id", (Object)"1"));
jobManager.addJob(TOPIC, Collections.singletonMap("id", (Object)"2"));
jobManager.addJob(TOPIC, Collections.singletonMap("id", (Object)"3"));
jobManager.addJob(TOPIC, Collections.singletonMap("id", (Object)"4"));
jobManager.addJob(TOPIC, Collections.singletonMap("id", (Object)"5"));
int count = 0;
final long startTime = System.currentTimeMillis();
do {
count = finished.size() + cancelled.size();
// after 25 seconds we cancel the test
if ( System.currentTimeMillis() - startTime > 25000 ) {
throw new Exception("Timeout during notification test.");
}
} while ( count < 5 || started.size() < 10 );
assertEquals("Finished count", 4, finished.size());
assertEquals("Cancelled count", 1, cancelled.size());
assertEquals("Started count", 10, started.size());
assertEquals("Failed count", 5, failed.size());
} finally {
final Collection<Job> col = jobManager.findJobs(JobManager.QueryType.HISTORY, "sling/test", -1, (Map<String, Object>[])null);
for(final Job j : col) {
jobManager.removeJobById(j.getId());
}
jcReg.unregister();
eh1Reg.unregister();
eh2Reg.unregister();
eh3Reg.unregister();
eh4Reg.unregister();
}
}