Search in sources :

Example 16 with Promise

use of org.osgi.util.promise.Promise in project aries by apache.

the class AsyncServiceTest method test.

@Test
public void test() throws InterruptedException {
    DelayedEcho raw = new DelayedEcho();
    AsyncService service = new AsyncService(null, es, ses, serviceTracker);
    DelayedEcho mediated = service.mediate(raw, DelayedEcho.class);
    Promise<String> promise = service.call(mediated.echo("Hello World", 1000));
    final CountDownLatch latch = new CountDownLatch(1);
    promise.filter(new Predicate<String>() {

        @Override
        public boolean test(String t) {
            return "Hello World".equals(t);
        }
    }).then(new Success<String, Void>() {

        @Override
        public Promise<Void> call(Promise<String> resolved) throws Exception {
            latch.countDown();
            return null;
        }
    });
    assertTrue(latch.await(5, TimeUnit.SECONDS));
}
Also used : Promise(org.osgi.util.promise.Promise) CountDownLatch(java.util.concurrent.CountDownLatch) Predicate(org.osgi.util.function.Predicate) Test(org.junit.Test)

Example 17 with Promise

use of org.osgi.util.promise.Promise in project aries by apache.

the class PromisesTest method testLatch.

// T = String
private void testLatch(boolean preResolve, String... rv) throws Exception {
    @SuppressWarnings("unchecked") Deferred<String>[] dv = new Deferred[rv.length];
    @SuppressWarnings("unchecked") Promise<String>[] pv = new Promise[rv.length];
    for (int i = 0; i < rv.length; i++) {
        dv[i] = new Deferred<String>();
        pv[i] = dv[i].getPromise();
    }
    Promise<List<String>> latch = null;
    if (!preResolve) {
        Promise<List<String>> latch2 = Promises.all(pv);
        latch = latch2;
        if (rv.length == 0) {
            assertTrue("latch resolved", latch.isDone());
            return;
        }
        assertFalse("latch not resolved", latch.isDone());
    }
    int nFail = 0;
    for (int i = 0; i < rv.length; i++) {
        String res = rv[i];
        if (res.startsWith("!")) {
            dv[i].fail(new Exception(res));
            nFail++;
        } else {
            dv[i].resolve(res);
        }
    }
    if (preResolve) {
        Promise<List<String>> latch2 = Promises.all(pv);
        latch = latch2;
    }
    assertTrue("latch resolved", latch.isDone());
    if (nFail > 0) {
        @SuppressWarnings({ "not thrown", "all" }) Throwable failure = latch.getFailure();
        assertTrue("failure instanceof FailedPromisesException", failure instanceof FailedPromisesException);
        Collection<Promise<?>> failedPromises = ((FailedPromisesException) failure).getFailedPromises();
        assertEquals("failedPromises size matches", nFail, failedPromises.size());
        for (int i = 0; i < rv.length; i++) {
            Promise<String> promise = pv[i];
            if (rv[i].startsWith("!")) {
                assertTrue("failedPromises contains", failedPromises.contains(promise));
            } else {
                assertFalse("failedPromises doesn't contain", failedPromises.contains(promise));
            }
        }
    } else {
        List<String> list = latch.getValue();
        assertEquals("list size matches", rv.length, list.size());
        for (int i = 0; i < rv.length; i++) {
            assertEquals("list[i] matches", rv[i], list.get(i));
        }
        // check list is modifiable
        list.add(0, "new item");
        assertEquals("list modifiable", "new item", list.get(0));
    }
}
Also used : Deferred(org.osgi.util.promise.Deferred) FailedPromisesException(org.osgi.util.promise.FailedPromisesException) FailedPromisesException(org.osgi.util.promise.FailedPromisesException) Promise(org.osgi.util.promise.Promise) List(java.util.List) ArrayList(java.util.ArrayList)

Example 18 with Promise

use of org.osgi.util.promise.Promise in project aries by apache.

the class PromisesTest method testLatch.

