| 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.single; |
| 15 |
|
| 16 |
import static org.junit.Assert.*; |
| 17 |
|
| 18 |
import java.util.Arrays; |
| 19 |
|
| 20 |
import org.junit.Test; |
| 21 |
|
| 22 |
import io.reactivex.rxjava3.core.*; |
| 23 |
import io.reactivex.rxjava3.processors.PublishProcessor; |
| 24 |
import io.reactivex.rxjava3.subscribers.TestSubscriber; |
| 25 |
import io.reactivex.rxjava3.testsupport.TestHelper; |
| 26 |
|
| 27 |
public class SingleConcatTest extends RxJavaTest { |
| 28 |
@Test |
| 29 |
public void concatWith() { |
| 30 |
Single.just(1).concatWith(Single.just(2)) |
| 31 |
.test() |
| 32 |
.assertResult(1, 2); |
| 33 |
} |
| 34 |
|
| 35 |
@Test |
| 36 |
public void concat2() { |
| 37 |
Single.concat(Single.just(1), Single.just(2)) |
| 38 |
.test() |
| 39 |
.assertResult(1, 2); |
| 40 |
} |
| 41 |
|
| 42 |
@Test |
| 43 |
public void concat3() { |
| 44 |
Single.concat(Single.just(1), Single.just(2), Single.just(3)) |
| 45 |
.test() |
| 46 |
.assertResult(1, 2, 3); |
| 47 |
} |
| 48 |
|
| 49 |
@Test |
| 50 |
public void concat4() { |
| 51 |
Single.concat(Single.just(1), Single.just(2), Single.just(3), Single.just(4)) |
| 52 |
.test() |
| 53 |
.assertResult(1, 2, 3, 4); |
| 54 |
} |
| 55 |
|
| 56 |
@SuppressWarnings("unchecked") |
| 57 |
@Test |
| 58 |
public void concatArray() { |
| 59 |
for (int i = 1; i < 100; i++) { |
| 60 |
Single<Integer>[] array = new Single[i]; |
| 61 |
|
| 62 |
Arrays.fill(array, Single.just(1)); |
| 63 |
|
| 64 |
Single.concatArray(array) |
| 65 |
.to(TestHelper.<Integer>testConsumer()) |
| 66 |
.assertSubscribed() |
| 67 |
.assertValueCount(i) |
| 68 |
.assertNoErrors() |
| 69 |
.assertComplete(); |
| 70 |
} |
| 71 |
} |
| 72 |
|
| 73 |
@Test |
| 74 |
public void concatArrayEagerTest() { |
| 75 |
PublishProcessor<String> pp1 = PublishProcessor.create(); |
| 76 |
PublishProcessor<String> pp2 = PublishProcessor.create(); |
| 77 |
|
| 78 |
TestSubscriber<String> ts = Single.concatArrayEager(pp1.single("1"), pp2.single("2")).test(); |
| 79 |
|
| 80 |
assertTrue(pp1.hasSubscribers()); |
| 81 |
assertTrue(pp2.hasSubscribers()); |
| 82 |
|
| 83 |
pp2.onComplete(); |
| 84 |
ts.assertEmpty(); |
| 85 |
pp1.onComplete(); |
| 86 |
|
| 87 |
ts.assertResult("1", "2"); |
| 88 |
ts.assertComplete(); |
| 89 |
} |
| 90 |
|
| 91 |
@Test |
| 92 |
public void concatEagerIterableTest() { |
| 93 |
PublishProcessor<String> pp1 = PublishProcessor.create(); |
| 94 |
PublishProcessor<String> pp2 = PublishProcessor.create(); |
| 95 |
|
| 96 |
TestSubscriber<String> ts = Single.concatEager(Arrays.asList(pp1.single("2"), pp2.single("1"))).test(); |
| 97 |
|
| 98 |
assertTrue(pp1.hasSubscribers()); |
| 99 |
assertTrue(pp2.hasSubscribers()); |
| 100 |
|
| 101 |
pp2.onComplete(); |
| 102 |
ts.assertEmpty(); |
| 103 |
pp1.onComplete(); |
| 104 |
|
| 105 |
ts.assertResult("2", "1"); |
| 106 |
ts.assertComplete(); |
| 107 |
} |
| 108 |
|
| 109 |
@Test |
| 110 |
public void concatEagerPublisherTest() { |
| 111 |
PublishProcessor<String> pp1 = PublishProcessor.create(); |
| 112 |
PublishProcessor<String> pp2 = PublishProcessor.create(); |
| 113 |
|
| 114 |
TestSubscriber<String> ts = Single.concatEager(Flowable.just(pp1.single("1"), pp2.single("2"))).test(); |
| 115 |
|
| 116 |
assertTrue(pp1.hasSubscribers()); |
| 117 |
assertTrue(pp2.hasSubscribers()); |
| 118 |
|
| 119 |
pp2.onComplete(); |
| 120 |
ts.assertEmpty(); |
| 121 |
pp1.onComplete(); |
| 122 |
|
| 123 |
ts.assertResult("1", "2"); |
| 124 |
ts.assertComplete(); |
| 125 |
} |
| 126 |
|
| 127 |
@SuppressWarnings("unchecked") |
| 128 |
@Test |
| 129 |
public void concatObservable() { |
| 130 |
for (int i = 1; i < 100; i++) { |
| 131 |
Single<Integer>[] array = new Single[i]; |
| 132 |
|
| 133 |
Arrays.fill(array, Single.just(1)); |
| 134 |
|
| 135 |
Single.concat(Observable.fromArray(array)) |
| 136 |
.to(TestHelper.<Integer>testConsumer()) |
| 137 |
.assertSubscribed() |
| 138 |
.assertValueCount(i) |
| 139 |
.assertNoErrors() |
| 140 |
.assertComplete(); |
| 141 |
} |
| 142 |
} |
| 143 |
|
| 144 |
@Test |
| 145 |
public void noSubsequentSubscription() { |
| 146 |
final int[] calls = { 0 }; |
| 147 |
|
| 148 |
Single<Integer> source = Single.create(new SingleOnSubscribe<Integer>() { |
| 149 |
@Override |
| 150 |
public void subscribe(SingleEmitter<Integer> s) throws Exception { |
| 151 |
calls[0]++; |
| 152 |
s.onSuccess(1); |
| 153 |
} |
| 154 |
}); |
| 155 |
|
| 156 |
Single.concatArray(source, source).firstElement() |
| 157 |
.test() |
| 158 |
.assertResult(1); |
| 159 |
|
| 160 |
assertEquals(1, calls[0]); |
| 161 |
} |
| 162 |
|
| 163 |
@Test |
| 164 |
public void noSubsequentSubscriptionIterable() { |
| 165 |
final int[] calls = { 0 }; |
| 166 |
|
| 167 |
Single<Integer> source = Single.create(new SingleOnSubscribe<Integer>() { |
| 168 |
@Override |
| 169 |
public void subscribe(SingleEmitter<Integer> s) throws Exception { |
| 170 |
calls[0]++; |
| 171 |
s.onSuccess(1); |
| 172 |
} |
| 173 |
}); |
| 174 |
|
| 175 |
Single.concat(Arrays.asList(source, source)).firstElement() |
| 176 |
.test() |
| 177 |
.assertResult(1); |
| 178 |
|
| 179 |
assertEquals(1, calls[0]); |
| 180 |
} |
| 181 |
} |