Search in sources :

Example 1 with TimeoutException

use of org.osgi.util.promise.TimeoutException 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);
}
Also used : AtomicLong(java.util.concurrent.atomic.AtomicLong) Deferred(org.osgi.util.promise.Deferred) CountDownLatch(java.util.concurrent.CountDownLatch) TimeoutException(org.osgi.util.promise.TimeoutException) Test(org.junit.Test)

Aggregations

CountDownLatch (java.util.concurrent.CountDownLatch)1 AtomicLong (java.util.concurrent.atomic.AtomicLong)1 Test (org.junit.Test)1 Deferred (org.osgi.util.promise.Deferred)1 TimeoutException (org.osgi.util.promise.TimeoutException)1