|
40 | 40 | import org.mockito.stubbing.Answer;
|
41 | 41 | import rx.Single.OnSubscribe;
|
42 | 42 | import rx.exceptions.CompositeException;
|
| 43 | +import rx.functions.Action; |
43 | 44 | import rx.functions.Action0;
|
44 | 45 | import rx.functions.Action1;
|
45 | 46 | import rx.functions.Func1;
|
@@ -828,4 +829,66 @@ public void deferShouldPassNullPointerExceptionToTheSubscriberIfSingleFactoryRet
|
828 | 829 |
|
829 | 830 | verify(singleFactory).call();
|
830 | 831 | }
|
| 832 | + |
| 833 | + @Test |
| 834 | + public void doOnUnsubscribeShouldInvokeActionAfterSuccess() { |
| 835 | + Action0 action = mock(Action0.class); |
| 836 | + |
| 837 | + Single<String> single = Single |
| 838 | + .just("test") |
| 839 | + .doOnUnsubscribe(action); |
| 840 | + |
| 841 | + verifyZeroInteractions(action); |
| 842 | + |
| 843 | + TestSubscriber<String> testSubscriber = new TestSubscriber<String>(); |
| 844 | + single.subscribe(testSubscriber); |
| 845 | + |
| 846 | + testSubscriber.assertValue("test"); |
| 847 | + testSubscriber.assertCompleted(); |
| 848 | + |
| 849 | + verify(action).call(); |
| 850 | + } |
| 851 | + |
| 852 | + @Test |
| 853 | + public void doOnUnsubscribeShouldInvokeActionAfterError() { |
| 854 | + Action0 action = mock(Action0.class); |
| 855 | + |
| 856 | + Single<Object> single = Single |
| 857 | + .error(new RuntimeException("test")) |
| 858 | + .doOnUnsubscribe(action); |
| 859 | + |
| 860 | + verifyZeroInteractions(action); |
| 861 | + |
| 862 | + TestSubscriber<Object> testSubscriber = new TestSubscriber<Object>(); |
| 863 | + single.subscribe(testSubscriber); |
| 864 | + |
| 865 | + testSubscriber.assertError(RuntimeException.class); |
| 866 | + assertEquals("test", testSubscriber.getOnErrorEvents().get(0).getMessage()); |
| 867 | + |
| 868 | + verify(action).call(); |
| 869 | + } |
| 870 | + |
| 871 | + @Test |
| 872 | + public void doOnUnsubscribeShouldInvokeActionAfterExplicitUnsubscription() { |
| 873 | + Action0 action = mock(Action0.class); |
| 874 | + |
| 875 | + Single<Object> single = Single |
| 876 | + .create(new OnSubscribe<Object>() { |
| 877 | + @Override |
| 878 | + public void call(SingleSubscriber<? super Object> singleSubscriber) { |
| 879 | + // Broken Single that never ends itself (simulates long computation in one thread). |
| 880 | + } |
| 881 | + }) |
| 882 | + .doOnUnsubscribe(action); |
| 883 | + |
| 884 | + TestSubscriber<Object> testSubscriber = new TestSubscriber<Object>(); |
| 885 | + Subscription subscription = single.subscribe(testSubscriber); |
| 886 | + |
| 887 | + verifyZeroInteractions(action); |
| 888 | + |
| 889 | + subscription.unsubscribe(); |
| 890 | + verify(action).call(); |
| 891 | + testSubscriber.assertNoValues(); |
| 892 | + testSubscriber.assertNoTerminalEvent(); |
| 893 | + } |
831 | 894 | }
|
0 commit comments