| 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.junit.Assert.*; |
| 17 |
|
| 18 |
import java.util.*; |
| 19 |
import java.util.concurrent.TimeUnit; |
| 20 |
import java.util.concurrent.atomic.AtomicInteger; |
| 21 |
|
| 22 |
import io.reactivex.rxjava3.disposables.Disposable; |
| 23 |
import org.junit.*; |
| 24 |
|
| 25 |
import io.reactivex.rxjava3.core.*; |
| 26 |
import io.reactivex.rxjava3.core.Observable; |
| 27 |
import io.reactivex.rxjava3.core.Observer; |
| 28 |
import io.reactivex.rxjava3.internal.schedulers.IoScheduler; |
| 29 |
import io.reactivex.rxjava3.observers.TestObserver; |
| 30 |
import io.reactivex.rxjava3.schedulers.Schedulers; |
| 31 |
import io.reactivex.rxjava3.testsupport.*; |
| 32 |
|
| 33 |
public class ObservableMergeMaxConcurrentTest extends RxJavaTest { |
| 34 |
|
| 35 |
Observer<String> stringObserver; |
| 36 |
|
| 37 |
@Before |
| 38 |
public void before() { |
| 39 |
stringObserver = TestHelper.mockObserver(); |
| 40 |
} |
| 41 |
|
| 42 |
@Test |
| 43 |
public void whenMaxConcurrentIsOne() { |
| 44 |
for (int i = 0; i < 100; i++) { |
| 45 |
List<Observable<String>> os = new ArrayList<>(); |
| 46 |
os.add(Observable.just("one", "two", "three", "four", "five").subscribeOn(Schedulers.newThread())); |
| 47 |
os.add(Observable.just("one", "two", "three", "four", "five").subscribeOn(Schedulers.newThread())); |
| 48 |
os.add(Observable.just("one", "two", "three", "four", "five").subscribeOn(Schedulers.newThread())); |
| 49 |
|
| 50 |
List<String> expected = Arrays.asList("one", "two", "three", "four", "five", "one", "two", "three", "four", "five", "one", "two", "three", "four", "five"); |
| 51 |
Iterator<String> iter = Observable.merge(os, 1).blockingIterable().iterator(); |
| 52 |
List<String> actual = new ArrayList<>(); |
| 53 |
while (iter.hasNext()) { |
| 54 |
actual.add(iter.next()); |
| 55 |
} |
| 56 |
assertEquals(expected, actual); |
| 57 |
} |
| 58 |
} |
| 59 |
|
| 60 |
@Test |
| 61 |
public void maxConcurrent() { |
| 62 |
for (int times = 0; times < 100; times++) { |
| 63 |
int observableCount = 100; |
| 64 |
// Test maxConcurrent from 2 to 12 |
| 65 |
int maxConcurrent = 2 + (times % 10); |
| 66 |
AtomicInteger subscriptionCount = new AtomicInteger(0); |
| 67 |
|
| 68 |
List<Observable<String>> os = new ArrayList<>(); |
| 69 |
List<SubscriptionCheckObservable> scos = new ArrayList<>(); |
| 70 |
for (int i = 0; i < observableCount; i++) { |
| 71 |
SubscriptionCheckObservable sco = new SubscriptionCheckObservable(subscriptionCount, maxConcurrent); |
| 72 |
scos.add(sco); |
| 73 |
os.add(Observable.unsafeCreate(sco)); |
| 74 |
} |
| 75 |
|
| 76 |
Iterator<String> iter = Observable.merge(os, maxConcurrent).blockingIterable().iterator(); |
| 77 |
List<String> actual = new ArrayList<>(); |
| 78 |
while (iter.hasNext()) { |
| 79 |
actual.add(iter.next()); |
| 80 |
} |
| 81 |
// System.out.println("actual: " + actual); |
| 82 |
assertEquals(5 * observableCount, actual.size()); |
| 83 |
for (SubscriptionCheckObservable sco : scos) { |
| 84 |
assertFalse(sco.failed); |
| 85 |
} |
| 86 |
} |
| 87 |
} |
| 88 |
|
| 89 |
private static class SubscriptionCheckObservable implements ObservableSource<String> { |
| 90 |
|
| 91 |
private final AtomicInteger subscriptionCount; |
| 92 |
private final int maxConcurrent; |
| 93 |
volatile boolean failed; |
| 94 |
|
| 95 |
SubscriptionCheckObservable(AtomicInteger subscriptionCount, int maxConcurrent) { |
| 96 |
this.subscriptionCount = subscriptionCount; |
| 97 |
this.maxConcurrent = maxConcurrent; |
| 98 |
} |
| 99 |
|
| 100 |
@Override |
| 101 |
public void subscribe(final Observer<? super String> t1) { |
| 102 |
t1.onSubscribe(Disposable.empty()); |
| 103 |
new Thread(new Runnable() { |
| 104 |
|
| 105 |
@Override |
| 106 |
public void run() { |
| 107 |
if (subscriptionCount.incrementAndGet() > maxConcurrent) { |
| 108 |
failed = true; |
| 109 |
} |
| 110 |
t1.onNext("one"); |
| 111 |
t1.onNext("two"); |
| 112 |
t1.onNext("three"); |
| 113 |
t1.onNext("four"); |
| 114 |
t1.onNext("five"); |
| 115 |
// We could not decrement subscriptionCount in the unsubscribe method |
| 116 |
// as "unsubscribe" is not guaranteed to be called before the next "subscribe". |
| 117 |
subscriptionCount.decrementAndGet(); |
| 118 |
t1.onComplete(); |
| 119 |
} |
| 120 |
|
| 121 |
}).start(); |
| 122 |
} |
| 123 |
|
| 124 |
} |
| 125 |
|
| 126 |
@Test |
| 127 |
public void mergeALotOfSourcesOneByOneSynchronously() { |
| 128 |
int n = 10000; |
| 129 |
List<Observable<Integer>> sourceList = new ArrayList<>(n); |
| 130 |
for (int i = 0; i < n; i++) { |
| 131 |
sourceList.add(Observable.just(i)); |
| 132 |
} |
| 133 |
Iterator<Integer> it = Observable.merge(Observable.fromIterable(sourceList), 1).blockingIterable().iterator(); |
| 134 |
int j = 0; |
| 135 |
while (it.hasNext()) { |
| 136 |
assertEquals((Integer)j, it.next()); |
| 137 |
j++; |
| 138 |
} |
| 139 |
assertEquals(j, n); |
| 140 |
} |
| 141 |
|
| 142 |
@Test |
| 143 |
public void mergeALotOfSourcesOneByOneSynchronouslyTakeHalf() { |
| 144 |
int n = 10000; |
| 145 |
List<Observable<Integer>> sourceList = new ArrayList<>(n); |
| 146 |
for (int i = 0; i < n; i++) { |
| 147 |
sourceList.add(Observable.just(i)); |
| 148 |
} |
| 149 |
Iterator<Integer> it = Observable.merge(Observable.fromIterable(sourceList), 1).take(n / 2).blockingIterable().iterator(); |
| 150 |
int j = 0; |
| 151 |
while (it.hasNext()) { |
| 152 |
assertEquals((Integer)j, it.next()); |
| 153 |
j++; |
| 154 |
} |
| 155 |
assertEquals(j, n / 2); |
| 156 |
} |
| 157 |
|
| 158 |
@Test |
| 159 |
public void simple() { |
| 160 |
for (int i = 1; i < 100; i++) { |
| 161 |
TestObserverEx<Integer> to = new TestObserverEx<>(); |
| 162 |
List<Observable<Integer>> sourceList = new ArrayList<>(i); |
| 163 |
List<Integer> result = new ArrayList<>(i); |
| 164 |
for (int j = 1; j <= i; j++) { |
| 165 |
sourceList.add(Observable.just(j)); |
| 166 |
result.add(j); |
| 167 |
} |
| 168 |
|
| 169 |
Observable.merge(sourceList, i).subscribe(to); |
| 170 |
|
| 171 |
to.assertNoErrors(); |
| 172 |
to.assertTerminated(); |
| 173 |
to.assertValueSequence(result); |
| 174 |
} |
| 175 |
} |
| 176 |
|
| 177 |
@Test |
| 178 |
public void simpleOneLess() { |
| 179 |
for (int i = 2; i < 100; i++) { |
| 180 |
TestObserverEx<Integer> to = new TestObserverEx<>(); |
| 181 |
List<Observable<Integer>> sourceList = new ArrayList<>(i); |
| 182 |
List<Integer> result = new ArrayList<>(i); |
| 183 |
for (int j = 1; j <= i; j++) { |
| 184 |
sourceList.add(Observable.just(j)); |
| 185 |
result.add(j); |
| 186 |
} |
| 187 |
|
| 188 |
Observable.merge(sourceList, i - 1).subscribe(to); |
| 189 |
|
| 190 |
to.assertNoErrors(); |
| 191 |
to.assertTerminated(); |
| 192 |
to.assertValueSequence(result); |
| 193 |
} |
| 194 |
} |
| 195 |
|
| 196 |
@Test |
| 197 |
public void simpleAsyncLoop() { |
| 198 |
IoScheduler ios = (IoScheduler)Schedulers.io(); |
| 199 |
int c = ios.size(); |
| 200 |
for (int i = 0; i < 200; i++) { |
| 201 |
simpleAsync(); |
| 202 |
int c1 = ios.size(); |
| 203 |
if (c + 60 < c1) { |
| 204 |
throw new AssertionError("Worker leak: " + c + " - " + c1); |
| 205 |
} |
| 206 |
} |
| 207 |
} |
| 208 |
|
| 209 |
@Test |
| 210 |
public void simpleAsync() { |
| 211 |
for (int i = 1; i < 50; i++) { |
| 212 |
TestObserver<Integer> to = new TestObserver<>(); |
| 213 |
List<Observable<Integer>> sourceList = new ArrayList<>(i); |
| 214 |
Set<Integer> expected = new HashSet<>(i); |
| 215 |
for (int j = 1; j <= i; j++) { |
| 216 |
sourceList.add(Observable.just(j).subscribeOn(Schedulers.io())); |
| 217 |
expected.add(j); |
| 218 |
} |
| 219 |
|
| 220 |
Observable.merge(sourceList, i).subscribe(to); |
| 221 |
|
| 222 |
to.awaitDone(1, TimeUnit.SECONDS); |
| 223 |
to.assertNoErrors(); |
| 224 |
Set<Integer> actual = new HashSet<>(to.values()); |
| 225 |
|
| 226 |
assertEquals(expected, actual); |
| 227 |
} |
| 228 |
} |
| 229 |
|
| 230 |
@Test |
| 231 |
public void simpleOneLessAsyncLoop() { |
| 232 |
for (int i = 0; i < 200; i++) { |
| 233 |
simpleOneLessAsync(); |
| 234 |
} |
| 235 |
} |
| 236 |
|
| 237 |
@Test |
| 238 |
public void simpleOneLessAsync() { |
| 239 |
long t = System.currentTimeMillis(); |
| 240 |
for (int i = 2; i < 50; i++) { |
| 241 |
if (System.currentTimeMillis() - t > TimeUnit.SECONDS.toMillis(9)) { |
| 242 |
break; |
| 243 |
} |
| 244 |
TestObserver<Integer> to = new TestObserver<>(); |
| 245 |
List<Observable<Integer>> sourceList = new ArrayList<>(i); |
| 246 |
Set<Integer> expected = new HashSet<>(i); |
| 247 |
for (int j = 1; j <= i; j++) { |
| 248 |
sourceList.add(Observable.just(j).subscribeOn(Schedulers.io())); |
| 249 |
expected.add(j); |
| 250 |
} |
| 251 |
|
| 252 |
Observable.merge(sourceList, i - 1).subscribe(to); |
| 253 |
|
| 254 |
to.awaitDone(1, TimeUnit.SECONDS); |
| 255 |
to.assertNoErrors(); |
| 256 |
Set<Integer> actual = new HashSet<>(to.values()); |
| 257 |
|
| 258 |
assertEquals(expected, actual); |
| 259 |
} |
| 260 |
} |
| 261 |
|
| 262 |
@Test |
| 263 |
public void take() throws Exception { |
| 264 |
List<Observable<Integer>> sourceList = new ArrayList<>(3); |
| 265 |
|
| 266 |
sourceList.add(Observable.range(0, 100000).subscribeOn(Schedulers.io())); |
| 267 |
sourceList.add(Observable.range(0, 100000).subscribeOn(Schedulers.io())); |
| 268 |
sourceList.add(Observable.range(0, 100000).subscribeOn(Schedulers.io())); |
| 269 |
|
| 270 |
TestObserver<Integer> to = new TestObserver<>(); |
| 271 |
|
| 272 |
Observable.merge(sourceList, 2).take(5).subscribe(to); |
| 273 |
|
| 274 |
to.awaitDone(5, TimeUnit.SECONDS); |
| 275 |
to.assertNoErrors(); |
| 276 |
to.assertValueCount(5); |
| 277 |
} |
| 278 |
} |