Search in sources :

Example 1 with Callback

use of org.osgi.util.function.Callback 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)

Example 2 with Callback

use of org.osgi.util.function.Callback 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 3 with Callback

use of org.osgi.util.function.Callback 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 4 with Callback

use of org.osgi.util.function.Callback 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

Test (org.junit.Test)4 Callback (org.osgi.util.function.Callback)4 Deferred (org.osgi.util.promise.Deferred)4 TimeoutException (org.osgi.util.promise.TimeoutException)4 AtomicBoolean (java.util.concurrent.atomic.AtomicBoolean)2