Search in sources :

Example 51 with ServiceReference

use of org.osgi.framework.ServiceReference in project sling by apache.

the class ChaosTest method setupChaosThreads.

/**
     * Setup chaos thread(s)
     *
     * Chaos is right now created by sending topology changing/changed events randomly
     */
private void setupChaosThreads(final List<Thread> threads, final AtomicLong finishedThreads) {
    final List<TopologyView> views = new ArrayList<>();
    // register topology listener
    final ServiceRegistration<TopologyEventListener> reg = this.bc.registerService(TopologyEventListener.class, new TopologyEventListener() {

        @Override
        public void handleTopologyEvent(final TopologyEvent event) {
            if (event.getType() == Type.TOPOLOGY_INIT) {
                views.add(event.getNewView());
            }
        }
    }, null);
    while (views.isEmpty()) {
        this.sleep(10);
    }
    reg.unregister();
    final TopologyView view = views.get(0);
    try {
        final Collection<ServiceReference<TopologyEventListener>> refs = this.bc.getServiceReferences(TopologyEventListener.class, null);
        assertNotNull(refs);
        assertFalse(refs.isEmpty());
        TopologyEventListener found = null;
        for (final ServiceReference<TopologyEventListener> ref : refs) {
            final TopologyEventListener listener = this.bc.getService(ref);
            if (listener != null && listener.getClass().getName().equals("org.apache.sling.event.impl.jobs.config.TopologyHandler")) {
                found = listener;
                break;
            }
            bc.ungetService(ref);
        }
        assertNotNull(found);
        final TopologyEventListener tel = found;
        threads.add(new Thread() {

            private final Random random = new Random();

            @Override
            public void run() {
                final long startTime = System.currentTimeMillis();
                // this thread runs 30 seconds longer than the job creation thread
                final long endTime = startTime + (DURATION + 30) * 1000;
                while (System.currentTimeMillis() < endTime) {
                    final int sleepTime = random.nextInt(25) + 15;
                    try {
                        Thread.sleep(sleepTime * 1000);
                    } catch (final InterruptedException ie) {
                        Thread.currentThread().interrupt();
                    }
                    tel.handleTopologyEvent(new TopologyEvent(Type.TOPOLOGY_CHANGING, view, null));
                    final int changingTime = random.nextInt(20) + 3;
                    try {
                        Thread.sleep(changingTime * 1000);
                    } catch (final InterruptedException ie) {
                        Thread.currentThread().interrupt();
                    }
                    tel.handleTopologyEvent(new TopologyEvent(Type.TOPOLOGY_CHANGED, view, view));
                }
                tel.getClass().getName();
                finishedThreads.incrementAndGet();
            }
        });
    } catch (InvalidSyntaxException e) {
        e.printStackTrace();
    }
}
Also used : TopologyEvent(org.apache.sling.discovery.TopologyEvent) ArrayList(java.util.ArrayList) ServiceReference(org.osgi.framework.ServiceReference) Random(java.util.Random) InvalidSyntaxException(org.osgi.framework.InvalidSyntaxException) TopologyView(org.apache.sling.discovery.TopologyView) TopologyEventListener(org.apache.sling.discovery.TopologyEventListener)

Example 52 with ServiceReference

use of org.osgi.framework.ServiceReference in project sling by apache.

the class JobConsumerManagerTest method testSimpleMappingConsumer.

@Test
public void testSimpleMappingConsumer() {
    final BundleContext bc = Mockito.mock(BundleContext.class);
    final JobConsumerManager jcs = new JobConsumerManager();
    jcs.activate(bc, getDefaultConfig());
    final JobConsumer jc1 = Mockito.mock(JobConsumer.class);
    final ServiceReference ref1 = Mockito.mock(ServiceReference.class);
    Mockito.when(ref1.getProperty(JobConsumer.PROPERTY_TOPICS)).thenReturn("a/b");
    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.bindJobConsumer(ref1);
    assertNotNull(jcs.getExecutor("a/b"));
    assertNull(jcs.getExecutor("a"));
    assertNull(jcs.getExecutor("a/c"));
    assertNull(jcs.getExecutor("a/b/a"));
}
Also used : JobConsumer(org.apache.sling.event.jobs.consumer.JobConsumer) BundleContext(org.osgi.framework.BundleContext) ServiceReference(org.osgi.framework.ServiceReference) Test(org.junit.Test)

