use of org.apache.sling.event.impl.Barrier in project sling by apache.
the class RoundRobinQueueTest method testRoundRobinQueue.
@Test(timeout = DEFAULT_TEST_TIMEOUT)
public void testRoundRobinQueue() 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));
}
}
Aggregations