Search in sources :

Example 1 with JobExecutionResult

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();
}
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) EventHandler(org.osgi.service.event.EventHandler) Event(org.osgi.service.event.Event) Barrier(org.apache.sling.event.impl.Barrier) Job(org.apache.sling.event.jobs.Job) Test(org.junit.Test)

Example 2 with JobExecutionResult

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();
}
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) EventHandler(org.osgi.service.event.EventHandler) Event(org.osgi.service.event.Event) Barrier(org.apache.sling.event.impl.Barrier) Job(org.apache.sling.event.jobs.Job) Test(org.junit.Test)

Example 3 with JobExecutionResult

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--;
    }
}
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) Job(org.apache.sling.event.jobs.Job) HashMap(java.util.HashMap) Map(java.util.Map) Test(org.junit.Test)

Example 4 with JobExecutionResult

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();
}
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 5 with JobExecutionResult

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));
}
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

Job (org.apache.sling.event.jobs.Job)5 JobExecutionContext (org.apache.sling.event.jobs.consumer.JobExecutionContext)5 JobExecutionResult (org.apache.sling.event.jobs.consumer.JobExecutionResult)5 JobExecutor (org.apache.sling.event.jobs.consumer.JobExecutor)5 Test (org.junit.Test)5 Barrier (org.apache.sling.event.impl.Barrier)4 Event (org.osgi.service.event.Event)2 EventHandler (org.osgi.service.event.EventHandler)2 HashMap (java.util.HashMap)1 Map (java.util.Map)1