| 1 |
/* |
| 2 |
* Copyright (c) 2016-present, RxJava Contributors. |
| 3 |
* |
| 4 |
* Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in |
| 5 |
* compliance with the License. You may obtain a copy of the License at |
| 6 |
* |
| 7 |
* http://www.apache.org/licenses/LICENSE-2.0 |
| 8 |
* |
| 9 |
* Unless required by applicable law or agreed to in writing, software distributed under the License is |
| 10 |
* distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See |
| 11 |
* the License for the specific language governing permissions and limitations under the License. |
| 12 |
*/ |
| 13 |
|
| 14 |
package io.reactivex.rxjava3.internal.operators.observable; |
| 15 |
|
| 16 |
import static org.mockito.Mockito.*; |
| 17 |
|
| 18 |
import org.junit.*; |
| 19 |
import org.mockito.InOrder; |
| 20 |
|
| 21 |
import io.reactivex.rxjava3.core.*; |
| 22 |
import io.reactivex.rxjava3.functions.BiFunction; |
| 23 |
import io.reactivex.rxjava3.subjects.PublishSubject; |
| 24 |
import io.reactivex.rxjava3.testsupport.TestHelper; |
| 25 |
|
| 26 |
/** |
| 27 |
* Systematically tests that when zipping an infinite and a finite Observable, |
| 28 |
* the resulting Observable is finite. |
| 29 |
* |
| 30 |
*/ |
| 31 |
public class ObservableZipCompletionTest extends RxJavaTest { |
| 32 |
BiFunction<String, String, String> concat2Strings; |
| 33 |
|
| 34 |
PublishSubject<String> s1; |
| 35 |
PublishSubject<String> s2; |
| 36 |
Observable<String> zipped; |
| 37 |
|
| 38 |
Observer<String> observer; |
| 39 |
InOrder inOrder; |
| 40 |
|
| 41 |
@Before |
| 42 |
public void setUp() { |
| 43 |
concat2Strings = new BiFunction<String, String, String>() { |
| 44 |
@Override |
| 45 |
public String apply(String t1, String t2) { |
| 46 |
return t1 + "-" + t2; |
| 47 |
} |
| 48 |
}; |
| 49 |
|
| 50 |
s1 = PublishSubject.create(); |
| 51 |
s2 = PublishSubject.create(); |
| 52 |
zipped = Observable.zip(s1, s2, concat2Strings); |
| 53 |
|
| 54 |
observer = TestHelper.mockObserver(); |
| 55 |
inOrder = inOrder(observer); |
| 56 |
|
| 57 |
zipped.subscribe(observer); |
| 58 |
} |
| 59 |
|
| 60 |
@Test |
| 61 |
public void firstCompletesThenSecondInfinite() { |
| 62 |
s1.onNext("a"); |
| 63 |
s1.onNext("b"); |
| 64 |
s1.onComplete(); |
| 65 |
s2.onNext("1"); |
| 66 |
inOrder.verify(observer, times(1)).onNext("a-1"); |
| 67 |
s2.onNext("2"); |
| 68 |
inOrder.verify(observer, times(1)).onNext("b-2"); |
| 69 |
inOrder.verify(observer, times(1)).onComplete(); |
| 70 |
inOrder.verifyNoMoreInteractions(); |
| 71 |
} |
| 72 |
|
| 73 |
@Test |
| 74 |
public void secondInfiniteThenFirstCompletes() { |
| 75 |
s2.onNext("1"); |
| 76 |
s2.onNext("2"); |
| 77 |
s1.onNext("a"); |
| 78 |
inOrder.verify(observer, times(1)).onNext("a-1"); |
| 79 |
s1.onNext("b"); |
| 80 |
inOrder.verify(observer, times(1)).onNext("b-2"); |
| 81 |
s1.onComplete(); |
| 82 |
inOrder.verify(observer, times(1)).onComplete(); |
| 83 |
inOrder.verifyNoMoreInteractions(); |
| 84 |
} |
| 85 |
|
| 86 |
@Test |
| 87 |
public void secondCompletesThenFirstInfinite() { |
| 88 |
s2.onNext("1"); |
| 89 |
s2.onNext("2"); |
| 90 |
s2.onComplete(); |
| 91 |
s1.onNext("a"); |
| 92 |
inOrder.verify(observer, times(1)).onNext("a-1"); |
| 93 |
s1.onNext("b"); |
| 94 |
inOrder.verify(observer, times(1)).onNext("b-2"); |
| 95 |
inOrder.verify(observer, times(1)).onComplete(); |
| 96 |
inOrder.verifyNoMoreInteractions(); |
| 97 |
} |
| 98 |
|
| 99 |
@Test |
| 100 |
public void firstInfiniteThenSecondCompletes() { |
| 101 |
s1.onNext("a"); |
| 102 |
s1.onNext("b"); |
| 103 |
s2.onNext("1"); |
| 104 |
inOrder.verify(observer, times(1)).onNext("a-1"); |
| 105 |
s2.onNext("2"); |
| 106 |
inOrder.verify(observer, times(1)).onNext("b-2"); |
| 107 |
s2.onComplete(); |
| 108 |
inOrder.verify(observer, times(1)).onComplete(); |
| 109 |
inOrder.verifyNoMoreInteractions(); |
| 110 |
} |
| 111 |
|
| 112 |
} |