Search in sources :

Example 16 with Deferred

use of org.osgi.util.promise.Deferred 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 17 with Deferred

use of org.osgi.util.promise.Deferred 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 18 with Deferred

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

the class AbstractPushStreamImpl method count.

@Override
public Promise<Long> count() {
    Deferred<Long> d = new Deferred<>();
    LongAdder counter = new LongAdder();
    updateNext((event) -> {
        try {
            switch(event.getType()) {
                case DATA:
                    counter.add(1);
                    return CONTINUE;
                case CLOSE:
                    d.resolve(Long.valueOf(counter.sum()));
                    break;
                case ERROR:
                    d.fail(event.getFailure());
                    break;
            }
            close(event.nodata());
            return ABORT;
        } catch (Exception e) {
            close(PushEvent.error(e));
            return ABORT;
        }
    });
    begin();
    return d.getPromise();
}
Also used : LongAdder(java.util.concurrent.atomic.LongAdder) Deferred(org.osgi.util.promise.Deferred) AtomicLong(java.util.concurrent.atomic.AtomicLong) ConcurrentModificationException(java.util.ConcurrentModificationException) NoSuchElementException(java.util.NoSuchElementException)

Example 19 with Deferred

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

the class AbstractPushStreamImpl method forEachEvent.

@Override
public Promise<Long> forEachEvent(PushEventConsumer<? super T> action) {
    Deferred<Long> d = new Deferred<>();
    LongAdder la = new LongAdder();
    updateNext((event) -> {
        try {
            switch(event.getType()) {
                case DATA:
                    long value = action.accept(event);
                    la.add(value);
                    return value;
                case CLOSE:
                    try {
                        action.accept(event);
                    } finally {
                        d.resolve(Long.valueOf(la.sum()));
                    }
                    break;
                case ERROR:
                    try {
                        action.accept(event);
                    } finally {
                        d.fail(event.getFailure());
                    }
                    break;
            }
            return ABORT;
        } catch (Exception e) {
            close(PushEvent.error(e));
            return ABORT;
        }
    });
    begin();
    return d.getPromise();
}
Also used : LongAdder(java.util.concurrent.atomic.LongAdder) Deferred(org.osgi.util.promise.Deferred) AtomicLong(java.util.concurrent.atomic.AtomicLong) ConcurrentModificationException(java.util.ConcurrentModificationException) NoSuchElementException(java.util.NoSuchElementException)

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