Search in sources :

Example 6 with Deferred

use of org.osgi.util.promise.Deferred in project aries by apache.

the class DeferredTest method testResolveWithFailure.

@Test
public void testResolveWithFailure() throws Exception {
    Deferred<String> def = new Deferred<String>();
    Promise<String> promise = def.getPromise();
    Deferred<String> def2 = new Deferred<String>();
    Promise<String> promise2 = def2.getPromise();
    Promise<Void> with = def.resolveWith(promise2);
    // If the specified Promise is resolved with a failure,
    // the associated Promise is resolved with the failure of the specified Promise.
    Exception failure = new Exception("resolveWithFailure");
    def2.fail(failure);
    assertTrue("Promise resolved", promise.isDone());
    assertEquals("Failure matches", failure, promise.getFailure());
    // The returned Promise will be successfully resolved, with the value null,
    // if the associated Promise was resolved by the specified Promise.
    assertNull("resolveWith null", with.getValue());
}
Also used : Deferred(org.osgi.util.promise.Deferred) InvocationTargetException(java.lang.reflect.InvocationTargetException) Test(org.junit.Test)

Example 7 with Deferred

use of org.osgi.util.promise.Deferred in project aries by apache.

the class DeferredTest method testFail.

@Test
public void testFail() throws Exception {
    Deferred<String> def = new Deferred<String>();
    Promise<String> promise = def.getPromise();
    Exception failure = new Exception("Oops");
    def.fail(failure);
    assertTrue("Promise resolved", promise.isDone());
    assertEquals("Failure matches", failure, promise.getFailure());
    try {
        promise.getValue();
        fail("getValue didn't throw InvocationTargetException");
    } catch (InvocationTargetException e) {
        assertEquals("Failure matches", failure, e.getCause());
    }
    try {
        def.fail(failure);
        fail("Already failed didn't throw IllegalStateException");
    } catch (IllegalStateException e) {
        assertNotNull(e);
    }
}
Also used : Deferred(org.osgi.util.promise.Deferred) InvocationTargetException(java.lang.reflect.InvocationTargetException) InvocationTargetException(java.lang.reflect.InvocationTargetException) Test(org.junit.Test)

Example 8 with Deferred

use of org.osgi.util.promise.Deferred in project bnd by bndtools.

the class OSGiIndex method isStale.

/**
	 * Check any of the URL indexes are stale.
	 * 
	 * @return
	 * @throws Exception
	 */
boolean isStale() throws Exception {
    final Deferred<List<Void>> freshness = new Deferred<>();
    List<Promise<Void>> promises = new ArrayList<>(getURIs().size());
    for (final URI uri : getURIs()) {
        if (freshness.getPromise().isDone()) {
            // early exit if staleness already detected
            break;
        }
        try {
            Promise<TaggedData> async = client.build().useCache().asTag().async(uri);
            promises.add(async.then(new Success<TaggedData, Void>() {

                @Override
                public Promise<Void> call(Promise<TaggedData> resolved) throws Exception {
                    switch(resolved.getValue().getState()) {
                        case OTHER:
                            // in the offline case
                            // ignore might be best here
                            logger.debug("Could not verify {}", uri);
                            break;
                        case UNMODIFIED:
                            break;
                        case NOT_FOUND:
                        case UPDATED:
                        default:
                            logger.debug("Found {} to be stale", uri);
                            freshness.fail(new Exception("stale"));
                    }
                    return null;
                }
            }, new Failure() {

                @Override
                public void fail(Promise<?> resolved) throws Exception {
                    logger.debug("Could not verify {}: {}", uri, resolved.getFailure());
                    freshness.fail(resolved.getFailure());
                }
            }));
        } catch (Exception e) {
            logger.debug("Checking stale status: {}: {}", uri, e);
        }
    }
    // Resolve when all uris checked
    Promise<List<Void>> all = Promises.all(promises);
    freshness.resolveWith(all);
    // Block until freshness is resolved
    return freshness.getPromise().getFailure() != null;
}
Also used : Deferred(org.osgi.util.promise.Deferred) ArrayList(java.util.ArrayList) TaggedData(aQute.bnd.service.url.TaggedData) URI(java.net.URI) Success(org.osgi.util.promise.Success) Promise(org.osgi.util.promise.Promise) ArrayList(java.util.ArrayList) List(java.util.List) Failure(org.osgi.util.promise.Failure)

