Search in sources :

Example 1 with Deferred

use of org.osgi.util.promise.Deferred in project ecf by eclipse.

the class AbstractAsyncProxyRemoteService method callFuture.

@SuppressWarnings("unchecked")
protected Object callFuture(AbstractAsyncProxyRemoteCall call, @SuppressWarnings("rawtypes") Class returnType) {
    if (Promise.class.isAssignableFrom(returnType)) {
        @SuppressWarnings("rawtypes") Deferred d = new Deferred();
        callCompletableAsync(call, (r, hadException, exception) -> {
            if (hadException)
                d.fail(exception);
            else
                d.resolve(r);
        });
        return d.getPromise();
    }
    // we callCompletableAsync
    if (CompletableFuture.class.isAssignableFrom(returnType) || CompletionStage.class.isAssignableFrom(returnType)) {
        @SuppressWarnings("rawtypes") CompletableFuture result = new CompletableFuture();
        callCompletableAsync(call, (r, hadException, exception) -> {
            if (hadException)
                result.completeExceptionally(exception);
            else
                result.complete(r);
        });
        // And return the CompletableFuture
        return result;
    }
    // IFuture result of callAsync
    if (IFuture.class.isAssignableFrom(returnType))
        return callAsync(call);
    // Else it must be a Future return value
    return callFutureAsync(call);
}
Also used : CompletableFuture(java.util.concurrent.CompletableFuture) Deferred(org.osgi.util.promise.Deferred) CompletionStage(java.util.concurrent.CompletionStage)

Example 2 with Deferred

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

the class ChainTest method testThenFail.

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

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

Example 3 with Deferred

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

the class ChainTest method testThenSuccessException.

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

        @Override
        public Promise<String> call(Promise<String> resolved) throws Exception {
            throw thenException;
        }
    });
    assertFalse("chain not resolved", chain.isDone());
    def.resolve("ok");
    assertTrue("chain resolved", chain.isDone());
    assertEquals("chain failure matches", thenException, chain.getFailure());
}
Also used : Promise(org.osgi.util.promise.Promise) Deferred(org.osgi.util.promise.Deferred) TimeoutException(org.osgi.util.promise.TimeoutException) Test(org.junit.Test)

Example 4 with Deferred

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

the class ChainTest method testTimeoutFailure.

@Test
public void testTimeoutFailure() throws Exception {
    Deferred<String> def = new Deferred<String>();
    Promise<String> promise = def.getPromise();
    final CountDownLatch latch = new CountDownLatch(1);
    Promise<String> chain = promise.timeout(500).onResolve(new Runnable() {

        @Override
        public void run() {
            latch.countDown();
        }
    });
    assertFalse("promise should not be resolved", promise.isDone());
    assertFalse("chain should not be resolved", chain.isDone());
    Exception failure = new Exception("bang!");
    def.fail(failure);
    assertTrue("Did not eagerly complete!", latch.await(100, MILLISECONDS));
    assertTrue("promise should not be resolved", promise.isDone());
    assertTrue("chain should now be resolved", chain.isDone());
    assertSame(promise.getFailure(), chain.getFailure());
}
Also used : Deferred(org.osgi.util.promise.Deferred) CountDownLatch(java.util.concurrent.CountDownLatch) TimeoutException(org.osgi.util.promise.TimeoutException) Test(org.junit.Test)

Example 5 with Deferred

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

the class ChainTest method testThenCallbackThrowsExceptionFail.

@Test
public void testThenCallbackThrowsExceptionFail() throws Exception {
    Deferred<String> def = new Deferred<String>();
    final Exception failure = new Exception("bang!");
    Promise<String> chain = def.getPromise().then(new Callback() {

        @Override
        public void run() throws Exception {
            throw failure;
        }
    });
    assertFalse("chain should not be resolved", chain.isDone());
    def.fail(new IllegalStateException());
    assertTrue("chain resolved", chain.isDone());
    assertSame("chain value matches", failure, chain.getFailure());
}
Also used : Callback(org.osgi.util.function.Callback) Deferred(org.osgi.util.promise.Deferred) TimeoutException(org.osgi.util.promise.TimeoutException) 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