use of org.apache.felix.dm.impl.ComponentImpl in project felix by apache.
the class ConcurrencyTest method createComponentAddAndRemoveDependenciesInParallelThreads.
@Test
public void createComponentAddAndRemoveDependenciesInParallelThreads() throws Exception {
final ComponentImpl c = new ComponentImpl();
// If not, we may see NullPointers when invoking lifecycle callbacks
c.setImplementation(new Object());
ExecutorService e = Executors.newFixedThreadPool(16);
c.start();
for (int i = 0; i < 1000; i++) {
e.execute(new Runnable() {
@Override
public void run() {
SimpleServiceDependency d = new SimpleServiceDependency();
d.setRequired(true);
c.add(d);
// d.changed(new EventImpl(true));
// d.changed(new EventImpl(false));
c.remove(d);
}
});
}
e.shutdown();
e.awaitTermination(10, TimeUnit.SECONDS);
// Assert.assertEquals("component should not be available", false, c.isAvailable());
c.stop();
Assert.assertEquals("component should not be available", false, c.isAvailable());
}
use of org.apache.felix.dm.impl.ComponentImpl in project felix by apache.
the class ConcurrencyTest method createComponentAddDependencyAndListenerAndAddAnotherDependencyInAParallelThread.
/**
* Ensure actions from another thread than the current thread executing in the SerialExecutor are being
* scheduled (added to the queue) rather than being executed immediately.
*/
@Test
public void createComponentAddDependencyAndListenerAndAddAnotherDependencyInAParallelThread() {
final Semaphore s = new Semaphore(0);
final ComponentImpl c = new ComponentImpl();
final SimpleServiceDependency d = new SimpleServiceDependency();
d.setRequired(true);
final SimpleServiceDependency d2 = new SimpleServiceDependency();
d2.setRequired(true);
final Thread t = new Thread() {
public void run() {
c.add(d2);
s.release();
}
};
ComponentStateListener l = new ComponentStateListener() {
@Override
public void changed(Component component, ComponentState state) {
try {
c.remove(this);
// launch a second thread interacting with our ComponentImpl and block this thread until the
// second thread finished its interaction with our component. We want to ensure the work of
// the second thread is scheduled after our current job in the serial executor and does not
// get executed immediately.
t.start();
s.acquire();
Assert.assertEquals("dependency count should be 1", 1, c.getDependencies().size());
} catch (InterruptedException e) {
e.printStackTrace();
}
}
};
// If not, we may see NullPointers when invoking lifecycle callbacks
c.setImplementation(new Object());
c.start();
c.add(d);
c.add(l);
Assert.assertEquals("component should not be available", false, c.isAvailable());
// sets dependency d to available and triggers our ComponentStateListener
d.add(new EventImpl());
// due to the dependency added by the second thread in the serial executor we still expect our component
// to be unavailable. This work was scheduled in the serial executor and will be executed by the current
// thread after it finished handling the job for handling the changed() method.
Assert.assertEquals("component should not be available", false, c.isAvailable());
c.remove(l);
Assert.assertEquals("component should not be available", false, c.isAvailable());
c.remove(d);
Assert.assertEquals("component should not be available", false, c.isAvailable());
c.remove(d2);
Assert.assertEquals("component should be available", true, c.isAvailable());
c.stop();
Assert.assertEquals("component should not be available", false, c.isAvailable());
}
use of org.apache.felix.dm.impl.ComponentImpl in project felix by apache.
the class ConfigurationTest method testConfigurationFailure.
@Test
public void testConfigurationFailure() throws Throwable {
final Ensure e = new Ensure();
// Create our configuration dependency
final ConfigurationDependencyImpl conf = new ConfigurationDependencyImpl();
conf.setPid("some.pid");
// Create another required dependency
final SimpleServiceDependency requiredDependency = new SimpleServiceDependency();
requiredDependency.setRequired(true);
requiredDependency.setCallbacks("addDep", null);
// Create our component, which will fail when handling configuration update
ComponentImpl c = new ComponentImpl();
c.setImplementation(new Object() {
volatile Dictionary m_conf;
public void updated(Dictionary conf) {
debug("updated: conf=%s", conf);
m_conf = conf;
if ("invalid".equals(conf.get("conf"))) {
// We refuse the first configuration.
debug("refusing configuration");
e.step(1);
// Set our acceptUpdate flag to true, so next update will be successful
throw new RuntimeException("update failed (expected)");
} else {
debug("accepting configuration");
e.step(2);
}
}
public void addDep() {
if ("invalid".equals(m_conf.get("conf"))) {
e.throwable(new Exception("addDep should not be called"));
}
e.step(3);
debug("addDep");
}
void init(Component c) {
if ("invalid".equals(m_conf.get("conf"))) {
e.throwable(new Exception("init should not be called"));
}
e.step(4);
debug("init");
}
void start() {
if ("invalid".equals(m_conf.get("conf"))) {
e.throwable(new Exception("start should not be called"));
}
e.step(5);
debug("start");
}
});
// Add the dependencies
c.add(conf);
c.add(requiredDependency);
// Start our component ("requiredDependency" is not yet available, so we'll stay in WAITING_FOR_REQUIRED state).
c.start();
// Enabled "requiredDependency"
requiredDependency.add(new EventImpl());
// Now, act as the configuration admin service and inject a wrong dependency
try {
Hashtable props = new Hashtable();
props.put("conf", "invalid");
conf.updated(props);
} catch (ConfigurationException err) {
warn("got expected configuration error");
}
e.waitForStep(1, 5000);
e.ensure();
// Now, inject another valid configuration
try {
Hashtable props = new Hashtable();
props.put("conf", "valid");
conf.updated(props);
} catch (ConfigurationException err) {
warn("got unexpected configuration error");
e.throwable(err);
}
// This time, our component should be started properly.
e.waitForStep(5, 5000);
e.ensure();
}
use of org.apache.felix.dm.impl.ComponentImpl in project felix by apache.
the class ServiceRaceTest 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 ComponentImpl client = new ComponentImpl();
final Client theClient = new Client(step);
client.setImplementation(theClient);
// Create client service dependencies
final SimpleServiceDependency[] dependencies = new SimpleServiceDependency[DEPENDENCIES];
for (int i = 0; i < DEPENDENCIES; i++) {
dependencies[i] = new SimpleServiceDependency();
dependencies[i].setRequired(true);
dependencies[i].setCallbacks("add", "remove");
client.add(dependencies[i]);
}
final ConfigurationDependencyImpl confDependency = new ConfigurationDependencyImpl();
confDependency.setPid("mypid");
client.add(confDependency);
// Create Configuration (concurrently).
// We have to simulate the configuration update, using a component state listener, which will
// trigger an update thread, but only once the component is started.
final ComponentStateListener listener = new ComponentStateListener() {
private volatile Dictionary m_conf;
public void changed(Component c, ComponentState state) {
if (state == ComponentState.WAITING_FOR_REQUIRED && m_conf == null) {
m_conf = new Hashtable();
m_conf.put("foo", "bar");
m_threadpool.execute(new Runnable() {
public void run() {
try {
confDependency.updated(m_conf);
} catch (ConfigurationException e) {
warn("configuration failed", e);
}
}
});
}
}
};
client.add(listener);
// Start the client (concurrently)
m_threadpool.execute(new Runnable() {
public void run() {
client.start();
// injected only one the tracker has been opened ...
for (int i = 0; i < DEPENDENCIES; i++) {
final SimpleServiceDependency dep = dependencies[i];
final Event added = new EventImpl(i);
m_threadpool.execute(new Runnable() {
public void run() {
dep.add(added);
}
});
}
}
});
// Ensure that client has been started.
int expectedStep = 1 + /* conf */
DEPENDENCIES + 1;
step.waitForStep(expectedStep, STEP_WAIT);
Assert.assertEquals(DEPENDENCIES, theClient.getDependencies());
Assert.assertNotNull(theClient.getConfiguration());
client.remove(listener);
// Stop the client and all dependencies concurrently.
for (int i = 0; i < DEPENDENCIES; i++) {
final SimpleServiceDependency dep = dependencies[i];
final Event removed = new EventImpl(i);
m_threadpool.execute(new Runnable() {
public void run() {
dep.remove(removed);
}
});
}
m_threadpool.execute(new Runnable() {
public void run() {
client.stop();
}
});
m_threadpool.execute(new Runnable() {
public void run() {
try {
// simulate a configuration suppression.
confDependency.updated(null);
} catch (ConfigurationException e) {
warn("error while unconfiguring", e);
}
}
});
// Ensure that client has been stopped, then destroyed, then unbound from all dependencies
// stop/destroy
expectedStep += 2;
// removed all dependencies
expectedStep += DEPENDENCIES;
step.waitForStep(expectedStep, STEP_WAIT);
step.ensure();
Assert.assertEquals(0, theClient.getDependencies());
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.impl.ComponentImpl in project felix by apache.
the class ComponentTest method createComponentAddDependencyMakeAvailableChangeAndUnavailableWithCallbacks.
@Test
public void createComponentAddDependencyMakeAvailableChangeAndUnavailableWithCallbacks() {
final Ensure e = new Ensure();
ComponentImpl c = new ComponentImpl();
c.setImplementation(new Object() {
public void add() {
e.step(1);
}
public void change() {
e.step(3);
}
public void remove() {
e.step(5);
}
});
SimpleServiceDependency d1 = new SimpleServiceDependency();
d1.setCallbacks("add", "change", "remove");
d1.setRequired(true);
// add the dependency to the component
c.add(d1);
// start the component
c.start();
// make the dependency available, we expect the add callback
// to be invoked here
d1.add(new EventImpl());
e.step(2);
// change the dependency
d1.change(new EventImpl());
e.step(4);
// remove the dependency, should trigger the remove callback
d1.remove(new EventImpl());
e.step(6);
c.stop();
c.remove(d1);
Assert.assertEquals("Component stopped, should be unavailable again", false, c.isAvailable());
}
Aggregations