Search in sources :

Example 1 with Indexable

use of com.microsoft.azure.arm.model.Indexable in project autorest-clientruntime-for-java by Azure.

the class DAGErrorTests method testCompositeError.

@Test
public void testCompositeError() {
    // Terminate on error strategy used in this task group is
    // TaskGroupTerminateOnErrorStrategy::TERMINATE_ON_IN_PROGRESS_TASKS_COMPLETION
    // Tasks marked X (B & G) will fault. B and G are not depends on each other.
    // If B start at time 't0'th ms then G starts ~'t1 = (t0 + 250)'th ms.
    // After B start, it asynchronously wait and emit an error at time '(t0 + 3500)' ms.
    // In this setup, G gets ~3250 ms to start before B emits error. Eventually G also
    // emit error.
    // The final stream, emits result of all tasks that B and G directly or indirectly
    // depends on and terminate with composite exception (that composes exception from
    // B and G).
    /**
     *                                                                        |--------->[M](0)
     *                                                                        |
     *                                                       |==============>[J](1)----->[N](0)
     *                                 X                     |
     *   |------------------>[D](4)-->[B](3)--------------->[A](2)======================>[K](0)
     *   |                             ^                     ^
     *   |                             |                     |
     *  [F](6)---->[E](5)--------------|                     |
     *   |          |         X                              |
     *   |          |------->[G](4)-->[C](3)------------------
     *   |                    |
     *   |                    |============================>[L](2)---------->[P](1)=====>[Q](0)
     *   |
     *   |------------------------------------------------------------------>[H](1)----->[I](0)
     */
    PancakeImpl pancakeM = new PancakeImpl("M", 250);
    PancakeImpl pancakeN = new PancakeImpl("N", 250);
    PancakeImpl pancakeK = new PancakeImpl("K", 250);
    PancakeImpl pancakeQ = new PancakeImpl("Q", 250);
    PancakeImpl pancakeI = new PancakeImpl("I", 250);
    PancakeImpl pancakeJ = new PancakeImpl("J", 250);
    pancakeJ.withInstantPancake(pancakeM);
    pancakeJ.withInstantPancake(pancakeN);
    PancakeImpl pancakeP = new PancakeImpl("P", 250);
    pancakeP.withDelayedPancake(pancakeQ);
    PancakeImpl pancakeH = new PancakeImpl("H", 250);
    pancakeH.withInstantPancake(pancakeI);
    PancakeImpl pancakeA = new PancakeImpl("A", 250);
    PancakeImpl pancakeL = new PancakeImpl("L", 250);
    pancakeL.withInstantPancake(pancakeP);
    // Task B wait for 3500 ms then emit error
    PancakeImpl pancakeB = new PancakeImpl("B", 3500, true);
    pancakeB.withInstantPancake(pancakeA);
    PancakeImpl pancakeC = new PancakeImpl("C", 250);
    pancakeC.withInstantPancake(pancakeA);
    PancakeImpl pancakeD = new PancakeImpl("D", 250);
    pancakeD.withInstantPancake(pancakeB);
    // Task G wait for 250 ms then emit error
    PancakeImpl pancakeG = new PancakeImpl("G", 250, true);
    pancakeG.withInstantPancake(pancakeC);
    pancakeG.withDelayedPancake(pancakeL);
    PancakeImpl pancakeE = new PancakeImpl("E", 250);
    pancakeE.withInstantPancake(pancakeB);
    pancakeE.withInstantPancake(pancakeG);
    PancakeImpl pancakeF = new PancakeImpl("F", 250);
    pancakeF.withInstantPancake(pancakeD);
    pancakeF.withInstantPancake(pancakeE);
    pancakeF.withInstantPancake(pancakeH);
    pancakeA.withDelayedPancake(pancakeJ);
    pancakeA.withDelayedPancake(pancakeK);
    final Set<String> expectedToSee = new TreeSet<>();
    expectedToSee.add("M");
    expectedToSee.add("N");
    expectedToSee.add("K");
    expectedToSee.add("Q");
    expectedToSee.add("I");
    expectedToSee.add("J");
    expectedToSee.add("P");
    expectedToSee.add("H");
    expectedToSee.add("A");
    expectedToSee.add("L");
    expectedToSee.add("C");
    final Set<String> seen = new TreeSet<>();
    final List<Throwable> exceptions = new ArrayList<>();
    TaskGroup pancakeFtg = pancakeF.taskGroup();
    TaskGroup.InvocationContext context = pancakeFtg.newInvocationContext().withTerminateOnErrorStrategy(TaskGroupTerminateOnErrorStrategy.TERMINATE_ON_IN_PROGRESS_TASKS_COMPLETION);
    IPancake rootPancake = pancakeFtg.invokeAsync(context).map(new Func1<Indexable, IPancake>() {

        @Override
        public IPancake call(Indexable indexable) {
            IPancake pancake = (IPancake) indexable;
            String name = pancake.name();
            System.out.println("map.onNext:" + name);
            seen.add(name);
            return pancake;
        }
    }).onErrorResumeNext(new Func1<Throwable, Observable<IPancake>>() {

        @Override
        public Observable<IPancake> call(Throwable throwable) {
            System.out.println("map.onErrorResumeNext:" + throwable);
            exceptions.add(throwable);
            return Observable.empty();
        }
    }).toBlocking().last();
    Assert.assertTrue(Sets.difference(expectedToSee, seen).isEmpty());
    Assert.assertEquals(exceptions.size(), 1);
    Assert.assertTrue(exceptions.get(0) instanceof CompositeException);
    CompositeException compositeException = (CompositeException) exceptions.get(0);
    Assert.assertEquals(compositeException.getExceptions().size(), 2);
    for (Throwable throwable : compositeException.getExceptions()) {
        String message = throwable.getMessage();
        Assert.assertTrue(message.equalsIgnoreCase("B") || message.equalsIgnoreCase("G"));
    }
}
Also used : CompositeException(rx.exceptions.CompositeException) ArrayList(java.util.ArrayList) Observable(rx.Observable) TreeSet(java.util.TreeSet) Indexable(com.microsoft.azure.arm.model.Indexable) Func1(rx.functions.Func1) Test(org.junit.Test)

Aggregations

Indexable (com.microsoft.azure.arm.model.Indexable)1 ArrayList (java.util.ArrayList)1 TreeSet (java.util.TreeSet)1 Test (org.junit.Test)1 Observable (rx.Observable)1 CompositeException (rx.exceptions.CompositeException)1 Func1 (rx.functions.Func1)1