Skip to content

Commit 0c0a18f

Browse files
Add Single.doOnSuccess()
1 parent 1db23ee commit 0c0a18f

File tree

2 files changed

+112
-0
lines changed

2 files changed

+112
-0
lines changed

src/main/java/rx/Single.java

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1864,4 +1864,38 @@ public void onNext(T t) {
18641864

18651865
return lift(new OperatorDoOnEach<T>(observer));
18661866
}
1867+
1868+
/**
1869+
* Modifies the source {@link Single} so that it invokes an action when it calls {@code onSuccess}.
1870+
* <p>
1871+
* <img width="640" height="310" src="https://raw.github.com/wiki/ReactiveX/RxJava/images/rx-operators/doOnNext.png" alt="">
1872+
* <dl>
1873+
* <dt><b>Scheduler:</b></dt>
1874+
* <dd>{@code doOnSuccess} does not operate by default on a particular {@link Scheduler}.</dd>
1875+
* </dl>
1876+
*
1877+
* @param onSuccess
1878+
* the action to invoke when the source {@link Single} calls {@code onSuccess}
1879+
* @return the source {@link Single} with the side-effecting behavior applied
1880+
* @see <a href="http://reactivex.io/documentation/operators/do.html">ReactiveX operators documentation: Do</a>
1881+
*/
1882+
@Experimental
1883+
public final Single<T> doOnSuccess(final Action1<? super T> onSuccess) {
1884+
Observer<T> observer = new Observer<T>() {
1885+
@Override
1886+
public void onCompleted() {
1887+
}
1888+
1889+
@Override
1890+
public void onError(Throwable e) {
1891+
}
1892+
1893+
@Override
1894+
public void onNext(T t) {
1895+
onSuccess.call(t);
1896+
}
1897+
};
1898+
1899+
return lift(new OperatorDoOnEach<T>(observer));
1900+
}
18671901
}

src/test/java/rx/SingleTest.java

Lines changed: 78 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
import static org.junit.Assert.assertSame;
1717
import static org.junit.Assert.assertTrue;
1818
import static org.junit.Assert.fail;
19+
import static org.mockito.Matchers.eq;
1920
import static org.mockito.Mockito.doThrow;
2021
import static org.mockito.Mockito.mock;
2122
import static org.mockito.Mockito.verify;
@@ -570,4 +571,81 @@ public void shouldPassErrorFromCallable() throws Exception {
570571

571572
verify(callable).call();
572573
}
574+
575+
@Test
576+
public void doOnSuccessShouldInvokeAction() {
577+
Action1<String> action = mock(Action1.class);
578+
579+
TestSubscriber<String> testSubscriber = new TestSubscriber<String>();
580+
581+
Single
582+
.just("value")
583+
.doOnSuccess(action)
584+
.subscribe(testSubscriber);
585+
586+
testSubscriber.assertValue("value");
587+
testSubscriber.assertNoErrors();
588+
589+
verify(action).call(eq("value"));
590+
}
591+
592+
@Test
593+
public void doOnSuccessShouldPassErrorFromActionToSubscriber() {
594+
Action1<String> action = mock(Action1.class);
595+
596+
Throwable error = new IllegalStateException();
597+
doThrow(error).when(action).call(eq("value"));
598+
599+
TestSubscriber<String> testSubscriber = new TestSubscriber<String>();
600+
601+
Single
602+
.just("value")
603+
.doOnSuccess(action)
604+
.subscribe(testSubscriber);
605+
606+
testSubscriber.assertNoValues();
607+
testSubscriber.assertError(error);
608+
609+
verify(action).call(eq("value"));
610+
}
611+
612+
@Test
613+
public void doOnSuccessShouldNotCallActionIfSingleThrowsError() {
614+
Action1<Object> action = mock(Action1.class);
615+
616+
Throwable error = new IllegalStateException();
617+
618+
TestSubscriber<Object> testSubscriber = new TestSubscriber<Object>();
619+
620+
Single
621+
.error(error)
622+
.doOnSuccess(action)
623+
.subscribe(testSubscriber);
624+
625+
testSubscriber.assertNoValues();
626+
testSubscriber.assertError(error);
627+
628+
verifyZeroInteractions(action);
629+
}
630+
631+
@Test
632+
public void doOnSuccessShouldNotSwallowExceptionThrownByAction() {
633+
Action1<String> action = mock(Action1.class);
634+
635+
Throwable exceptionFromAction = new IllegalStateException();
636+
637+
doThrow(exceptionFromAction).when(action).call(eq("value"));
638+
639+
TestSubscriber<String> testSubscriber = new TestSubscriber<String>();
640+
641+
Single
642+
.just("value")
643+
.doOnSuccess(action)
644+
.subscribe(testSubscriber);
645+
646+
testSubscriber.assertNoValues();
647+
testSubscriber.assertError(exceptionFromAction);
648+
649+
verify(action).call(eq("value"));
650+
}
573651
}

0 commit comments

Comments
 (0)