Search in sources :

Example 11 with Deferred

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());
}
Also used : AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) Callback(org.osgi.util.function.Callback) Deferred(org.osgi.util.promise.Deferred) TimeoutException(org.osgi.util.promise.TimeoutException) Test(org.junit.Test)

Example 12 with Deferred

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());
}
Also used : Callback(org.osgi.util.function.Callback) Deferred(org.osgi.util.promise.Deferred) TimeoutException(org.osgi.util.promise.TimeoutException) Test(org.junit.Test)

Example 13 with Deferred

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

Example 14 with Deferred

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

Example 15 with Deferred

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());
}
Also used : AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) 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