Search in sources :

Example 1 with JobExecutor

use of org.apache.sling.event.jobs.consumer.JobExecutor 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 JobExecutor

use of org.apache.sling.event.jobs.consumer.JobExecutor 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 JobExecutor

use of org.apache.sling.event.jobs.consumer.JobExecutor in project sling by apache.

the class JobConsumerManagerTest method testCategoryMappingExecutor.

@Test
public void testCategoryMappingExecutor() {
    final BundleContext bc = Mockito.mock(BundleContext.class);
    final JobConsumerManager jcs = new JobConsumerManager();
    jcs.activate(bc, getDefaultConfig());
    final JobExecutor jc1 = Mockito.mock(JobExecutor.class);
    final ServiceReference ref1 = Mockito.mock(ServiceReference.class);
    Mockito.when(ref1.getProperty(JobExecutor.PROPERTY_TOPICS)).thenReturn("a/*");
    Mockito.when(ref1.getProperty(Constants.SERVICE_RANKING)).thenReturn(1);
    Mockito.when(ref1.getProperty(Constants.SERVICE_ID)).thenReturn(1L);
    Mockito.when(bc.getService(ref1)).thenReturn(jc1);
    jcs.bindJobExecutor(ref1);
    assertNotNull(jcs.getExecutor("a/b"));
    assertNull(jcs.getExecutor("a"));
    assertNotNull(jcs.getExecutor("a/c"));
    assertNull(jcs.getExecutor("a/b/a"));
}
Also used : JobExecutor(org.apache.sling.event.jobs.consumer.JobExecutor) BundleContext(org.osgi.framework.BundleContext) ServiceReference(org.osgi.framework.ServiceReference) Test(org.junit.Test)

Example 4 with JobExecutor

use of org.apache.sling.event.jobs.consumer.JobExecutor in project sling by apache.

the class JobConsumerManagerTest method testSubCategoryMappingExecutor.

@Test
public void testSubCategoryMappingExecutor() {
    final BundleContext bc = Mockito.mock(BundleContext.class);
    final JobConsumerManager jcs = new JobConsumerManager();
    jcs.activate(bc, getDefaultConfig());
    final JobExecutor jc1 = Mockito.mock(JobExecutor.class);
    final ServiceReference ref1 = Mockito.mock(ServiceReference.class);
    Mockito.when(ref1.getProperty(JobExecutor.PROPERTY_TOPICS)).thenReturn("a/**");
    Mockito.when(ref1.getProperty(Constants.SERVICE_RANKING)).thenReturn(1);
    Mockito.when(ref1.getProperty(Constants.SERVICE_ID)).thenReturn(1L);
    Mockito.when(bc.getService(ref1)).thenReturn(jc1);
    jcs.bindJobExecutor(ref1);
    assertNotNull(jcs.getExecutor("a/b"));
    assertNull(jcs.getExecutor("a"));
    assertNotNull(jcs.getExecutor("a/c"));
    assertNotNull(jcs.getExecutor("a/b/a"));
}
Also used : JobExecutor(org.apache.sling.event.jobs.consumer.JobExecutor) BundleContext(org.osgi.framework.BundleContext) ServiceReference(org.osgi.framework.ServiceReference) Test(org.junit.Test)

Example 5 with JobExecutor

use of org.apache.sling.event.jobs.consumer.JobExecutor 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)

Aggregations

JobExecutor (org.apache.sling.event.jobs.consumer.JobExecutor)10 Test (org.junit.Test)9 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 Barrier (org.apache.sling.event.impl.Barrier)4 BundleContext (org.osgi.framework.BundleContext)4 ServiceReference (org.osgi.framework.ServiceReference)4 Event (org.osgi.service.event.Event)2 EventHandler (org.osgi.service.event.EventHandler)2 HashMap (java.util.HashMap)1 HashSet (java.util.HashSet)1 Map (java.util.Map)1 JobHandler (org.apache.sling.event.impl.jobs.JobHandler)1 JobImpl (org.apache.sling.event.impl.jobs.JobImpl)1