Search in sources :

Example 6 with Barrier

use of org.apache.sling.event.impl.Barrier in project sling by apache.

the class JobHandlingTest method testSimpleJobExecutionUsingJobConsumer.

/**
     * Test simple job execution.
     * The job is executed once and finished successfully.
     */
@Test(timeout = DEFAULT_TEST_TIMEOUT)
public void testSimpleJobExecutionUsingJobConsumer() throws Exception {
    final Barrier cb = new Barrier(2);
    this.registerJobConsumer(TOPIC, new JobConsumer() {

        @Override
        public JobResult process(final Job job) {
            cb.block();
            return JobResult.OK;
        }
    });
    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));
}
Also used : Barrier(org.apache.sling.event.impl.Barrier) JobConsumer(org.apache.sling.event.jobs.consumer.JobConsumer) Job(org.apache.sling.event.jobs.Job) Test(org.junit.Test)

Example 7 with Barrier

use of org.apache.sling.event.impl.Barrier in project sling by apache.

the class OrderedQueueTest method testOrderedQueue.

/**
     * Ordered Queue Test
     */
@Test(timeout = DEFAULT_TEST_TIMEOUT)
public void testOrderedQueue() throws Exception {
    final JobManager jobManager = this.getJobManager();
    // register consumer and event handler
    final Barrier cb = new Barrier(2);
    final AtomicInteger count = new AtomicInteger(0);
    final AtomicInteger parallelCount = new AtomicInteger(0);
    this.registerJobConsumer("sling/orderedtest/*", new JobConsumer() {

        private volatile int lastCounter = -1;

        @Override
        public JobResult process(final Job job) {
            final int counter = job.getProperty("counter", -10);
            assertNotEquals("Counter property is missing", -10, counter);
            assertTrue("Counter should only increment by max of 1 " + counter + " - " + lastCounter, counter == lastCounter || counter == lastCounter + 1);
            lastCounter = counter;
            if ("sling/orderedtest/start".equals(job.getTopic())) {
                cb.block();
                return JobResult.OK;
            }
            if (parallelCount.incrementAndGet() > 1) {
                parallelCount.decrementAndGet();
                return JobResult.FAILED;
            }
            final String topic = job.getTopic();
            if (topic.endsWith("sub1")) {
                final int i = (Integer) job.getProperty(Job.PROPERTY_JOB_RETRY_COUNT);
                if (i == 0) {
                    parallelCount.decrementAndGet();
                    return JobResult.FAILED;
                }
            }
            try {
                Thread.sleep(30);
            } catch (InterruptedException ie) {
            // ignore
            }
            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
    final Map<String, Object> properties = new HashMap<String, Object>();
    properties.put("counter", -1);
    jobManager.addJob("sling/orderedtest/start", properties);
    assertTrue("No event received in the given time.", cb.block(5));
    cb.reset();
    // get the queue
    final Queue q = jobManager.getQueue("orderedtest");
    assertNotNull("Queue 'orderedtest' should exist!", q);
    // suspend it
    q.suspend();
    final int NUM_JOBS = 30;
    // we start "some" jobs:
    for (int i = 0; i < NUM_JOBS; i++) {
        final String subTopic = "sling/orderedtest/sub" + (i % 10);
        properties.clear();
        properties.put("counter", i);
        jobManager.addJob(subTopic, properties);
    }
    // start the queue
    q.resume();
    while (count.get() < NUM_JOBS + 1) {
        try {
            Thread.sleep(500);
        } catch (InterruptedException ie) {
        // ignore
        }
    }
    // 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", NUM_JOBS / 10, q.getStatistics().getNumberOfFailedJobs());
    assertEquals("Cancelled count", 0, q.getStatistics().getNumberOfCancelledJobs());
}
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) 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) Test(org.junit.Test)

Example 8 with Barrier

use of org.apache.sling.event.impl.Barrier 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();
}
Also used : JobExecutionResult(org.apache.sling.event.jobs.consumer.JobExecutionResult) JobExecutor(org.apache.sling.event.jobs.consumer.JobExecutor) JobExecutionContext(org.apache.sling.event.jobs.consumer.JobExecutionContext) Barrier(org.apache.sling.event.impl.Barrier) Job(org.apache.sling.event.jobs.Job) Test(org.junit.Test)

Example 9 with Barrier

use of org.apache.sling.event.impl.Barrier in project sling by apache.

the class JobHandlingTest method testCancelJob.

/**
     * Test canceling a job
     * The job execution always fails
     */
@Test(timeout = DEFAULT_TEST_TIMEOUT)
public void testCancelJob() 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.FAILED;
        }
    });
    final Map<String, Object> jobProperties = Collections.singletonMap("id", (Object) "cancelJobId");
    @SuppressWarnings("unchecked") final Map<String, Object>[] jobPropertiesAsArray = new Map[1];
    jobPropertiesAsArray[0] = jobProperties;
    // create job
    final JobManager jobManager = this.getJobManager();
    jobManager.addJob(TOPIC, jobProperties);
    cb.block();
    assertEquals(1, jobManager.findJobs(JobManager.QueryType.ALL, TOPIC, -1, jobPropertiesAsArray).size());
    // job is currently waiting, therefore cancel fails
    final Job e1 = jobManager.getJob(TOPIC, jobProperties);
    assertNotNull(e1);
    // and continue job
    cb2.block();
    sleep(200);
    // the job is now in the queue again
    final Job e2 = jobManager.getJob(TOPIC, jobProperties);
    assertNotNull(e2);
    assertTrue(jobManager.removeJobById(e2.getId()));
    assertEquals(0, jobManager.findJobs(JobManager.QueryType.ALL, TOPIC, -1, jobPropertiesAsArray).size());
    final Collection<Job> col = jobManager.findJobs(JobManager.QueryType.HISTORY, TOPIC, -1, jobPropertiesAsArray);
    try {
        assertEquals(1, col.size());
    } finally {
        for (final Job j : col) {
            jobManager.removeJobById(j.getId());
        }
    }
}
Also used : Barrier(org.apache.sling.event.impl.Barrier) JobManager(org.apache.sling.event.jobs.JobManager) JobConsumer(org.apache.sling.event.jobs.consumer.JobConsumer) Job(org.apache.sling.event.jobs.Job) Map(java.util.Map) Test(org.junit.Test)

Example 10 with Barrier

use of org.apache.sling.event.impl.Barrier 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));
}
Also used : JobExecutionResult(org.apache.sling.event.jobs.consumer.JobExecutionResult) JobExecutor(org.apache.sling.event.jobs.consumer.JobExecutor) JobExecutionContext(org.apache.sling.event.jobs.consumer.JobExecutionContext) Barrier(org.apache.sling.event.impl.Barrier) Job(org.apache.sling.event.jobs.Job) 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