Search in sources :

Example 1 with ServiceDependency

use of org.apache.felix.dm.ServiceDependency 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();
    }
}
Also used : TimeUnit(java.util.concurrent.TimeUnit) List(java.util.List) Component(org.apache.felix.dm.Component) ForkJoinPool(java.util.concurrent.ForkJoinPool) ComponentStateListener(org.apache.felix.dm.ComponentStateListener) Ensure(org.apache.felix.dm.itest.util.Ensure) TestBase(org.apache.felix.dm.itest.util.TestBase) ServiceDependency(org.apache.felix.dm.ServiceDependency) Assert(org.junit.Assert) Hashtable(java.util.Hashtable) ArrayList(java.util.ArrayList) Hashtable(java.util.Hashtable) ArrayList(java.util.ArrayList) ServiceDependency(org.apache.felix.dm.ServiceDependency) Ensure(org.apache.felix.dm.itest.util.Ensure) Component(org.apache.felix.dm.Component) ComponentStateListener(org.apache.felix.dm.ComponentStateListener)

Example 2 with ServiceDependency

use of org.apache.felix.dm.ServiceDependency 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();
}
Also used : DependencyManager(org.apache.felix.dm.DependencyManager) ServiceDependency(org.apache.felix.dm.ServiceDependency) Ensure(org.apache.felix.dm.itest.util.Ensure) Component(org.apache.felix.dm.Component)

Example 3 with ServiceDependency

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

the class DiagnosticsTest method testProvidersWithoutProperties.

public void testProvidersWithoutProperties() throws Exception {
    DependencyManager dm = getDM();
    ServiceDependency serviceDependency1 = dm.createServiceDependency().setService(S1.class).setRequired(true);
    Component component1 = dm.createComponent().setImplementation(C0.class).add(serviceDependency1);
    Component component2 = dm.createComponent().setImplementation(S1Impl1.class).setInterface(S1.class.getName(), null);
    Component component3 = dm.createComponent().setImplementation(S1Impl2.class).setInterface(S1.class.getName(), null);
    dm.add(component1);
    dm.add(component2);
    dm.add(component3);
    DependencyGraph graph = DependencyGraph.getGraph(ComponentState.ALL, DependencyState.ALL);
    List<ComponentDeclaration> providers = graph.getProviders(serviceDependency1);
    assertEquals(2, providers.size());
    assertTrue(providers.contains(component2));
    assertTrue(providers.contains(component3));
}
Also used : ComponentDeclaration(org.apache.felix.dm.ComponentDeclaration) DependencyManager(org.apache.felix.dm.DependencyManager) DependencyGraph(org.apache.felix.dm.diagnostics.DependencyGraph) ServiceDependency(org.apache.felix.dm.ServiceDependency) Component(org.apache.felix.dm.Component)

Example 4 with ServiceDependency

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

the class DiagnosticsTest method testProvidersWithProperties.

public void testProvidersWithProperties() throws Exception {
    DependencyManager dm = getDM();
    ServiceDependency serviceDependency1 = dm.createServiceDependency().setService(S1.class, "(key=value)").setRequired(true);
    Component component1 = dm.createComponent().setImplementation(C0.class).add(serviceDependency1);
    Properties component2Properties = new Properties();
    component2Properties.put("key", "value");
    Properties component4Properties = new Properties();
    component4Properties.put("key", "otherValue");
    Component component2 = dm.createComponent().setImplementation(S1Impl1.class).setInterface(S1.class.getName(), component2Properties);
    Component component3 = dm.createComponent().setImplementation(S1Impl2.class).setInterface(S1.class.getName(), null);
    Component component4 = dm.createComponent().setImplementation(S1Impl3.class).setInterface(S1.class.getName(), component4Properties);
    m_dm.add(component1);
    m_dm.add(component2);
    m_dm.add(component3);
    m_dm.add(component4);
    DependencyGraph graph = DependencyGraph.getGraph(ComponentState.ALL, DependencyState.ALL);
    List<ComponentDeclaration> providers = graph.getProviders(serviceDependency1);
    assertEquals(1, providers.size());
    assertTrue(providers.contains(component2));
    assertFalse(providers.contains(component3));
    assertFalse(providers.contains(component4));
}
Also used : ComponentDeclaration(org.apache.felix.dm.ComponentDeclaration) DependencyManager(org.apache.felix.dm.DependencyManager) DependencyGraph(org.apache.felix.dm.diagnostics.DependencyGraph) ServiceDependency(org.apache.felix.dm.ServiceDependency) Component(org.apache.felix.dm.Component) Properties(java.util.Properties)

Example 5 with ServiceDependency

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

the class TemporalServiceDependencyTest method testFELIX4858_ServiceAdapterConsumptionWithCallbackAndIntermittentAvailability.

// Same test as testServiceConsumptionWithCallbackAndIntermittentAvailability, but the consumer is now
// an adapter for the Adaptee interface.
public void testFELIX4858_ServiceAdapterConsumptionWithCallbackAndIntermittentAvailability() {
    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 = component(m).impl(provider).provides(TemporalServiceInterface.class.getName()).build();
    TemporalServiceProvider2 provider2 = new TemporalServiceProvider2(e);
    Component sp2 = component(m).impl(provider2).provides(TemporalServiceInterface.class.getName()).build();
    TemporalServiceConsumerAdapterWithCallback consumer = new TemporalServiceConsumerAdapterWithCallback(e);
    Component sc = adapter(m, Adaptee.class).impl(consumer).build();
    ServiceDependency temporalDep = serviceDependency(sc, TemporalServiceInterface.class).timeout(10000).add("add").remove("remove").build();
    sc.add(temporalDep);
    Component adaptee = component(m).impl(new Adaptee()).provides(Adaptee.class.getName()).build();
    // add the adapter service consumer
    m.add(sc);
    // add the adaptee (the adapter service depends on it)
    m.add(adaptee);
    // 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();
}
Also used : DependencyManager(org.apache.felix.dm.DependencyManager) ServiceDependency(org.apache.felix.dm.ServiceDependency) Component(org.apache.felix.dm.Component)

Aggregations

ServiceDependency (org.apache.felix.dm.ServiceDependency)12 Component (org.apache.felix.dm.Component)9 DependencyManager (org.apache.felix.dm.DependencyManager)8 Ensure (org.apache.felix.dm.itest.util.Ensure)4 DependencyGraph (org.apache.felix.dm.diagnostics.DependencyGraph)3 ArrayList (java.util.ArrayList)2 Hashtable (java.util.Hashtable)2 ComponentDeclaration (org.apache.felix.dm.ComponentDeclaration)2 ConfigurationDependency (org.apache.felix.dm.ConfigurationDependency)2 IOException (java.io.IOException)1 List (java.util.List)1 Properties (java.util.Properties)1 ForkJoinPool (java.util.concurrent.ForkJoinPool)1 TimeUnit (java.util.concurrent.TimeUnit)1 BundleDependency (org.apache.felix.dm.BundleDependency)1 ComponentStateListener (org.apache.felix.dm.ComponentStateListener)1 Dependency (org.apache.felix.dm.Dependency)1 ResourceDependency (org.apache.felix.dm.ResourceDependency)1 MissingDependency (org.apache.felix.dm.diagnostics.MissingDependency)1 TestBase (org.apache.felix.dm.itest.util.TestBase)1