use of org.apache.felix.dm.Component in project felix by apache.
the class ServiceDependencyTest method testServiceRegistrationAndConsumption.
public void testServiceRegistrationAndConsumption() {
DependencyManager m = getDM();
// helper class that ensures certain steps get executed in sequence
Ensure e = new Ensure();
// create a service provider and consumer
Component sp = m.createComponent().setImplementation(new ServiceProvider(e)).setInterface(ServiceInterface.class.getName(), null);
Component sc = m.createComponent().setImplementation(new ServiceConsumer(e)).add(m.createServiceDependency().setService(ServiceInterface.class).setRequired(true));
Component sc2 = m.createComponent().setImplementation(new ServiceConsumerCallbacks(e)).add(m.createServiceDependency().setService(ServiceInterface.class).setRequired(false).setCallbacks("add", "remove"));
m.add(sp);
m.add(sc);
m.remove(sc);
m.add(sc2);
m.remove(sp);
m.remove(sc2);
// ensure we executed all steps inside the component instance
e.step(6);
}
use of org.apache.felix.dm.Component in project felix by apache.
the class ServiceRaceWithOrderedUnbindTest method doTest.
void doTest(int loop) throws Throwable {
debug("loop#%d -------------------------", loop);
final Ensure step = new Ensure(false);
// Create one client component, which depends on many service dependencies
final Component client = m_dm.createComponent();
final Client clientImpl = new Client(step);
client.setImplementation(clientImpl);
// Before creating the client, register a component listener to check
// the client is really started or deactivated.
ComponentStateListener clientListener = (c, s) -> {
switch(s) {
case TRACKING_OPTIONAL:
step.step(1);
break;
case INACTIVE:
step.step(2);
break;
default:
break;
}
};
client.add(clientListener);
// Create client service dependencies
final ServiceDependency[] dependencies = new ServiceDependency[DEPENDENCIES];
for (int i = 0; i < DEPENDENCIES; i++) {
final String filter = "(id=loop" + loop + "." + i + ")";
dependencies[i] = m_dm.createServiceDependency().setService(Dep.class, filter).setRequired(true).setCallbacks("add", "remove");
client.add(dependencies[i]);
}
// Activate the client service dependencies concurrently.
List<Component> deps = new ArrayList();
for (int i = 0; i < DEPENDENCIES; i++) {
Hashtable h = new Hashtable();
h.put("id", "loop" + loop + "." + i);
final Component s = m_dm.createComponent().setInterface(Dep.class.getName(), h).setImplementation(new DepImpl());
deps.add(s);
schedule(() -> m_dm.add(s));
}
// Start the client (concurrently)
schedule(() -> m_dm.add(client));
// Ensure that client has been started.
// client has entered in TRACKING_OPTIONAL state
step.waitForStep(1, STEP_WAIT);
Assert.assertEquals(DEPENDENCIES, clientImpl.getDependencies());
// Make sure threadpool is quiescent, then deactivate all components.
if (!m_threadPool.awaitQuiescence(5000, TimeUnit.MILLISECONDS)) {
throw new RuntimeException("Could not start components timely.");
}
// Stop all dependencies, and client
schedule(() -> {
for (Component dep : deps) {
final Component dependency = dep;
m_dm.remove(dependency);
}
m_dm.remove(client);
});
// Ensure that client has been stopped, then destroyed, then unbound from all dependencies
// Client entered in INACTIVE state
step.waitForStep(2, STEP_WAIT);
step.ensure();
Assert.assertEquals(0, clientImpl.getDependencies());
// Make sure threadpool is quiescent before doing next iteration.
if (!m_threadPool.awaitQuiescence(5000, TimeUnit.MILLISECONDS)) {
throw new RuntimeException("Could not start components timely.");
}
if (super.errorsLogged()) {
throw new IllegalStateException("Race test interrupted (some error occured, see previous logs)");
}
debug("finished one test loop");
if ((loop + 1) % 100 == 0) {
long duration = System.currentTimeMillis() - m_timeStamp;
warn("Performed 100 tests (total=%d) in %d ms.", (loop + 1), duration);
m_timeStamp = System.currentTimeMillis();
}
}
use of org.apache.felix.dm.Component in project felix by apache.
the class TemporalServiceDependencyTest method testServiceConsumptionWithCallbackAndIntermittentAvailability.
public void testServiceConsumptionWithCallbackAndIntermittentAvailability() {
final DependencyManager m = getDM();
// helper class that ensures certain steps get executed in sequence
Ensure e = new Ensure();
// create a service provider and consumer
TemporalServiceProvider provider = new TemporalServiceProvider(e);
Component sp = m.createComponent().setImplementation(provider).setInterface(TemporalServiceInterface.class.getName(), null);
TemporalServiceProvider2 provider2 = new TemporalServiceProvider2(e);
Component sp2 = m.createComponent().setImplementation(provider2).setInterface(TemporalServiceInterface.class.getName(), null);
TemporalServiceConsumerWithCallback consumer = new TemporalServiceConsumerWithCallback(e);
ServiceDependency temporalDep = m.createTemporalServiceDependency(10000).setService(TemporalServiceInterface.class).setRequired(true).setCallbacks("add", "remove");
Component sc = m.createComponent().setImplementation(consumer).add(temporalDep);
// add the service consumer
m.add(sc);
// now add the first provider
m.add(sp);
e.waitForStep(2, 5000);
// and remove it again (this should not affect the consumer yet)
m.remove(sp);
// now add the second provider
m.add(sp2);
e.step(3);
e.waitForStep(4, 5000);
// and remove it again
m.remove(sp2);
// finally remove the consumer
m.remove(sc);
// Wait for the consumer.remove callback
e.waitForStep(6, 5000);
// ensure we executed all steps inside the component instance
e.step(7);
m.clear();
}
use of org.apache.felix.dm.Component in project felix by apache.
the class TemporalServiceDependencyTest method testServiceConsumptionAndIntermittentAvailability.
public void testServiceConsumptionAndIntermittentAvailability() {
final DependencyManager m = getDM();
// helper class that ensures certain steps get executed in sequence
Ensure e = new Ensure();
// create a service provider and consumer
TemporalServiceProvider provider = new TemporalServiceProvider(e);
Component sp = m.createComponent().setImplementation(provider).setInterface(TemporalServiceInterface.class.getName(), null);
TemporalServiceProvider2 provider2 = new TemporalServiceProvider2(e);
Component sp2 = m.createComponent().setImplementation(provider2).setInterface(TemporalServiceInterface.class.getName(), null);
TemporalServiceConsumer consumer = new TemporalServiceConsumer(e);
Component sc = m.createComponent().setImplementation(consumer).add(m.createTemporalServiceDependency(10000).setService(TemporalServiceInterface.class).setRequired(true));
// add the service consumer
m.add(sc);
// now add the first provider
m.add(sp);
e.waitForStep(2, 5000);
// and remove it again (this should not affect the consumer yet)
m.remove(sp);
// now add the second provider
m.add(sp2);
e.step(3);
e.waitForStep(4, 5000);
// and remove it again
m.remove(sp2);
// finally remove the consumer
m.remove(sc);
// ensure we executed all steps inside the component instance
e.step(6);
m.clear();
}
use of org.apache.felix.dm.Component in project felix by apache.
the class TemporalServiceDependencyTest method testFelix4602_PropagateServiceInvocationException.
public void testFelix4602_PropagateServiceInvocationException() {
final DependencyManager m = getDM();
final Ensure ensure = new Ensure();
Runnable provider = new Runnable() {
public void run() {
throw new UncheckedException();
}
};
Hashtable props = new Hashtable();
props.put("target", getClass().getSimpleName());
Component providerComp = m.createComponent().setInterface(Runnable.class.getName(), props).setImplementation(provider);
Object consumer = new Object() {
volatile Runnable m_provider;
@SuppressWarnings("unused")
void start() {
try {
ensure.step(1);
m_provider.run();
} catch (UncheckedException e) {
ensure.step(2);
}
}
};
Component consumerComp = m.createComponent().setImplementation(consumer).add(m.createTemporalServiceDependency(5000).setService(Runnable.class, "(target=" + getClass().getSimpleName() + ")").setRequired(true));
m.add(consumerComp);
m.add(providerComp);
ensure.waitForStep(2, 5000);
m.clear();
}
Aggregations