|
20 | 20 | import static org.mockito.Matchers.eq;
|
21 | 21 | import static org.mockito.Mockito.doThrow;
|
22 | 22 | import static org.mockito.Mockito.mock;
|
| 23 | +import static org.mockito.Mockito.times; |
23 | 24 | import static org.mockito.Mockito.verify;
|
24 | 25 | import static org.mockito.Mockito.verifyZeroInteractions;
|
25 | 26 | import static org.mockito.Mockito.when;
|
|
30 | 31 | import java.util.concurrent.TimeUnit;
|
31 | 32 | import java.util.concurrent.TimeoutException;
|
32 | 33 | import java.util.concurrent.atomic.AtomicBoolean;
|
| 34 | +import java.util.concurrent.atomic.AtomicInteger; |
33 | 35 | import java.util.concurrent.atomic.AtomicReference;
|
34 | 36 |
|
35 | 37 | import org.junit.Test;
|
36 | 38 |
|
| 39 | +import org.mockito.invocation.InvocationOnMock; |
| 40 | +import org.mockito.stubbing.Answer; |
37 | 41 | import rx.Single.OnSubscribe;
|
38 | 42 | import rx.exceptions.CompositeException;
|
39 | 43 | import rx.functions.Action0;
|
@@ -699,4 +703,129 @@ public void call(SingleSubscriber<? super Integer> singleSubscriber) {
|
699 | 703 | subscriber.assertNoValues();
|
700 | 704 | subscriber.assertError(expected);
|
701 | 705 | }
|
| 706 | + |
| 707 | + @Test |
| 708 | + public void deferShouldNotCallFactoryFuncUntilSubscriberSubscribes() throws Exception { |
| 709 | + Callable<Single<Object>> singleFactory = mock(Callable.class); |
| 710 | + Single.defer(singleFactory); |
| 711 | + verifyZeroInteractions(singleFactory); |
| 712 | + } |
| 713 | + |
| 714 | + @Test |
| 715 | + public void deferShouldSubscribeSubscriberToSingleFromFactoryFuncAndEmitValue() throws Exception { |
| 716 | + Callable<Single<Object>> singleFactory = mock(Callable.class); |
| 717 | + Object value = new Object(); |
| 718 | + Single<Object> single = Single.just(value); |
| 719 | + |
| 720 | + when(singleFactory.call()).thenReturn(single); |
| 721 | + |
| 722 | + TestSubscriber<Object> testSubscriber = new TestSubscriber<Object>(); |
| 723 | + |
| 724 | + Single |
| 725 | + .defer(singleFactory) |
| 726 | + .subscribe(testSubscriber); |
| 727 | + |
| 728 | + testSubscriber.assertValue(value); |
| 729 | + testSubscriber.assertNoErrors(); |
| 730 | + |
| 731 | + verify(singleFactory).call(); |
| 732 | + } |
| 733 | + |
| 734 | + @Test |
| 735 | + public void deferShouldSubscribeSubscriberToSingleFromFactoryFuncAndEmitError() throws Exception { |
| 736 | + Callable<Single<Object>> singleFactory = mock(Callable.class); |
| 737 | + Throwable error = new IllegalStateException(); |
| 738 | + Single<Object> single = Single.error(error); |
| 739 | + |
| 740 | + when(singleFactory.call()).thenReturn(single); |
| 741 | + |
| 742 | + TestSubscriber<Object> testSubscriber = new TestSubscriber<Object>(); |
| 743 | + |
| 744 | + Single |
| 745 | + .defer(singleFactory) |
| 746 | + .subscribe(testSubscriber); |
| 747 | + |
| 748 | + testSubscriber.assertNoValues(); |
| 749 | + testSubscriber.assertError(error); |
| 750 | + |
| 751 | + verify(singleFactory).call(); |
| 752 | + } |
| 753 | + |
| 754 | + @Test |
| 755 | + public void deferShouldPassErrorFromSingleFactoryToTheSubscriber() throws Exception { |
| 756 | + Callable<Single<Object>> singleFactory = mock(Callable.class); |
| 757 | + Throwable errorFromSingleFactory = new IllegalStateException(); |
| 758 | + when(singleFactory.call()).thenThrow(errorFromSingleFactory); |
| 759 | + |
| 760 | + TestSubscriber<Object> testSubscriber = new TestSubscriber<Object>(); |
| 761 | + |
| 762 | + Single |
| 763 | + .defer(singleFactory) |
| 764 | + .subscribe(testSubscriber); |
| 765 | + |
| 766 | + testSubscriber.assertNoValues(); |
| 767 | + testSubscriber.assertError(errorFromSingleFactory); |
| 768 | + |
| 769 | + verify(singleFactory).call(); |
| 770 | + } |
| 771 | + |
| 772 | + @Test |
| 773 | + public void deferShouldCallSingleFactoryForEachSubscriber() throws Exception { |
| 774 | + Callable<Single<String>> singleFactory = mock(Callable.class); |
| 775 | + |
| 776 | + String[] values = {"1", "2", "3"}; |
| 777 | + final Single[] singles = new Single[]{Single.just(values[0]), Single.just(values[1]), Single.just(values[2])}; |
| 778 | + |
| 779 | + final AtomicInteger singleFactoryCallsCounter = new AtomicInteger(); |
| 780 | + |
| 781 | + when(singleFactory.call()).thenAnswer(new Answer<Single<String>>() { |
| 782 | + @Override |
| 783 | + public Single<String> answer(InvocationOnMock invocation) throws Throwable { |
| 784 | + return singles[singleFactoryCallsCounter.getAndIncrement()]; |
| 785 | + } |
| 786 | + }); |
| 787 | + |
| 788 | + Single<String> deferredSingle = Single.defer(singleFactory); |
| 789 | + |
| 790 | + for (int i = 0; i < singles.length; i ++) { |
| 791 | + TestSubscriber<String> testSubscriber = new TestSubscriber<String>(); |
| 792 | + |
| 793 | + deferredSingle.subscribe(testSubscriber); |
| 794 | + |
| 795 | + testSubscriber.assertValue(values[i]); |
| 796 | + testSubscriber.assertNoErrors(); |
| 797 | + } |
| 798 | + |
| 799 | + verify(singleFactory, times(3)).call(); |
| 800 | + } |
| 801 | + |
| 802 | + @Test |
| 803 | + public void deferShouldPassNullPointerExceptionToTheSubscriberIfSingleFactoryIsNull() { |
| 804 | + TestSubscriber<Object> testSubscriber = new TestSubscriber<Object>(); |
| 805 | + |
| 806 | + Single |
| 807 | + .defer(null) |
| 808 | + .subscribe(testSubscriber); |
| 809 | + |
| 810 | + testSubscriber.assertNoValues(); |
| 811 | + testSubscriber.assertError(NullPointerException.class); |
| 812 | + } |
| 813 | + |
| 814 | + |
| 815 | + @Test |
| 816 | + public void deferShouldPassNullPointerExceptionToTheSubscriberIfSingleFactoryReturnsNull() throws Exception { |
| 817 | + Callable<Single<Object>> singleFactory = mock(Callable.class); |
| 818 | + when(singleFactory.call()).thenReturn(null); |
| 819 | + |
| 820 | + TestSubscriber<Object> testSubscriber = new TestSubscriber<Object>(); |
| 821 | + |
| 822 | + Single |
| 823 | + .defer(singleFactory) |
| 824 | + .subscribe(testSubscriber); |
| 825 | + |
| 826 | + testSubscriber.assertNoValues(); |
| 827 | + testSubscriber.assertError(NullPointerException.class); |
| 828 | + |
| 829 | + verify(singleFactory).call(); |
| 830 | + } |
702 | 831 | }
|
0 commit comments