|
| 1 | +package rx.operators; |
| 2 | + |
| 3 | +import static org.mockito.Mockito.inOrder; |
| 4 | +import static org.mockito.Mockito.mock; |
| 5 | +import static org.mockito.Mockito.times; |
| 6 | + |
| 7 | +import org.junit.Before; |
| 8 | +import org.junit.Test; |
| 9 | +import org.mockito.InOrder; |
| 10 | + |
| 11 | +import rx.Observable; |
| 12 | +import rx.Observer; |
| 13 | +import rx.subjects.PublishSubject; |
| 14 | +import rx.util.functions.Func2; |
| 15 | + |
| 16 | +/** |
| 17 | + * Systematically tests that when zipping an infinite and a finite Observable, |
| 18 | + * the resulting Observable is finite. |
| 19 | + * |
| 20 | + */ |
| 21 | +public class OperationZipTestCompletion { |
| 22 | + Func2<String, String, String> concat2Strings; |
| 23 | + |
| 24 | + PublishSubject<String> s1; |
| 25 | + PublishSubject<String> s2; |
| 26 | + Observable<String> zipped; |
| 27 | + |
| 28 | + Observer<String> observer; |
| 29 | + InOrder inOrder; |
| 30 | + |
| 31 | + |
| 32 | + @Before @SuppressWarnings("unchecked") |
| 33 | + public void setUp() { |
| 34 | + concat2Strings = new Func2<String, String, String>() { |
| 35 | + @Override |
| 36 | + public String call(String t1, String t2) { |
| 37 | + return t1 + "-" + t2; |
| 38 | + } |
| 39 | + }; |
| 40 | + |
| 41 | + s1 = PublishSubject.create(); |
| 42 | + s2 = PublishSubject.create(); |
| 43 | + zipped = Observable.zip(s1, s2, concat2Strings); |
| 44 | + |
| 45 | + observer = mock(Observer.class); |
| 46 | + inOrder = inOrder(observer); |
| 47 | + |
| 48 | + zipped.subscribe(observer); |
| 49 | + } |
| 50 | + |
| 51 | + @Test |
| 52 | + public void testFirstCompletesThenSecondInfinite() { |
| 53 | + s1.onNext("a"); |
| 54 | + s1.onNext("b"); |
| 55 | + s1.onCompleted(); |
| 56 | + s2.onNext("1"); |
| 57 | + inOrder.verify(observer, times(1)).onNext("a-1"); |
| 58 | + s2.onNext("2"); |
| 59 | + inOrder.verify(observer, times(1)).onNext("b-2"); |
| 60 | + inOrder.verify(observer, times(1)).onCompleted(); |
| 61 | + inOrder.verifyNoMoreInteractions(); |
| 62 | + } |
| 63 | + |
| 64 | + @Test |
| 65 | + public void testSecondInfiniteThenFirstCompletes() { |
| 66 | + s2.onNext("1"); |
| 67 | + s2.onNext("2"); |
| 68 | + s1.onNext("a"); |
| 69 | + inOrder.verify(observer, times(1)).onNext("a-1"); |
| 70 | + s1.onNext("b"); |
| 71 | + inOrder.verify(observer, times(1)).onNext("b-2"); |
| 72 | + s1.onCompleted(); |
| 73 | + inOrder.verify(observer, times(1)).onCompleted(); |
| 74 | + inOrder.verifyNoMoreInteractions(); |
| 75 | + } |
| 76 | + |
| 77 | + @Test |
| 78 | + public void testSecondCompletesThenFirstInfinite() { |
| 79 | + s2.onNext("1"); |
| 80 | + s2.onNext("2"); |
| 81 | + s2.onCompleted(); |
| 82 | + s1.onNext("a"); |
| 83 | + inOrder.verify(observer, times(1)).onNext("a-1"); |
| 84 | + s1.onNext("b"); |
| 85 | + inOrder.verify(observer, times(1)).onNext("b-2"); |
| 86 | + inOrder.verify(observer, times(1)).onCompleted(); |
| 87 | + inOrder.verifyNoMoreInteractions(); |
| 88 | + } |
| 89 | + |
| 90 | + @Test |
| 91 | + public void testFirstInfiniteThenSecondCompletes() { |
| 92 | + s1.onNext("a"); |
| 93 | + s1.onNext("b"); |
| 94 | + s2.onNext("1"); |
| 95 | + inOrder.verify(observer, times(1)).onNext("a-1"); |
| 96 | + s2.onNext("2"); |
| 97 | + inOrder.verify(observer, times(1)).onNext("b-2"); |
| 98 | + s2.onCompleted(); |
| 99 | + inOrder.verify(observer, times(1)).onCompleted(); |
| 100 | + inOrder.verifyNoMoreInteractions(); |
| 101 | + } |
| 102 | + |
| 103 | +} |
0 commit comments