package com.github.davidmoten.rx;

import static java.util.Arrays.asList;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertTrue;

import java.util.ArrayList;
import java.util.List;

import org.junit.Test;

import rx.functions.Action1;
import rx.subjects.PublishSubject;

/**
 * Unit test for retry operator in RxJava which has shown some fragility lately.
 *
 */
public class RetryTest {

    // TODO move this test as pull request to rxjava-core.
    /**
     * Overlaps somewhat with testSourceObservableCallsUnsubscribe() but is
     * simpler and synchronous. This test fails against 0.16.1-0.17.4, hangs on
     * 0.17.5 and passes in 0.17.6 thanks to fix for issue #1027.
     */
    @Test
    public void testRetrySubscribesAgainAfterError() {
        List<Integer> list = new ArrayList<Integer>();
        PublishSubject<Integer> subject = PublishSubject.create();
        subject
        // record item
        .doOnNext(addToList(list))
        // throw a RuntimeException
                .doOnNext(throwException())
                // retry on error
                .retry()
                // subscribe and ignore
                .subscribe();
        assertTrue(list.isEmpty());
        subject.onNext(1);
        assertEquals(asList(1), list);
        subject.onNext(2);
        assertEquals(asList(1, 2), list);
        subject.onNext(3);
        assertEquals(asList(1, 2, 3), list);
    }

    private Action1<Integer> throwException() {
        return new Action1<Integer>() {
            @Override
            public void call(Integer t1) {
                throw new RuntimeException("boo");
            }
        };
    }

    private Action1<Integer> addToList(final List<Integer> list) {
        return new Action1<Integer>() {
            @Override
            public void call(Integer n) {
                list.add(n);
            }
        };
    }
}

@@ -39,202 +39,78 @@
 ort 
-static java.util.Arrays.asList;%0Aimport static org.junit.Assert.assertEquals;%0Aimport static org.junit.Assert.assertTrue;%0A%0Aimport java.util.ArrayList;%0Aimport java.util.List;%0A%0Aimport org.junit.Test
+org.junit.Test;%0Aimport org.mockito.InOrder;%0Aimport org.mockito.Mockito
 ;%0A%0Ai
@@ -661,20 +661,23 @@
         
-List
+Action1
 %3CInteger
@@ -682,38 +682,94 @@
 er%3E 
-list = new ArrayList%3CInteger%3E(
+record = Mockito.mock(Action1.class);%0A        InOrder inOrder = Mockito.inOrder(record
 );%0A 
@@ -895,23 +895,14 @@
 ext(
-addToList(list)
+record
 )%0A  
@@ -1120,33 +1120,41 @@
     
-assertTrue(list.isEmpty()
+inOrder.verifyNoMoreInteractions(
 );%0A 
@@ -1191,36 +1191,37 @@
     
-assertEquals(asList(1), list
+inOrder.verify(record).call(1
 );%0A 
@@ -1258,39 +1258,37 @@
     
-assertEquals(asList(1, 2), list
+inOrder.verify(record).call(2
 );%0A 
@@ -1325,42 +1325,81 @@
     
-assertEquals(asList(1, 2, 3), list
+inOrder.verify(record).call(3);%0A        inOrder.verifyNoMoreInteractions(
 );%0A 
@@ -1644,237 +1644,6 @@
  %7D%0A%0A
-    private Action1%3CInteger%3E addToList(final List%3CInteger%3E list) %7B%0A        return new Action1%3CInteger%3E() %7B%0A            @Override%0A            public void call(Integer n) %7B%0A                list.add(n);%0A            %7D%0A        %7D;%0A    %7D%0A
 %7D%0A

use mocks in test