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();
}
}
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"));
}
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"));
}
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"));
}
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"));
}
Aggregations