use of org.osgi.util.promise.Deferred in project aries by apache.
the class ChainTest method testThenCallbackSuccess.
@Test
public void testThenCallbackSuccess() throws Exception {
Deferred<String> def = new Deferred<String>();
final AtomicBoolean run = new AtomicBoolean(false);
Promise<String> chain = def.getPromise().then(new Callback() {
@Override
public void run() throws Exception {
run.set(true);
}
});
assertFalse("chain should not be resolved", chain.isDone());
assertFalse("callback should not have been run", run.get());
def.resolve("ok");
assertTrue("chain resolved", chain.isDone());
assertEquals("chain value matches", "ok", chain.getValue());
assertTrue("callback should have been run", run.get());
}
use of org.osgi.util.promise.Deferred in project aries by apache.
the class ChainTest method testThenCallbackThrowsExceptionSuccess.
@Test
public void testThenCallbackThrowsExceptionSuccess() 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.resolve("ok");
assertTrue("chain resolved", chain.isDone());
assertSame("chain value matches", failure, chain.getFailure());
}
use of org.osgi.util.promise.Deferred in project aries by apache.
the class ChainTest method testTimeoutSuccess.
@Test
public void testTimeoutSuccess() 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());
def.resolve("ok");
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());
assertEquals(promise.getValue(), chain.getValue());
}
use of org.osgi.util.promise.Deferred in project aries by apache.
the class ChainTest method testTimeout.
@Test
public void testTimeout() throws Exception {
Deferred<String> def = new Deferred<String>();
Promise<String> promise = def.getPromise();
long start = System.nanoTime();
final CountDownLatch latch = new CountDownLatch(1);
final AtomicLong finish = new AtomicLong();
Promise<String> chain = promise.timeout(500).onResolve(new Runnable() {
@Override
public void run() {
finish.set(System.nanoTime());
latch.countDown();
}
});
assertFalse("promise should not be resolved", promise.isDone());
assertFalse("chain should not be resolved", chain.isDone());
assertTrue("Did not time out!", latch.await(1, SECONDS));
assertTrue("Finished too fast", NANOSECONDS.toMillis(finish.get() - start) > 450);
assertFalse("promise should not be resolved", promise.isDone());
assertTrue("chain should now be resolved", chain.isDone());
assertTrue("Should fail with a timeout exception", chain.getFailure() instanceof TimeoutException);
}
use of org.osgi.util.promise.Deferred in project aries by apache.
the class ChainTest method testThenCallbackFail.
@Test
public void testThenCallbackFail() throws Exception {
Deferred<String> def = new Deferred<String>();
final AtomicBoolean run = new AtomicBoolean(false);
Promise<String> chain = def.getPromise().then(new Callback() {
@Override
public void run() throws Exception {
run.set(true);
}
});
Exception failure = new Exception("bang!");
assertFalse("chain should not be resolved", chain.isDone());
assertFalse("callback should not have been run", run.get());
def.fail(failure);
assertTrue("chain resolved", chain.isDone());
assertSame("chain value matches", failure, chain.getFailure());
assertTrue("callback should have been run", run.get());
}
Aggregations