Search in sources :

Example 11 with Barrier

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));
    }
}
Also used : HashMap(java.util.HashMap) EventHandler(org.osgi.service.event.EventHandler) Barrier(org.apache.sling.event.impl.Barrier) JobManager(org.apache.sling.event.jobs.JobManager) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) Event(org.osgi.service.event.Event) JobConsumer(org.apache.sling.event.jobs.consumer.JobConsumer) Job(org.apache.sling.event.jobs.Job) Queue(org.apache.sling.event.jobs.Queue) HashSet(java.util.HashSet) Test(org.junit.Test)

Aggregations

Barrier (org.apache.sling.event.impl.Barrier)11 Job (org.apache.sling.event.jobs.Job)11 Test (org.junit.Test)11 JobConsumer (org.apache.sling.event.jobs.consumer.JobConsumer)7 JobManager (org.apache.sling.event.jobs.JobManager)6 Event (org.osgi.service.event.Event)5 EventHandler (org.osgi.service.event.EventHandler)5 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)4 JobExecutionContext (org.apache.sling.event.jobs.consumer.JobExecutionContext)4 JobExecutionResult (org.apache.sling.event.jobs.consumer.JobExecutionResult)4 JobExecutor (org.apache.sling.event.jobs.consumer.JobExecutor)4 HashMap (java.util.HashMap)3 Queue (org.apache.sling.event.jobs.Queue)3 HashSet (java.util.HashSet)2 ArrayList (java.util.ArrayList)1 Map (java.util.Map)1