Search in sources :

Example 1 with AggregateException

use of bolts.AggregateException in project Parse-SDK-Android by ParsePlatform.

the class ParseTaskUtilsTest method testWaitForTaskWrapsAggregateExceptionAsParseException.

/**
   * Verifies {@link bolts.AggregateException} gets wrapped with {@link ParseException} when thrown from
   * {@link com.parse.ParseTaskUtils#wait(bolts.Task)}.
   */
@Test
public void testWaitForTaskWrapsAggregateExceptionAsParseException() {
    final Exception error = new RuntimeException("This task failed.");
    final ArrayList<Task<Void>> tasks = new ArrayList<>();
    for (int i = 0; i < 20; i++) {
        final int number = i;
        Task<Void> task = Task.callInBackground(new Callable<Void>() {

            @Override
            public Void call() throws Exception {
                Thread.sleep((long) (Math.random() * 100));
                if (number == 10 || number == 11) {
                    throw error;
                }
                return null;
            }
        });
        tasks.add(task);
    }
    try {
        ParseTaskUtils.wait(Task.whenAll(tasks));
    } catch (ParseException e) {
        assertTrue(e.getCause() instanceof AggregateException);
    }
}
Also used : Task(bolts.Task) ArrayList(java.util.ArrayList) AggregateException(bolts.AggregateException) AggregateException(bolts.AggregateException) Test(org.junit.Test)

Example 2 with AggregateException

use of bolts.AggregateException in project Bolts-Java by BoltsFramework.

the class TaskTest method testWhenAllTwoErrors.

public void testWhenAllTwoErrors() {
    final Exception error = new RuntimeException("This task failed.");
    runTaskTest(new Callable<Task<?>>() {

        @Override
        public Task<?> call() throws Exception {
            final ArrayList<Task<Void>> tasks = new ArrayList<Task<Void>>();
            for (int i = 0; i < 20; i++) {
                final int number = i;
                Task<Void> task = Task.callInBackground(new Callable<Void>() {

                    @Override
                    public Void call() throws Exception {
                        Thread.sleep((long) (Math.random() * 1000));
                        if (number == 10 || number == 11) {
                            throw error;
                        }
                        return null;
                    }
                });
                tasks.add(task);
            }
            return Task.whenAll(tasks).continueWith(new Continuation<Void, Void>() {

                @Override
                public Void then(Task<Void> task) {
                    assertTrue(task.isCompleted());
                    assertTrue(task.isFaulted());
                    assertFalse(task.isCancelled());
                    assertTrue(task.getError() instanceof AggregateException);
                    assertEquals(2, ((AggregateException) task.getError()).getErrors().size());
                    assertEquals(error, ((AggregateException) task.getError()).getErrors().get(0));
                    assertEquals(error, ((AggregateException) task.getError()).getErrors().get(1));
                    for (Task<Void> t : tasks) {
                        assertTrue(t.isCompleted());
                    }
                    return null;
                }
            });
        }
    });
}
Also used : Task(bolts.Task) Continuation(bolts.Continuation) ArrayList(java.util.ArrayList) AggregateException(bolts.AggregateException) AggregateException(bolts.AggregateException) CancellationException(java.util.concurrent.CancellationException) Callable(java.util.concurrent.Callable)

Example 3 with AggregateException

use of bolts.AggregateException in project Bolts-Java by BoltsFramework.

the class TaskTest method testWhenAllOneError.

public void testWhenAllOneError() {
    final Exception error = new RuntimeException("This task failed.");
    runTaskTest(new Callable<Task<?>>() {

        @Override
        public Task<?> call() throws Exception {
            final ArrayList<Task<Void>> tasks = new ArrayList<Task<Void>>();
            for (int i = 0; i < 20; i++) {
                final int number = i;
                Task<Void> task = Task.callInBackground(new Callable<Void>() {

                    @Override
                    public Void call() throws Exception {
                        Thread.sleep((long) (Math.random() * 1000));
                        if (number == 10) {
                            throw error;
                        }
                        return null;
                    }
                });
                tasks.add(task);
            }
            return Task.whenAll(tasks).continueWith(new Continuation<Void, Void>() {

                @Override
                public Void then(Task<Void> task) {
                    assertTrue(task.isCompleted());
                    assertTrue(task.isFaulted());
                    assertFalse(task.isCancelled());
                    assertFalse(task.getError() instanceof AggregateException);
                    assertEquals(error, task.getError());
                    for (Task<Void> t : tasks) {
                        assertTrue(t.isCompleted());
                    }
                    return null;
                }
            });
        }
    });
}
Also used : Task(bolts.Task) Continuation(bolts.Continuation) ArrayList(java.util.ArrayList) AggregateException(bolts.AggregateException) AggregateException(bolts.AggregateException) CancellationException(java.util.concurrent.CancellationException) Callable(java.util.concurrent.Callable)

Aggregations

AggregateException (bolts.AggregateException)3 Task (bolts.Task)3 ArrayList (java.util.ArrayList)3 Continuation (bolts.Continuation)2 Callable (java.util.concurrent.Callable)2 CancellationException (java.util.concurrent.CancellationException)2 Test (org.junit.Test)1