Example 53 with ServiceReference

use of org.osgi.framework.ServiceReference in project sling by apache.

the class JobConsumerManagerTest method testSubCategoryMappingConsumer.

@Test
public void testSubCategoryMappingConsumer() {
    final BundleContext bc = Mockito.mock(BundleContext.class);
    final JobConsumerManager jcs = new JobConsumerManager();
    jcs.activate(bc, getDefaultConfig());
    final JobConsumer jc1 = Mockito.mock(JobConsumer.class);
    final ServiceReference ref1 = Mockito.mock(ServiceReference.class);
    Mockito.when(ref1.getProperty(JobConsumer.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.bindJobConsumer(ref1);
    assertNotNull(jcs.getExecutor("a/b"));
    assertNull(jcs.getExecutor("a"));
    assertNotNull(jcs.getExecutor("a/c"));
    assertNotNull(jcs.getExecutor("a/b/a"));
}
Also used : JobConsumer(org.apache.sling.event.jobs.consumer.JobConsumer) BundleContext(org.osgi.framework.BundleContext) ServiceReference(org.osgi.framework.ServiceReference) Test(org.junit.Test)

Example 54 with ServiceReference

use of org.osgi.framework.ServiceReference in project sling by apache.

the class JobConsumerManagerTest method testCategoryMappingConsumer.

@Test
public void testCategoryMappingConsumer() {
    final BundleContext bc = Mockito.mock(BundleContext.class);
    final JobConsumerManager jcs = new JobConsumerManager();
    jcs.activate(bc, getDefaultConfig());
    final JobConsumer jc1 = Mockito.mock(JobConsumer.class);
    final ServiceReference ref1 = Mockito.mock(ServiceReference.class);
    Mockito.when(ref1.getProperty(JobConsumer.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.bindJobConsumer(ref1);
    assertNotNull(jcs.getExecutor("a/b"));
    assertNull(jcs.getExecutor("a"));
    assertNotNull(jcs.getExecutor("a/c"));
    assertNull(jcs.getExecutor("a/b/a"));
}
Also used : JobConsumer(org.apache.sling.event.jobs.consumer.JobConsumer) BundleContext(org.osgi.framework.BundleContext) ServiceReference(org.osgi.framework.ServiceReference) Test(org.junit.Test)

Example 55 with ServiceReference

use of org.osgi.framework.ServiceReference 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)

Aggregations

ServiceReference (org.osgi.framework.ServiceReference)1690 Test (org.junit.Test)926 Properties (java.util.Properties)396 Architecture (org.apache.felix.ipojo.architecture.Architecture)263 CheckService (org.apache.felix.ipojo.runtime.core.test.services.CheckService)233 BundleContext (org.osgi.framework.BundleContext)231 InstanceDescription (org.apache.felix.ipojo.architecture.InstanceDescription)227 ComponentInstance (org.apache.felix.ipojo.ComponentInstance)215 InvalidSyntaxException (org.osgi.framework.InvalidSyntaxException)183 ArrayList (java.util.ArrayList)167 Bundle (org.osgi.framework.Bundle)144 FooService (org.apache.felix.ipojo.runtime.core.services.FooService)141 Hashtable (java.util.Hashtable)124 IOException (java.io.IOException)107 CheckService (org.apache.felix.ipojo.runtime.core.services.CheckService)92 Dictionary (java.util.Dictionary)82 Configuration (org.osgi.service.cm.Configuration)74 CheckService (org.apache.felix.ipojo.handler.temporal.services.CheckService)70 FooService (org.apache.felix.ipojo.handler.temporal.services.FooService)70 CheckService (org.apache.felix.ipojo.handler.transaction.services.CheckService)65