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);
}
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());
}
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());
}
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());
}
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());
}
Aggregations