use of org.apache.sling.event.jobs.consumer.JobExecutionResult in project sling by apache.
the class TopicMatchingTest method testDeepMatching.
/**
* Test deep pattern matching /**
*/
@Test(timeout = DEFAULT_TEST_TIMEOUT)
public void testDeepMatching() throws Exception {
final Barrier barrier = new Barrier(2);
this.registerJobExecutor("sling/**", new JobExecutor() {
@Override
public JobExecutionResult process(final Job job, final JobExecutionContext context) {
return context.result().succeeded();
}
});
this.registerEventHandler(NotificationConstants.TOPIC_JOB_FINISHED, new EventHandler() {
@Override
public void handleEvent(final Event event) {
barrier.block();
}
});
this.getJobManager().addJob(TOPIC, null);
barrier.block();
}
use of org.apache.sling.event.jobs.consumer.JobExecutionResult in project sling by apache.
the class TopicMatchingTest method testSimpleMatching.
/**
* Test simple pattern matching /*
*/
@Test(timeout = DEFAULT_TEST_TIMEOUT)
public void testSimpleMatching() throws Exception {
final Barrier barrier = new Barrier(2);
this.registerJobExecutor("sling/test/*", new JobExecutor() {
@Override
public JobExecutionResult process(final Job job, final JobExecutionContext context) {
return context.result().succeeded();
}
});
this.registerEventHandler(NotificationConstants.TOPIC_JOB_FINISHED, new EventHandler() {
@Override
public void handleEvent(final Event event) {
barrier.block();
}
});
this.getJobManager().addJob(TOPIC, null);
barrier.block();
}
use of org.apache.sling.event.jobs.consumer.JobExecutionResult in project sling by apache.
the class HistoryTest method testHistory.
/**
* Test history.
* Start 10 jobs and cancel some of them and succeed others
*/
@Test(timeout = DEFAULT_TEST_TIMEOUT)
public void testHistory() throws Exception {
this.registerJobExecutor(TOPIC, new JobExecutor() {
@Override
public JobExecutionResult process(final Job job, final JobExecutionContext context) {
sleep(5L);
final long count = job.getProperty(PROP_COUNTER, Long.class);
if (count == 2 || count == 5 || count == 7) {
return context.result().message(Job.JobState.ERROR.name()).cancelled();
}
return context.result().message(Job.JobState.SUCCEEDED.name()).succeeded();
}
});
for (int i = 0; i < 10; i++) {
this.addJob(i);
}
this.sleep(200L);
while (this.getJobManager().findJobs(JobManager.QueryType.HISTORY, TOPIC, -1, (Map<String, Object>[]) null).size() < 10) {
this.sleep(20L);
}
Collection<Job> col = this.getJobManager().findJobs(JobManager.QueryType.HISTORY, TOPIC, -1, (Map<String, Object>[]) null);
assertEquals(10, col.size());
assertEquals(0, this.getJobManager().findJobs(JobManager.QueryType.ACTIVE, TOPIC, -1, (Map<String, Object>[]) null).size());
assertEquals(0, this.getJobManager().findJobs(JobManager.QueryType.QUEUED, TOPIC, -1, (Map<String, Object>[]) null).size());
assertEquals(0, this.getJobManager().findJobs(JobManager.QueryType.ALL, TOPIC, -1, (Map<String, Object>[]) null).size());
assertEquals(3, this.getJobManager().findJobs(JobManager.QueryType.CANCELLED, TOPIC, -1, (Map<String, Object>[]) null).size());
assertEquals(0, this.getJobManager().findJobs(JobManager.QueryType.DROPPED, TOPIC, -1, (Map<String, Object>[]) null).size());
assertEquals(3, this.getJobManager().findJobs(JobManager.QueryType.ERROR, TOPIC, -1, (Map<String, Object>[]) null).size());
assertEquals(0, this.getJobManager().findJobs(JobManager.QueryType.GIVEN_UP, TOPIC, -1, (Map<String, Object>[]) null).size());
assertEquals(0, this.getJobManager().findJobs(JobManager.QueryType.STOPPED, TOPIC, -1, (Map<String, Object>[]) null).size());
assertEquals(7, this.getJobManager().findJobs(JobManager.QueryType.SUCCEEDED, TOPIC, -1, (Map<String, Object>[]) null).size());
// find all topics
assertEquals(7, this.getJobManager().findJobs(JobManager.QueryType.SUCCEEDED, null, -1, (Map<String, Object>[]) null).size());
// verify order, message and state
long last = 9;
for (final Job j : col) {
assertNotNull(j.getFinishedDate());
final long count = j.getProperty(PROP_COUNTER, Long.class);
assertEquals(last, count);
if (count == 2 || count == 5 || count == 7) {
assertEquals(Job.JobState.ERROR, j.getJobState());
} else {
assertEquals(Job.JobState.SUCCEEDED, j.getJobState());
}
assertEquals(j.getJobState().name(), j.getResultMessage());
last--;
}
}
use of org.apache.sling.event.jobs.consumer.JobExecutionResult in project sling by apache.
the class TopicMatchingTest method testOrdering.
/**
* Test ordering of matchers
*/
@Test(timeout = DEFAULT_TEST_TIMEOUT)
public void testOrdering() throws Exception {
final Barrier barrier1 = new Barrier(2);
final Barrier barrier2 = new Barrier(2);
final Barrier barrier3 = new Barrier(2);
this.registerJobExecutor("sling/**", new JobExecutor() {
@Override
public JobExecutionResult process(final Job job, final JobExecutionContext context) {
barrier1.block();
return context.result().succeeded();
}
});
final ServiceRegistration<JobExecutor> reg2 = this.registerJobExecutor("sling/test/*", new JobExecutor() {
@Override
public JobExecutionResult process(final Job job, final JobExecutionContext context) {
barrier2.block();
return context.result().succeeded();
}
});
final ServiceRegistration<JobExecutor> reg3 = this.registerJobExecutor(TOPIC, new JobExecutor() {
@Override
public JobExecutionResult process(final Job job, final JobExecutionContext context) {
barrier3.block();
return context.result().succeeded();
}
});
// first test, all three registered, reg3 should get the precedence
this.getJobManager().addJob(TOPIC, null);
barrier3.block();
// second test, unregister reg3, now it should be reg2
long cc = this.getConsumerChangeCount();
this.unregister(reg3);
this.waitConsumerChangeCount(cc + 1);
this.getJobManager().addJob(TOPIC, null);
barrier2.block();
// third test, unregister reg2, reg1 is now the only one
cc = this.getConsumerChangeCount();
this.unregister(reg2);
this.waitConsumerChangeCount(cc + 1);
this.getJobManager().addJob(TOPIC, null);
barrier1.block();
}
use of org.apache.sling.event.jobs.consumer.JobExecutionResult in project sling by apache.
the class JobHandlingTest method testSimpleJobExecutionUsingJobExecutor.
/**
* Test simple job execution.
* The job is executed once and finished successfully.
*/
@Test(timeout = DEFAULT_TEST_TIMEOUT)
public void testSimpleJobExecutionUsingJobExecutor() throws Exception {
final Barrier cb = new Barrier(2);
this.registerJobExecutor(TOPIC, new JobExecutor() {
@Override
public JobExecutionResult process(final Job job, final JobExecutionContext context) {
cb.block();
return context.result().succeeded();
}
});
this.getJobManager().addJob(TOPIC, null);
assertTrue("No event received in the given time.", cb.block(5));
cb.reset();
assertFalse("Unexpected event received in the given time.", cb.block(5));
}
Aggregations