Search in sources :

Example 1 with ComponentState

use of org.apache.felix.dm.ComponentState 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());
}
Also used : Semaphore(java.util.concurrent.Semaphore) Component(org.apache.felix.dm.Component) ComponentImpl(org.apache.felix.dm.impl.ComponentImpl) ComponentStateListener(org.apache.felix.dm.ComponentStateListener) ComponentState(org.apache.felix.dm.ComponentState) Test(org.junit.Test)

Example 2 with ComponentState

use of org.apache.felix.dm.ComponentState 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();
    }
}
Also used : Dictionary(java.util.Dictionary) Hashtable(java.util.Hashtable) ConfigurationDependencyImpl(org.apache.felix.dm.impl.ConfigurationDependencyImpl) ComponentImpl(org.apache.felix.dm.impl.ComponentImpl) ConfigurationException(org.osgi.service.cm.ConfigurationException) Event(org.apache.felix.dm.context.Event) Component(org.apache.felix.dm.Component) ComponentStateListener(org.apache.felix.dm.ComponentStateListener) ComponentState(org.apache.felix.dm.ComponentState)

Example 3 with ComponentState

use of org.apache.felix.dm.ComponentState in project felix by apache.

the class ComponentTest method createComponentStartItAddDependencyAndListenerMakeDependencyAvailableAndUnavailableImmediately.

@Test
public void createComponentStartItAddDependencyAndListenerMakeDependencyAvailableAndUnavailableImmediately() {
    ComponentImpl c = new ComponentImpl();
    c.setImplementation(MyComponent.class);
    final SimpleServiceDependency d = new SimpleServiceDependency();
    d.setRequired(true);
    ComponentStateListener l = new ComponentStateListener() {

        @Override
        public void changed(Component c, ComponentState state) {
            // make the dependency unavailable
            d.remove(new EventImpl());
        }
    };
    c.start();
    c.add(d);
    // we add a listener here which immediately triggers an 'external event' that
    // makes the dependency unavailable again as soon as it's invoked
    c.add(l);
    Assert.assertEquals("Component unavailable, dependency unavailable", false, c.isAvailable());
    // so even though we make the dependency available here, before our call returns it
    // is made unavailable again
    d.add(new EventImpl());
    Assert.assertEquals("Component *still* unavailable, because the listener immediately makes the dependency unavailable", false, c.isAvailable());
    c.remove(l);
    Assert.assertEquals("listener removed, component still unavailable", false, c.isAvailable());
    c.remove(d);
    Assert.assertEquals("dependency removed, component available", true, c.isAvailable());
    c.stop();
    Assert.assertEquals("Component stopped, should be unavailable", false, c.isAvailable());
}
Also used : Component(org.apache.felix.dm.Component) ComponentImpl(org.apache.felix.dm.impl.ComponentImpl) ComponentStateListener(org.apache.felix.dm.ComponentStateListener) ComponentState(org.apache.felix.dm.ComponentState) Test(org.junit.Test)

Example 4 with ComponentState

use of org.apache.felix.dm.ComponentState in project felix by apache.

the class ComponentImpl method handleChange.

/**
 * Runs the state machine, to see if a change event has to trigger some component state transition.
 */
private void handleChange() {
    if (isHandlingChange()) {
        return;
    }
    m_logger.debug("handleChanged");
    handlingChange(true);
    try {
        ComponentState oldState;
        ComponentState newState;
        do {
            oldState = m_state;
            newState = calculateNewState(oldState);
            m_logger.debug("%s -> %s", oldState, newState);
            m_state = newState;
        } while (performTransition(oldState, newState));
    } finally {
        handlingChange(false);
        clearInvokeCallbackCache();
        m_logger.debug("end handling change.");
    }
}
Also used : ComponentState(org.apache.felix.dm.ComponentState)

Aggregations

ComponentState (org.apache.felix.dm.ComponentState)4 Component (org.apache.felix.dm.Component)3 ComponentStateListener (org.apache.felix.dm.ComponentStateListener)3 ComponentImpl (org.apache.felix.dm.impl.ComponentImpl)3 Test (org.junit.Test)2 Dictionary (java.util.Dictionary)1 Hashtable (java.util.Hashtable)1 Semaphore (java.util.concurrent.Semaphore)1 Event (org.apache.felix.dm.context.Event)1 ConfigurationDependencyImpl (org.apache.felix.dm.impl.ConfigurationDependencyImpl)1 ConfigurationException (org.osgi.service.cm.ConfigurationException)1