| 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.flowable; |
| 15 |
|
| 16 |
import static org.mockito.Mockito.*; |
| 17 |
|
| 18 |
import org.junit.*; |
| 19 |
import org.mockito.InOrder; |
| 20 |
import org.reactivestreams.Subscriber; |
| 21 |
|
| 22 |
import io.reactivex.rxjava3.core.*; |
| 23 |
import io.reactivex.rxjava3.functions.BiFunction; |
| 24 |
import io.reactivex.rxjava3.processors.PublishProcessor; |
| 25 |
import io.reactivex.rxjava3.testsupport.TestHelper; |
| 26 |
|
| 27 |
/** |
| 28 |
* Systematically tests that when zipping an infinite and a finite Observable, |
| 29 |
* the resulting Observable is finite. |
| 30 |
* |
| 31 |
*/ |
| 32 |
public class FlowableZipCompletionTest extends RxJavaTest { |
| 33 |
BiFunction<String, String, String> concat2Strings; |
| 34 |
|
| 35 |
PublishProcessor<String> s1; |
| 36 |
PublishProcessor<String> s2; |
| 37 |
Flowable<String> zipped; |
| 38 |
|
| 39 |
Subscriber<String> subscriber; |
| 40 |
InOrder inOrder; |
| 41 |
|
| 42 |
@Before |
| 43 |
public void setUp() { |
| 44 |
concat2Strings = new BiFunction<String, String, String>() { |
| 45 |
@Override |
| 46 |
public String apply(String t1, String t2) { |
| 47 |
return t1 + "-" + t2; |
| 48 |
} |
| 49 |
}; |
| 50 |
|
| 51 |
s1 = PublishProcessor.create(); |
| 52 |
s2 = PublishProcessor.create(); |
| 53 |
zipped = Flowable.zip(s1, s2, concat2Strings); |
| 54 |
|
| 55 |
subscriber = TestHelper.mockSubscriber(); |
| 56 |
inOrder = inOrder(subscriber); |
| 57 |
|
| 58 |
zipped.subscribe(subscriber); |
| 59 |
} |
| 60 |
|
| 61 |
@Test |
| 62 |
public void firstCompletesThenSecondInfinite() { |
| 63 |
s1.onNext("a"); |
| 64 |
s1.onNext("b"); |
| 65 |
s1.onComplete(); |
| 66 |
s2.onNext("1"); |
| 67 |
inOrder.verify(subscriber, times(1)).onNext("a-1"); |
| 68 |
s2.onNext("2"); |
| 69 |
inOrder.verify(subscriber, times(1)).onNext("b-2"); |
| 70 |
inOrder.verify(subscriber, times(1)).onComplete(); |
| 71 |
inOrder.verifyNoMoreInteractions(); |
| 72 |
} |
| 73 |
|
| 74 |
@Test |
| 75 |
public void secondInfiniteThenFirstCompletes() { |
| 76 |
s2.onNext("1"); |
| 77 |
s2.onNext("2"); |
| 78 |
s1.onNext("a"); |
| 79 |
inOrder.verify(subscriber, times(1)).onNext("a-1"); |
| 80 |
s1.onNext("b"); |
| 81 |
inOrder.verify(subscriber, times(1)).onNext("b-2"); |
| 82 |
s1.onComplete(); |
| 83 |
inOrder.verify(subscriber, times(1)).onComplete(); |
| 84 |
inOrder.verifyNoMoreInteractions(); |
| 85 |
} |
| 86 |
|
| 87 |
@Test |
| 88 |
public void secondCompletesThenFirstInfinite() { |
| 89 |
s2.onNext("1"); |
| 90 |
s2.onNext("2"); |
| 91 |
s2.onComplete(); |
| 92 |
s1.onNext("a"); |
| 93 |
inOrder.verify(subscriber, times(1)).onNext("a-1"); |
| 94 |
s1.onNext("b"); |
| 95 |
inOrder.verify(subscriber, times(1)).onNext("b-2"); |
| 96 |
inOrder.verify(subscriber, times(1)).onComplete(); |
| 97 |
inOrder.verifyNoMoreInteractions(); |
| 98 |
} |
| 99 |
|
| 100 |
@Test |
| 101 |
public void firstInfiniteThenSecondCompletes() { |
| 102 |
s1.onNext("a"); |
| 103 |
s1.onNext("b"); |
| 104 |
s2.onNext("1"); |
| 105 |
inOrder.verify(subscriber, times(1)).onNext("a-1"); |
| 106 |
s2.onNext("2"); |
| 107 |
inOrder.verify(subscriber, times(1)).onNext("b-2"); |
| 108 |
s2.onComplete(); |
| 109 |
inOrder.verify(subscriber, times(1)).onComplete(); |
| 110 |
inOrder.verifyNoMoreInteractions(); |
| 111 |
} |
| 112 |
|
| 113 |
} |