use of org.apache.sling.event.impl.Barrier 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.impl.Barrier 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.impl.Barrier in project sling by apache.
the class UnorderedQueueTest method testUnorderedQueue.
@Test(timeout = DEFAULT_TEST_TIMEOUT)
public void testUnorderedQueue() throws Exception {
final JobManager jobManager = this.getJobManager();
final Barrier cb = new Barrier(2);
this.registerJobConsumer(TOPIC + "/start", new JobConsumer() {
@Override
public JobResult process(final Job job) {
cb.block();
return JobResult.OK;
}
});
// register new consumer and event handle
final AtomicInteger count = new AtomicInteger(0);
final AtomicInteger parallelCount = new AtomicInteger(0);
final Set<Integer> maxParticipants = new HashSet<Integer>();
this.registerJobConsumer(TOPIC + "/*", new JobConsumer() {
@Override
public JobResult process(final Job job) {
final int max = parallelCount.incrementAndGet();
if (max > MAX_PAR) {
parallelCount.decrementAndGet();
return JobResult.FAILED;
}
synchronized (maxParticipants) {
maxParticipants.add(max);
}
sleep(job.getProperty("sleep", 30));
parallelCount.decrementAndGet();
return JobResult.OK;
}
});
this.registerEventHandler(NotificationConstants.TOPIC_JOB_FINISHED, new EventHandler() {
@Override
public void handleEvent(final Event event) {
count.incrementAndGet();
}
});
// we first sent one event to get the queue started
jobManager.addJob(TOPIC + "/start", null);
assertTrue("No event received in the given time.", cb.block(5));
cb.reset();
// get the queue
final Queue q = jobManager.getQueue(QUEUE_NAME);
assertNotNull("Queue '" + QUEUE_NAME + "' should exist!", q);
// suspend it
q.suspend();
// we start "some" jobs:
for (int i = 0; i < NUM_JOBS; i++) {
final String subTopic = TOPIC + "/sub" + (i % 10);
final Map<String, Object> props = new HashMap<String, Object>();
if (i < 10) {
props.put("sleep", 300);
} else {
props.put("sleep", 30);
}
jobManager.addJob(subTopic, props);
}
// start the queue
q.resume();
while (count.get() < NUM_JOBS + 1) {
assertEquals("Failed count", 0, q.getStatistics().getNumberOfFailedJobs());
assertEquals("Cancelled count", 0, q.getStatistics().getNumberOfCancelledJobs());
sleep(300);
}
// we started one event before the test, so add one
assertEquals("Finished count", NUM_JOBS + 1, count.get());
assertEquals("Finished count", NUM_JOBS + 1, jobManager.getStatistics().getNumberOfFinishedJobs());
assertEquals("Finished count", NUM_JOBS + 1, q.getStatistics().getNumberOfFinishedJobs());
assertEquals("Failed count", 0, q.getStatistics().getNumberOfFailedJobs());
assertEquals("Cancelled count", 0, q.getStatistics().getNumberOfCancelledJobs());
for (int i = 1; i <= MAX_PAR; i++) {
assertTrue("# Participants " + String.valueOf(i) + " not in " + maxParticipants, maxParticipants.contains(i));
}
}
use of org.apache.sling.event.impl.Barrier in project sling by apache.
the class JobHandlingTest method testStartJobAndReschedule.
/**
* Reschedule test.
* The job is rescheduled two times before it fails.
*/
@Test(timeout = DEFAULT_TEST_TIMEOUT)
public void testStartJobAndReschedule() throws Exception {
final List<Integer> retryCountList = new ArrayList<Integer>();
final Barrier cb = new Barrier(2);
this.registerJobConsumer(TOPIC, new JobConsumer() {
int retryCount;
@Override
public JobResult process(Job job) {
int retry = 0;
if (job.getProperty(Job.PROPERTY_JOB_RETRY_COUNT) != null) {
retry = (Integer) job.getProperty(Job.PROPERTY_JOB_RETRY_COUNT);
}
if (retry == retryCount) {
retryCountList.add(retry);
}
retryCount++;
cb.block();
return JobResult.FAILED;
}
});
final JobManager jobManager = this.getJobManager();
final Job job = jobManager.addJob(TOPIC, null);
assertTrue("No event received in the given time.", cb.block(5));
cb.reset();
// the job is retried after two seconds, so we wait again
assertTrue("No event received in the given time.", cb.block(5));
cb.reset();
// the job is retried after two seconds, so we wait again
assertTrue("No event received in the given time.", cb.block(5));
// we have reached the retry so we expect to not get an event
cb.reset();
assertFalse("Unexpected event received in the given time.", cb.block(5));
assertEquals("Unexpected number of retries", 3, retryCountList.size());
jobManager.removeJobById(job.getId());
}
use of org.apache.sling.event.impl.Barrier in project sling by apache.
the class JobHandlingTest method testGetJob.
/**
* Test get a job
*/
@Test(timeout = DEFAULT_TEST_TIMEOUT)
public void testGetJob() throws Exception {
final Barrier cb = new Barrier(2);
final Barrier cb2 = new Barrier(2);
this.registerJobConsumer(TOPIC, new JobConsumer() {
@Override
public JobResult process(Job job) {
cb.block();
cb2.block();
return JobResult.OK;
}
});
final JobManager jobManager = this.getJobManager();
final Job j = jobManager.addJob(TOPIC, null);
cb.block();
assertNotNull(jobManager.getJob(TOPIC, null));
// and continue job
cb2.block();
jobManager.removeJobById(j.getId());
}
Aggregations