Example 9 with Deferred

use of org.osgi.util.promise.Deferred in project felix by apache.

the class ConfigurableComponentHolder method enableComponents.

public Promise<Void> enableComponents(final boolean async) {
    synchronized (enableLock) {
        if (m_enablePromise != null) {
            return m_enablePromise;
        }
        wait(m_disablePromise);
        List<AbstractComponentManager<S>> cms = new ArrayList<AbstractComponentManager<S>>();
        synchronized (m_components) {
            if (isSatisfied()) {
                if (m_factoryPidIndex == null || (m_componentMetadata.isObsoleteFactoryComponentFactory() && !m_componentMetadata.isConfigurationRequired())) {
                    m_singleComponent = createComponentManager(false);
                    cms.add(m_singleComponent);
                    m_singleComponent.reconfigure(mergeProperties(null), false, null);
                }
                if (m_factoryPidIndex != null) {
                    for (String pid : m_factoryConfigurations.keySet()) {
                        AbstractComponentManager<S> scm = createComponentManager(true);
                        m_components.put(pid, scm);
                        scm.reconfigure(mergeProperties(pid), false, new TargetedPID(pid));
                        cms.add(scm);
                    }
                }
            }
            m_enabled = true;
        }
        List<Promise<Void>> promises = new ArrayList<Promise<Void>>();
        for (AbstractComponentManager<S> cm : cms) {
            promises.add(cm.enable(async));
        }
        m_enablePromise = new Deferred<List<Void>>().resolveWith(Promises.<Void, Void>all(promises));
        m_disablePromise = null;
        return m_enablePromise;
    }
}
Also used : Promise(org.osgi.util.promise.Promise) Deferred(org.osgi.util.promise.Deferred) ArrayList(java.util.ArrayList) TargetedPID(org.apache.felix.scr.impl.metadata.TargetedPID)

Example 10 with Deferred

use of org.osgi.util.promise.Deferred in project aries by apache.

the class ChainTest method testThenFailException.

@Test
public void testThenFailException() throws Exception {
    Deferred<String> def = new Deferred<String>();
    final Promise<String> promise = def.getPromise();
    final Exception thenException = new Exception("eek!");
    Promise<String> chain = promise.then(null, new Failure() {

        @Override
        public void fail(Promise<?> resolved) throws Exception {
            throw thenException;
        }
    });
    assertFalse("chain not resolved", chain.isDone());
    def.fail(new Throwable("failed"));
    assertTrue("chain resolved", chain.isDone());
    assertEquals("chain failure matches", thenException, chain.getFailure());
}
Also used : Deferred(org.osgi.util.promise.Deferred) TimeoutException(org.osgi.util.promise.TimeoutException) Failure(org.osgi.util.promise.Failure) Test(org.junit.Test)

Aggregations

Deferred (org.osgi.util.promise.Deferred)19 Test (org.junit.Test)12 TimeoutException (org.osgi.util.promise.TimeoutException)9 Promise (org.osgi.util.promise.Promise)5 ArrayList (java.util.ArrayList)4 Callback (org.osgi.util.function.Callback)4 List (java.util.List)3 CountDownLatch (java.util.concurrent.CountDownLatch)3 AtomicLong (java.util.concurrent.atomic.AtomicLong)3 Failure (org.osgi.util.promise.Failure)3 InvocationTargetException (java.lang.reflect.InvocationTargetException)2 ConcurrentModificationException (java.util.ConcurrentModificationException)2 NoSuchElementException (java.util.NoSuchElementException)2 AtomicBoolean (java.util.concurrent.atomic.AtomicBoolean)2 LongAdder (java.util.concurrent.atomic.LongAdder)2 FailedPromisesException (org.osgi.util.promise.FailedPromisesException)2 TaggedData (aQute.bnd.service.url.TaggedData)1 URI (java.net.URI)1 CompletableFuture (java.util.concurrent.CompletableFuture)1 CompletionStage (java.util.concurrent.CompletionStage)1