package com.github.davidmoten.rx;

import org.junit.Test;
import org.mockito.InOrder;
import org.mockito.Mockito;

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() {
        Action1<Integer> record = Mockito.mock(Action1.class);
        InOrder inOrder = Mockito.inOrder(record);
        PublishSubject<Integer> subject = PublishSubject.create();
        subject
        // record item
        .doOnNext(record)
        // throw a RuntimeException
                .doOnNext(throwException())
                // retry on error
                .retry()
                // subscribe and ignore
                .subscribe();
        inOrder.verifyNoMoreInteractions();
        subject.onNext(1);
        inOrder.verify(record).call(1);
        subject.onNext(2);
        inOrder.verify(record).call(2);
        subject.onNext(3);
        inOrder.verify(record).call(3);
        inOrder.verifyNoMoreInteractions();
    }

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

}

@@ -28,16 +28,143 @@
 en.rx;%0A%0A
+import static org.mockito.Mockito.doThrow;%0Aimport static org.mockito.Mockito.inOrder;%0Aimport static org.mockito.Mockito.mock;%0A%0A
 import o
@@ -496,58 +496,70 @@
   * 
-Overlaps somewhat with testSourceObservableCallsUn
+This test checks in a simple and synchronous way that retry re
 subs
@@ -567,16 +567,8 @@
 ribe
-() but i
 s%0A  
@@ -576,31 +576,19 @@
   * 
-simpler and synchronous
+after error
 . Th
@@ -632,23 +632,16 @@
 hangs on
-%0A     *
  0.17.5 
@@ -643,16 +643,23 @@
 17.5 and
+%0A     *
  passes 
@@ -707,16 +707,51 @@
      */%0A
+    @SuppressWarnings(%22unchecked%22)%0A
     @Tes
@@ -802,24 +802,75 @@
 terError() %7B
+%0A%0A        // record emitted values with this action
 %0A        Act
@@ -892,24 +892,16 @@
 ecord = 
-Mockito.
 mock(Act
@@ -939,24 +939,16 @@
 Order = 
-Mockito.
 inOrder(
@@ -955,16 +955,287 @@
 record);
+%0A%0A        // always throw and exception with this action%0A        Action1%3CInteger%3E throwException = mock(Action1.class);%0A        doThrow(new RuntimeException()).when(throwException).call(Mockito.anyInt());%0A%0A        // create a retrying observable based on a PublishSubject
 %0A       
@@ -1435,18 +1435,16 @@
 xception
-()
 )%0A      
@@ -1562,24 +1562,25 @@
 ubscribe();%0A
+%0A
         inOr
@@ -1607,24 +1607,25 @@
 ractions();%0A
+%0A
         subj
@@ -1675,24 +1675,25 @@
 d).call(1);%0A
+%0A
         subj
@@ -1743,24 +1743,25 @@
 d).call(2);%0A
+%0A
         subj
@@ -1811,24 +1811,25 @@
 d).call(3);%0A
+%0A
         inOr
@@ -1871,242 +1871,6 @@
  %7D%0A%0A
-    private Action1%3CInteger%3E throwException() %7B%0A        return new Action1%3CInteger%3E() %7B%0A            @Override%0A            public void call(Integer t1) %7B%0A                throw new RuntimeException(%22boo%22);%0A            %7D%0A        %7D;%0A    %7D%0A%0A
 %7D%0A

update retry test to use only mocks