// T = Number
// S = Integer
private void testLatch(Integer... rv) throws Exception {
    @SuppressWarnings("unchecked") Deferred<Integer>[] dv = new Deferred[rv.length];
    List<Promise<Integer>> promises = new ArrayList<Promise<Integer>>();
    for (int i = 0; i < rv.length; i++) {
        dv[i] = new Deferred<Integer>();
        promises.add(dv[i].getPromise());
    }
    Promise<List<Number>> latch = Promises.all(promises);
    if (rv.length == 0) {
        assertTrue("latch resolved", latch.isDone());
        return;
    }
    assertFalse("latch not resolved", latch.isDone());
    int nFail = 0;
    for (int i = 0; i < rv.length; i++) {
        Integer res = rv[i];
        if (res < 0) {
            dv[i].fail(new Exception("fail" + res));
            nFail++;
        } else {
            dv[i].resolve(res);
        }
    }
    assertTrue("latch resolved", latch.isDone());
    if (nFail > 0) {
        @SuppressWarnings({ "not thrown", "all" }) Throwable failure = latch.getFailure();
        assertTrue("failure instanceof FailedPromisesException", failure instanceof FailedPromisesException);
        Collection<Promise<?>> failedPromises = ((FailedPromisesException) failure).getFailedPromises();
        assertEquals("failedPromises size matches", nFail, failedPromises.size());
        for (int i = 0; i < rv.length; i++) {
            Promise<Integer> promise = promises.get(i);
            if (rv[i] < 0) {
                assertTrue("failedPromises contains", failedPromises.contains(promise));
            } else {
                assertFalse("failedPromises doesn't contain", failedPromises.contains(promise));
            }
        }
    } else {
        List<Number> list = latch.getValue();
        assertEquals("list size matches", rv.length, list.size());
        for (int i = 0; i < rv.length; i++) {
            assertEquals("list[i] matches", rv[i], list.get(i));
        }
        // check list is modifiable
        list.add(0, 3.14);
        assertEquals("list modifiable", 3.14, list.get(0));
    }
}
Also used : Deferred(org.osgi.util.promise.Deferred) ArrayList(java.util.ArrayList) FailedPromisesException(org.osgi.util.promise.FailedPromisesException) FailedPromisesException(org.osgi.util.promise.FailedPromisesException) Promise(org.osgi.util.promise.Promise) List(java.util.List) ArrayList(java.util.ArrayList)

Example 19 with Promise

use of org.osgi.util.promise.Promise in project aries by apache.

the class PromiseImpl method resolveWith.

public Promise<Void> resolveWith(final Promise<? extends T> with) {
    if (with == null)
        throw new NullPointerException();
    final PromiseImpl<Void> result = new PromiseImpl<Void>(exec, ses);
    with.then(new Success<T, T>() {

        @Override
        public Promise<T> call(Promise<T> resolved) throws Exception {
            if (isDone()) {
                result.fail(new IllegalStateException("associated Promise already resolved"));
            }
            PromiseImpl.this.resolve(resolved.getValue());
            result.resolve(null);
            return null;
        }
    }, new Failure() {

        @Override
        public void fail(Promise<?> resolved) throws Exception {
            if (isDone()) {
                result.fail(new IllegalStateException("associated Promise already resolved"));
            }
            PromiseImpl.this.fail(resolved.getFailure());
            result.resolve(null);
        }
    });
    return result;
}
Also used : InvocationTargetException(java.lang.reflect.InvocationTargetException) RejectedExecutionException(java.util.concurrent.RejectedExecutionException) NoSuchElementException(java.util.NoSuchElementException) TimeoutException(org.osgi.util.promise.TimeoutException) Promise(org.osgi.util.promise.Promise) Failure(org.osgi.util.promise.Failure)

Example 20 with Promise

use of org.osgi.util.promise.Promise in project aries by apache.

the class PromiseImpl method recover.

@Override
public Promise<T> recover(final Function<Promise<?>, ? extends T> recovery) {
    if (recovery == null)
        throw new NullPointerException();
    final PromiseImpl<T> result = new PromiseImpl<T>(exec, ses);
    then(new Success<T, T>() {

        @Override
        public Promise<T> call(Promise<T> resolved) throws Exception {
            result.resolve(resolved.getValue());
            return null;
        }
    }, new Failure() {

        @Override
        public void fail(Promise<?> resolved) throws Exception {
            try {
                T recover = recovery.apply(resolved);
                if (recover != null) {
                    result.resolve(recover);
                } else {
                    result.fail(resolved.getFailure());
                }
            } catch (Throwable t) {
                result.fail(t);
            }
        }
    });
    return result;
}
Also used : Promise(org.osgi.util.promise.Promise) InvocationTargetException(java.lang.reflect.InvocationTargetException) RejectedExecutionException(java.util.concurrent.RejectedExecutionException) NoSuchElementException(java.util.NoSuchElementException) TimeoutException(org.osgi.util.promise.TimeoutException) Failure(org.osgi.util.promise.Failure)

Aggregations

Promise (org.osgi.util.promise.Promise)21 Failure (org.osgi.util.promise.Failure)11 ArrayList (java.util.ArrayList)9 TimeoutException (org.osgi.util.promise.TimeoutException)9 InvocationTargetException (java.lang.reflect.InvocationTargetException)8 List (java.util.List)8 NoSuchElementException (java.util.NoSuchElementException)8 RejectedExecutionException (java.util.concurrent.RejectedExecutionException)8 File (java.io.File)4 URI (java.net.URI)4 Deferred (org.osgi.util.promise.Deferred)4 ResourcesRepository (aQute.bnd.osgi.repository.ResourcesRepository)3 Resource (org.osgi.resource.Resource)3 BridgeRepository (aQute.bnd.osgi.repository.BridgeRepository)2 IdentityCapability (aQute.bnd.osgi.resource.ResourceUtils.IdentityCapability)2 DownloadListenerPromise (aQute.bnd.util.repository.DownloadListenerPromise)2 PromiseCollectors.toPromise (aQute.lib.promise.PromiseCollectors.toPromise)2 Reporter (aQute.service.reporter.Reporter)2 Collections (java.util.Collections)2 HashMap (java.util.HashMap)2