| 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 java.util.Arrays.asList; |
| 17 |
import static org.junit.Assert.*; |
| 18 |
import static org.mockito.ArgumentMatchers.any; |
| 19 |
import static org.mockito.Mockito.*; |
| 20 |
|
| 21 |
import java.util.*; |
| 22 |
import java.util.concurrent.*; |
| 23 |
import java.util.concurrent.atomic.*; |
| 24 |
|
| 25 |
import org.junit.*; |
| 26 |
import org.reactivestreams.*; |
| 27 |
|
| 28 |
import io.reactivex.rxjava3.core.*; |
| 29 |
import io.reactivex.rxjava3.core.Scheduler.Worker; |
| 30 |
import io.reactivex.rxjava3.exceptions.TestException; |
| 31 |
import io.reactivex.rxjava3.functions.*; |
| 32 |
import io.reactivex.rxjava3.internal.functions.Functions; |
| 33 |
import io.reactivex.rxjava3.internal.subscriptions.*; |
| 34 |
import io.reactivex.rxjava3.internal.util.*; |
| 35 |
import io.reactivex.rxjava3.plugins.RxJavaPlugins; |
| 36 |
import io.reactivex.rxjava3.processors.PublishProcessor; |
| 37 |
import io.reactivex.rxjava3.schedulers.*; |
| 38 |
import io.reactivex.rxjava3.subscribers.*; |
| 39 |
import io.reactivex.rxjava3.testsupport.*; |
| 40 |
|
| 41 |
public class FlowableMergeTest extends RxJavaTest { |
| 42 |
|
| 43 |
Subscriber<String> stringSubscriber; |
| 44 |
|
| 45 |
int count; |
| 46 |
|
| 47 |
@Before |
| 48 |
public void before() { |
| 49 |
stringSubscriber = TestHelper.mockSubscriber(); |
| 50 |
|
| 51 |
for (Thread t : Thread.getAllStackTraces().keySet()) { |
| 52 |
if (t.getName().startsWith("RxNewThread")) { |
| 53 |
count++; |
| 54 |
} |
| 55 |
} |
| 56 |
} |
| 57 |
|
| 58 |
@After |
| 59 |
public void after() { |
| 60 |
try { |
| 61 |
Thread.sleep(100); |
| 62 |
} catch (InterruptedException e) { |
| 63 |
// TODO Auto-generated catch block |
| 64 |
e.printStackTrace(); |
| 65 |
} |
| 66 |
for (Thread t : Thread.getAllStackTraces().keySet()) { |
| 67 |
if (t.getName().startsWith("RxNewThread")) { |
| 68 |
--count; |
| 69 |
} |
| 70 |
} |
| 71 |
if (count != 0) { |
| 72 |
throw new IllegalStateException("NewThread leak!"); |
| 73 |
} |
| 74 |
} |
| 75 |
|
| 76 |
@Test |
| 77 |
public void mergeFlowableOfFlowables() { |
| 78 |
final Flowable<String> f1 = Flowable.unsafeCreate(new TestSynchronousFlowable()); |
| 79 |
final Flowable<String> f2 = Flowable.unsafeCreate(new TestSynchronousFlowable()); |
| 80 |
|
| 81 |
Flowable<Flowable<String>> flowableOfFlowables = Flowable.unsafeCreate(new Publisher<Flowable<String>>() { |
| 82 |
|
| 83 |
@Override |
| 84 |
public void subscribe(Subscriber<? super Flowable<String>> subscriber) { |
| 85 |
subscriber.onSubscribe(new BooleanSubscription()); |
| 86 |
// simulate what would happen in a Flowable |
| 87 |
subscriber.onNext(f1); |
| 88 |
subscriber.onNext(f2); |
| 89 |
subscriber.onComplete(); |
| 90 |
} |
| 91 |
|
| 92 |
}); |
| 93 |
Flowable<String> m = Flowable.merge(flowableOfFlowables); |
| 94 |
m.subscribe(stringSubscriber); |
| 95 |
|
| 96 |
verify(stringSubscriber, never()).onError(any(Throwable.class)); |
| 97 |
verify(stringSubscriber, times(1)).onComplete(); |
| 98 |
verify(stringSubscriber, times(2)).onNext("hello"); |
| 99 |
} |
| 100 |
|
| 101 |
@Test |
| 102 |
public void mergeArray() { |
| 103 |
final Flowable<String> f1 = Flowable.unsafeCreate(new TestSynchronousFlowable()); |
| 104 |
final Flowable<String> f2 = Flowable.unsafeCreate(new TestSynchronousFlowable()); |
| 105 |
|
| 106 |
Flowable<String> m = Flowable.merge(f1, f2); |
| 107 |
m.subscribe(stringSubscriber); |
| 108 |
|
| 109 |
verify(stringSubscriber, never()).onError(any(Throwable.class)); |
| 110 |
verify(stringSubscriber, times(2)).onNext("hello"); |
| 111 |
verify(stringSubscriber, times(1)).onComplete(); |
| 112 |
} |
| 113 |
|
| 114 |
@Test |
| 115 |
public void mergeList() { |
| 116 |
final Flowable<String> f1 = Flowable.unsafeCreate(new TestSynchronousFlowable()); |
| 117 |
final Flowable<String> f2 = Flowable.unsafeCreate(new TestSynchronousFlowable()); |
| 118 |
List<Flowable<String>> listOfFlowables = new ArrayList<>(); |
| 119 |
listOfFlowables.add(f1); |
| 120 |
listOfFlowables.add(f2); |
| 121 |
|
| 122 |
Flowable<String> m = Flowable.merge(listOfFlowables); |
| 123 |
m.subscribe(stringSubscriber); |
| 124 |
|
| 125 |
verify(stringSubscriber, never()).onError(any(Throwable.class)); |
| 126 |
verify(stringSubscriber, times(1)).onComplete(); |
| 127 |
verify(stringSubscriber, times(2)).onNext("hello"); |
| 128 |
} |
| 129 |
|
| 130 |
@Test |
| 131 |
public void unSubscribeFlowableOfFlowables() throws InterruptedException { |
| 132 |
|
| 133 |
final AtomicBoolean unsubscribed = new AtomicBoolean(); |
| 134 |
final CountDownLatch latch = new CountDownLatch(1); |
| 135 |
|
| 136 |
Flowable<Flowable<Long>> source = Flowable.unsafeCreate(new Publisher<Flowable<Long>>() { |
| 137 |
|
| 138 |
@Override |
| 139 |
public void subscribe(final Subscriber<? super Flowable<Long>> subscriber) { |
| 140 |
// verbose on purpose so I can track the inside of it |
| 141 |
final Subscription s = new Subscription() { |
| 142 |
|
| 143 |
@Override |
| 144 |
public void request(long n) { |
| 145 |
|
| 146 |
} |
| 147 |
|
| 148 |
@Override |
| 149 |
public void cancel() { |
| 150 |
System.out.println("*** unsubscribed"); |
| 151 |
unsubscribed.set(true); |
| 152 |
} |
| 153 |
|
| 154 |
}; |
| 155 |
subscriber.onSubscribe(s); |
| 156 |
|
| 157 |
new Thread(new Runnable() { |
| 158 |
|
| 159 |
@Override |
| 160 |
public void run() { |
| 161 |
|
| 162 |
while (!unsubscribed.get()) { |
| 163 |
subscriber.onNext(Flowable.just(1L, 2L)); |
| 164 |
} |
| 165 |
System.out.println("Done looping after unsubscribe: " + unsubscribed.get()); |
| 166 |
subscriber.onComplete(); |
| 167 |
|
| 168 |
// mark that the thread is finished |
| 169 |
latch.countDown(); |
| 170 |
} |
| 171 |
}).start(); |
| 172 |
} |
| 173 |
|
| 174 |
}); |
| 175 |
|
| 176 |
final AtomicInteger count = new AtomicInteger(); |
| 177 |
Flowable.merge(source).take(6).blockingForEach(new Consumer<Long>() { |
| 178 |
|
| 179 |
@Override |
| 180 |
public void accept(Long v) { |
| 181 |
System.out.println("Value: " + v); |
| 182 |
int c = count.incrementAndGet(); |
| 183 |
if (c > 6) { |
| 184 |
fail("Should be only 6"); |
| 185 |
} |
| 186 |
|
| 187 |
} |
| 188 |
}); |
| 189 |
|
| 190 |
latch.await(1000, TimeUnit.MILLISECONDS); |
| 191 |
|
| 192 |
System.out.println("unsubscribed: " + unsubscribed.get()); |
| 193 |
|
| 194 |
assertTrue(unsubscribed.get()); |
| 195 |
|
| 196 |
} |
| 197 |
|
| 198 |
@Test |
| 199 |
public void mergeArrayWithThreading() { |
| 200 |
final TestASynchronousFlowable f1 = new TestASynchronousFlowable(); |
| 201 |
final TestASynchronousFlowable f2 = new TestASynchronousFlowable(); |
| 202 |
|
| 203 |
Flowable<String> m = Flowable.merge(Flowable.unsafeCreate(f1), Flowable.unsafeCreate(f2)); |
| 204 |
TestSubscriber<String> ts = new TestSubscriber<>(stringSubscriber); |
| 205 |
m.subscribe(ts); |
| 206 |
|
| 207 |
ts.awaitDone(10, TimeUnit.SECONDS); |
| 208 |
ts.assertNoErrors(); |
| 209 |
|
| 210 |
verify(stringSubscriber, never()).onError(any(Throwable.class)); |
| 211 |
verify(stringSubscriber, times(2)).onNext("hello"); |
| 212 |
verify(stringSubscriber, times(1)).onComplete(); |
| 213 |
} |
| 214 |
|
| 215 |
@Test |
| 216 |
public void synchronizationOfMultipleSequencesLoop() throws Throwable { |
| 217 |
for (int i = 0; i < 100; i++) { |
| 218 |
System.out.println("testSynchronizationOfMultipleSequencesLoop > " + i); |
| 219 |
synchronizationOfMultipleSequences(); |
| 220 |
} |
| 221 |
} |
| 222 |
|
| 223 |
@Test |
| 224 |
public void synchronizationOfMultipleSequences() throws Throwable { |
| 225 |
final TestASynchronousFlowable f1 = new TestASynchronousFlowable(); |
| 226 |
final TestASynchronousFlowable f2 = new TestASynchronousFlowable(); |
| 227 |
|
| 228 |
// use this latch to cause onNext to wait until we're ready to let it go |
| 229 |
final CountDownLatch endLatch = new CountDownLatch(1); |
| 230 |
|
| 231 |
final AtomicInteger concurrentCounter = new AtomicInteger(); |
| 232 |
final AtomicInteger totalCounter = new AtomicInteger(); |
| 233 |
|
| 234 |
final AtomicReference<Throwable> error = new AtomicReference<>(); |
| 235 |
|
| 236 |
Flowable<String> m = Flowable.merge(Flowable.unsafeCreate(f1), Flowable.unsafeCreate(f2)); |
| 237 |
m.subscribe(new DefaultSubscriber<String>() { |
| 238 |
|
| 239 |
@Override |
| 240 |
public void onComplete() { |
| 241 |
|
| 242 |
} |
| 243 |
|
| 244 |
@Override |
| 245 |
public void onError(Throwable e) { |
| 246 |
error.set(e); |
| 247 |
} |
| 248 |
|
| 249 |
@Override |
| 250 |
public void onNext(String v) { |
| 251 |
totalCounter.incrementAndGet(); |
| 252 |
concurrentCounter.incrementAndGet(); |
| 253 |
try { |
| 254 |
// avoid deadlocking the main thread |
| 255 |
if (Thread.currentThread().getName().equals("TestASynchronousFlowable")) { |
| 256 |
// wait here until we're done asserting |
| 257 |
endLatch.await(); |
| 258 |
} |
| 259 |
} catch (InterruptedException e) { |
| 260 |
e.printStackTrace(); |
| 261 |
throw new RuntimeException("failed", e); |
| 262 |
} finally { |
| 263 |
concurrentCounter.decrementAndGet(); |
| 264 |
} |
| 265 |
} |
| 266 |
|
| 267 |
}); |
| 268 |
|
| 269 |
// wait for both Flowables to send (one should be blocked) |
| 270 |
f1.onNextBeingSent.await(); |
| 271 |
f2.onNextBeingSent.await(); |
| 272 |
|
| 273 |
// I can't think of a way to know for sure that both threads have or are trying to send onNext |
| 274 |
// since I can't use a CountDownLatch for "after" onNext since I want to catch during it |
| 275 |
// but I can't know for sure onNext is invoked |
| 276 |
// so I'm unfortunately reverting to using a Thread.sleep to allow the process scheduler time |
| 277 |
// to make sure after o1.onNextBeingSent and o2.onNextBeingSent are hit that the following |
| 278 |
// onNext is invoked. |
| 279 |
|
| 280 |
int timeout = 20; |
| 281 |
|
| 282 |
while (timeout-- > 0 && concurrentCounter.get() != 1) { |
| 283 |
Thread.sleep(100); |
| 284 |
} |
| 285 |
|
| 286 |
try { // in try/finally so threads are released via latch countDown even if assertion fails |
| 287 |
if (error.get() != null) { |
| 288 |
throw ExceptionHelper.wrapOrThrow(error.get()); |
| 289 |
} |
| 290 |
|
| 291 |
assertEquals(1, concurrentCounter.get()); |
| 292 |
} finally { |
| 293 |
// release so it can finish |
| 294 |
endLatch.countDown(); |
| 295 |
} |
| 296 |
|
| 297 |
try { |
| 298 |
f1.t.join(); |
| 299 |
f2.t.join(); |
| 300 |
} catch (InterruptedException e) { |
| 301 |
throw new RuntimeException(e); |
| 302 |
} |
| 303 |
|
| 304 |
assertEquals(2, totalCounter.get()); |
| 305 |
assertEquals(0, concurrentCounter.get()); |
| 306 |
} |
| 307 |
|
| 308 |
/** |
| 309 |
* Unit test from OperationMergeDelayError backported here to show how these use cases work with normal merge. |
| 310 |
*/ |
| 311 |
@Test |
| 312 |
public void error1() { |
| 313 |
// we are using synchronous execution to test this exactly rather than non-deterministic concurrent behavior |
| 314 |
final Flowable<String> f1 = Flowable.unsafeCreate(new TestErrorFlowable("four", null, "six")); // we expect to lose "six" |
| 315 |
final Flowable<String> f2 = Flowable.unsafeCreate(new TestErrorFlowable("one", "two", "three")); // we expect to lose all of these since o1 is done first and fails |
| 316 |
|
| 317 |
Flowable<String> m = Flowable.merge(f1, f2); |
| 318 |
m.subscribe(stringSubscriber); |
| 319 |
|
| 320 |
verify(stringSubscriber, times(1)).onError(any(NullPointerException.class)); |
| 321 |
verify(stringSubscriber, never()).onComplete(); |
| 322 |
verify(stringSubscriber, times(0)).onNext("one"); |
| 323 |
verify(stringSubscriber, times(0)).onNext("two"); |
| 324 |
verify(stringSubscriber, times(0)).onNext("three"); |
| 325 |
verify(stringSubscriber, times(1)).onNext("four"); |
| 326 |
verify(stringSubscriber, times(0)).onNext("five"); |
| 327 |
verify(stringSubscriber, times(0)).onNext("six"); |
| 328 |
} |
| 329 |
|
| 330 |
/** |
| 331 |
* Unit test from OperationMergeDelayError backported here to show how these use cases work with normal merge. |
| 332 |
*/ |
| 333 |
@Test |
| 334 |
public void error2() { |
| 335 |
// we are using synchronous execution to test this exactly rather than non-deterministic concurrent behavior |
| 336 |
final Flowable<String> f1 = Flowable.unsafeCreate(new TestErrorFlowable("one", "two", "three")); |
| 337 |
final Flowable<String> f2 = Flowable.unsafeCreate(new TestErrorFlowable("four", null, "six")); // we expect to lose "six" |
| 338 |
final Flowable<String> f3 = Flowable.unsafeCreate(new TestErrorFlowable("seven", "eight", null)); // we expect to lose all of these since o2 is done first and fails |
| 339 |
final Flowable<String> f4 = Flowable.unsafeCreate(new TestErrorFlowable("nine")); // we expect to lose all of these since o2 is done first and fails |
| 340 |
|
| 341 |
Flowable<String> m = Flowable.merge(f1, f2, f3, f4); |
| 342 |
m.subscribe(stringSubscriber); |
| 343 |
|
| 344 |
verify(stringSubscriber, times(1)).onError(any(NullPointerException.class)); |
| 345 |
verify(stringSubscriber, never()).onComplete(); |
| 346 |
verify(stringSubscriber, times(1)).onNext("one"); |
| 347 |
verify(stringSubscriber, times(1)).onNext("two"); |
| 348 |
verify(stringSubscriber, times(1)).onNext("three"); |
| 349 |
verify(stringSubscriber, times(1)).onNext("four"); |
| 350 |
verify(stringSubscriber, times(0)).onNext("five"); |
| 351 |
verify(stringSubscriber, times(0)).onNext("six"); |
| 352 |
verify(stringSubscriber, times(0)).onNext("seven"); |
| 353 |
verify(stringSubscriber, times(0)).onNext("eight"); |
| 354 |
verify(stringSubscriber, times(0)).onNext("nine"); |
| 355 |
} |
| 356 |
|
| 357 |
private static class TestSynchronousFlowable implements Publisher<String> { |
| 358 |
|
| 359 |
@Override |
| 360 |
public void subscribe(Subscriber<? super String> subscriber) { |
| 361 |
subscriber.onSubscribe(new BooleanSubscription()); |
| 362 |
subscriber.onNext("hello"); |
| 363 |
subscriber.onComplete(); |
| 364 |
} |
| 365 |
} |
| 366 |
|
| 367 |
private static class TestASynchronousFlowable implements Publisher<String> { |
| 368 |
Thread t; |
| 369 |
final CountDownLatch onNextBeingSent = new CountDownLatch(1); |
| 370 |
|
| 371 |
@Override |
| 372 |
public void subscribe(final Subscriber<? super String> subscriber) { |
| 373 |
subscriber.onSubscribe(new BooleanSubscription()); |
| 374 |
t = new Thread(new Runnable() { |
| 375 |
|
| 376 |
@Override |
| 377 |
public void run() { |
| 378 |
onNextBeingSent.countDown(); |
| 379 |
try { |
| 380 |
subscriber.onNext("hello"); |
| 381 |
// I can't use a countDownLatch to prove we are actually sending 'onNext' |
| 382 |
// since it will block if synchronized and I'll deadlock |
| 383 |
subscriber.onComplete(); |
| 384 |
} catch (Exception e) { |
| 385 |
subscriber.onError(e); |
| 386 |
} |
| 387 |
} |
| 388 |
|
| 389 |
}, "TestASynchronousFlowable"); |
| 390 |
t.start(); |
| 391 |
} |
| 392 |
} |
| 393 |
|
| 394 |
private static class TestErrorFlowable implements Publisher<String> { |
| 395 |
|
| 396 |
String[] valuesToReturn; |
| 397 |
|
| 398 |
TestErrorFlowable(String... values) { |
| 399 |
valuesToReturn = values; |
| 400 |
} |
| 401 |
|
| 402 |
@Override |
| 403 |
public void subscribe(Subscriber<? super String> subscriber) { |
| 404 |
subscriber.onSubscribe(new BooleanSubscription()); |
| 405 |
for (String s : valuesToReturn) { |
| 406 |
if (s == null) { |
| 407 |
System.out.println("throwing exception"); |
| 408 |
subscriber.onError(new NullPointerException()); |
| 409 |
} else { |
| 410 |
subscriber.onNext(s); |
| 411 |
} |
| 412 |
} |
| 413 |
subscriber.onComplete(); |
| 414 |
} |
| 415 |
} |
| 416 |
|
| 417 |
@Test |
| 418 |
public void unsubscribeAsFlowablesComplete() { |
| 419 |
TestScheduler scheduler1 = new TestScheduler(); |
| 420 |
AtomicBoolean os1 = new AtomicBoolean(false); |
| 421 |
Flowable<Long> f1 = createFlowableOf5IntervalsOf1SecondIncrementsWithSubscriptionHook(scheduler1, os1); |
| 422 |
|
| 423 |
TestScheduler scheduler2 = new TestScheduler(); |
| 424 |
AtomicBoolean os2 = new AtomicBoolean(false); |
| 425 |
Flowable<Long> f2 = createFlowableOf5IntervalsOf1SecondIncrementsWithSubscriptionHook(scheduler2, os2); |
| 426 |
|
| 427 |
TestSubscriberEx<Long> ts = new TestSubscriberEx<>(); |
| 428 |
Flowable.merge(f1, f2).subscribe(ts); |
| 429 |
|
| 430 |
// we haven't incremented time so nothing should be received yet |
| 431 |
ts.assertNoValues(); |
| 432 |
|
| 433 |
scheduler1.advanceTimeBy(3, TimeUnit.SECONDS); |
| 434 |
scheduler2.advanceTimeBy(2, TimeUnit.SECONDS); |
| 435 |
|
| 436 |
ts.assertValues(0L, 1L, 2L, 0L, 1L); |
| 437 |
// not unsubscribed yet |
| 438 |
assertFalse(os1.get()); |
| 439 |
assertFalse(os2.get()); |
| 440 |
|
| 441 |
// advance to the end at which point it should complete |
| 442 |
scheduler1.advanceTimeBy(3, TimeUnit.SECONDS); |
| 443 |
|
| 444 |
ts.assertValues(0L, 1L, 2L, 0L, 1L, 3L, 4L); |
| 445 |
assertTrue(os1.get()); |
| 446 |
assertFalse(os2.get()); |
| 447 |
|
| 448 |
// both should be completed now |
| 449 |
scheduler2.advanceTimeBy(3, TimeUnit.SECONDS); |
| 450 |
|
| 451 |
ts.assertValues(0L, 1L, 2L, 0L, 1L, 3L, 4L, 2L, 3L, 4L); |
| 452 |
assertTrue(os1.get()); |
| 453 |
assertTrue(os2.get()); |
| 454 |
|
| 455 |
ts.assertTerminated(); |
| 456 |
} |
| 457 |
|
| 458 |
@Test |
| 459 |
public void earlyUnsubscribe() { |
| 460 |
for (int i = 0; i < 10; i++) { |
| 461 |
TestScheduler scheduler1 = new TestScheduler(); |
| 462 |
AtomicBoolean os1 = new AtomicBoolean(false); |
| 463 |
Flowable<Long> f1 = createFlowableOf5IntervalsOf1SecondIncrementsWithSubscriptionHook(scheduler1, os1); |
| 464 |
|
| 465 |
TestScheduler scheduler2 = new TestScheduler(); |
| 466 |
AtomicBoolean os2 = new AtomicBoolean(false); |
| 467 |
Flowable<Long> f2 = createFlowableOf5IntervalsOf1SecondIncrementsWithSubscriptionHook(scheduler2, os2); |
| 468 |
|
| 469 |
TestSubscriber<Long> ts = new TestSubscriber<>(); |
| 470 |
Flowable.merge(f1, f2).subscribe(ts); |
| 471 |
|
| 472 |
// we haven't incremented time so nothing should be received yet |
| 473 |
ts.assertNoValues(); |
| 474 |
|
| 475 |
scheduler1.advanceTimeBy(3, TimeUnit.SECONDS); |
| 476 |
scheduler2.advanceTimeBy(2, TimeUnit.SECONDS); |
| 477 |
|
| 478 |
ts.assertValues(0L, 1L, 2L, 0L, 1L); |
| 479 |
// not unsubscribed yet |
| 480 |
assertFalse(os1.get()); |
| 481 |
assertFalse(os2.get()); |
| 482 |
|
| 483 |
// early unsubscribe |
| 484 |
ts.cancel(); |
| 485 |
|
| 486 |
assertTrue(os1.get()); |
| 487 |
assertTrue(os2.get()); |
| 488 |
|
| 489 |
ts.assertValues(0L, 1L, 2L, 0L, 1L); |
| 490 |
} |
| 491 |
} |
| 492 |
|
| 493 |
private Flowable<Long> createFlowableOf5IntervalsOf1SecondIncrementsWithSubscriptionHook(final Scheduler scheduler, final AtomicBoolean unsubscribed) { |
| 494 |
return Flowable.unsafeCreate(new Publisher<Long>() { |
| 495 |
|
| 496 |
@Override |
| 497 |
public void subscribe(final Subscriber<? super Long> child) { |
| 498 |
Flowable.interval(1, TimeUnit.SECONDS, scheduler) |
| 499 |
.take(5) |
| 500 |
.subscribe(new FlowableSubscriber<Long>() { |
| 501 |
@Override |
| 502 |
public void onSubscribe(final Subscription s) { |
| 503 |
child.onSubscribe(new Subscription() { |
| 504 |
@Override |
| 505 |
public void request(long n) { |
| 506 |
s.request(n); |
| 507 |
} |
| 508 |
|
| 509 |
@Override |
| 510 |
public void cancel() { |
| 511 |
unsubscribed.set(true); |
| 512 |
s.cancel(); |
| 513 |
} |
| 514 |
}); |
| 515 |
} |
| 516 |
|
| 517 |
@Override |
| 518 |
public void onNext(Long t) { |
| 519 |
child.onNext(t); |
| 520 |
} |
| 521 |
|
| 522 |
@Override |
| 523 |
public void onError(Throwable t) { |
| 524 |
unsubscribed.set(true); |
| 525 |
child.onError(t); |
| 526 |
} |
| 527 |
|
| 528 |
@Override |
| 529 |
public void onComplete() { |
| 530 |
unsubscribed.set(true); |
| 531 |
child.onComplete(); |
| 532 |
} |
| 533 |
|
| 534 |
}); |
| 535 |
} |
| 536 |
}); |
| 537 |
} |
| 538 |
|
| 539 |
@Test |
| 540 |
public void concurrency() { |
| 541 |
Flowable<Integer> f = Flowable.range(1, 10000).subscribeOn(Schedulers.newThread()); |
| 542 |
|
| 543 |
for (int i = 0; i < 10; i++) { |
| 544 |
Flowable<Integer> merge = Flowable.merge(f.onBackpressureBuffer(), f.onBackpressureBuffer(), f.onBackpressureBuffer()); |
| 545 |
TestSubscriberEx<Integer> ts = new TestSubscriberEx<>(); |
| 546 |
merge.subscribe(ts); |
| 547 |
|
| 548 |
ts.awaitDone(3, TimeUnit.SECONDS); |
| 549 |
ts.assertTerminated(); |
| 550 |
ts.assertNoErrors(); |
| 551 |
ts.assertComplete(); |
| 552 |
List<Integer> onNextEvents = ts.values(); |
| 553 |
assertEquals(30000, onNextEvents.size()); |
| 554 |
// System.out.println("onNext: " + onNextEvents.size() + " onComplete: " + ts.getOnCompletedEvents().size()); |
| 555 |
} |
| 556 |
} |
| 557 |
|
| 558 |
@Test |
| 559 |
public void concurrencyWithSleeping() { |
| 560 |
|
| 561 |
Flowable<Integer> f = Flowable.unsafeCreate(new Publisher<Integer>() { |
| 562 |
|
| 563 |
@Override |
| 564 |
public void subscribe(final Subscriber<? super Integer> s) { |
| 565 |
Worker inner = Schedulers.newThread().createWorker(); |
| 566 |
final AsyncSubscription as = new AsyncSubscription(); |
| 567 |
as.setSubscription(new BooleanSubscription()); |
| 568 |
as.setResource(inner); |
| 569 |
|
| 570 |
s.onSubscribe(as); |
| 571 |
|
| 572 |
inner.schedule(new Runnable() { |
| 573 |
|
| 574 |
@Override |
| 575 |
public void run() { |
| 576 |
try { |
| 577 |
for (int i = 0; i < 100; i++) { |
| 578 |
s.onNext(1); |
| 579 |
try { |
| 580 |
Thread.sleep(1); |
| 581 |
} catch (InterruptedException e) { |
| 582 |
e.printStackTrace(); |
| 583 |
} |
| 584 |
} |
| 585 |
} catch (Exception e) { |
| 586 |
s.onError(e); |
| 587 |
} |
| 588 |
as.dispose(); |
| 589 |
s.onComplete(); |
| 590 |
} |
| 591 |
|
| 592 |
}); |
| 593 |
} |
| 594 |
}); |
| 595 |
|
| 596 |
for (int i = 0; i < 10; i++) { |
| 597 |
Flowable<Integer> merge = Flowable.merge(f, f, f); |
| 598 |
TestSubscriber<Integer> ts = new TestSubscriber<>(); |
| 599 |
merge.subscribe(ts); |
| 600 |
|
| 601 |
ts.awaitDone(10, TimeUnit.SECONDS); |
| 602 |
ts.assertComplete(); |
| 603 |
List<Integer> onNextEvents = ts.values(); |
| 604 |
assertEquals(300, onNextEvents.size()); |
| 605 |
// System.out.println("onNext: " + onNextEvents.size() + " onComplete: " + ts.getOnCompletedEvents().size()); |
| 606 |
} |
| 607 |
} |
| 608 |
|
| 609 |
@Test |
| 610 |
public void concurrencyWithBrokenOnCompleteContract() { |
| 611 |
Flowable<Integer> f = Flowable.unsafeCreate(new Publisher<Integer>() { |
| 612 |
|
| 613 |
@Override |
| 614 |
public void subscribe(final Subscriber<? super Integer> s) { |
| 615 |
Worker inner = Schedulers.newThread().createWorker(); |
| 616 |
final AsyncSubscription as = new AsyncSubscription(); |
| 617 |
as.setSubscription(new BooleanSubscription()); |
| 618 |
as.setResource(inner); |
| 619 |
|
| 620 |
s.onSubscribe(as); |
| 621 |
|
| 622 |
inner.schedule(new Runnable() { |
| 623 |
|
| 624 |
@Override |
| 625 |
public void run() { |
| 626 |
try { |
| 627 |
for (int i = 0; i < 10000; i++) { |
| 628 |
s.onNext(i); |
| 629 |
} |
| 630 |
} catch (Exception e) { |
| 631 |
s.onError(e); |
| 632 |
} |
| 633 |
as.dispose(); |
| 634 |
s.onComplete(); |
| 635 |
s.onComplete(); |
| 636 |
s.onComplete(); |
| 637 |
} |
| 638 |
|
| 639 |
}); |
| 640 |
} |
| 641 |
}); |
| 642 |
|
| 643 |
for (int i = 0; i < 10; i++) { |
| 644 |
Flowable<Integer> merge = Flowable.merge(f.onBackpressureBuffer(), f.onBackpressureBuffer(), f.onBackpressureBuffer()); |
| 645 |
TestSubscriber<Integer> ts = new TestSubscriber<>(); |
| 646 |
merge.subscribe(ts); |
| 647 |
|
| 648 |
ts.awaitDone(10, TimeUnit.SECONDS); |
| 649 |
ts.assertNoErrors(); |
| 650 |
ts.assertComplete(); |
| 651 |
List<Integer> onNextEvents = ts.values(); |
| 652 |
assertEquals(30000, onNextEvents.size()); |
| 653 |
// System.out.println("onNext: " + onNextEvents.size() + " onComplete: " + ts.getOnCompletedEvents().size()); |
| 654 |
} |
| 655 |
} |
| 656 |
|
| 657 |
@Test |
| 658 |
public void backpressureUpstream() throws InterruptedException { |
| 659 |
final AtomicInteger generated1 = new AtomicInteger(); |
| 660 |
Flowable<Integer> f1 = createInfiniteFlowable(generated1).subscribeOn(Schedulers.computation()); |
| 661 |
final AtomicInteger generated2 = new AtomicInteger(); |
| 662 |
Flowable<Integer> f2 = createInfiniteFlowable(generated2).subscribeOn(Schedulers.computation()); |
| 663 |
|
| 664 |
TestSubscriberEx<Integer> testSubscriber = new TestSubscriberEx<Integer>() { |
| 665 |
@Override |
| 666 |
public void onNext(Integer t) { |
| 667 |
System.err.println("testSubscriber received => " + t + " on thread " + Thread.currentThread()); |
| 668 |
super.onNext(t); |
| 669 |
} |
| 670 |
}; |
| 671 |
|
| 672 |
Flowable.merge(f1.take(Flowable.bufferSize() * 2), f2.take(Flowable.bufferSize() * 2)).subscribe(testSubscriber); |
| 673 |
testSubscriber.awaitDone(5, TimeUnit.SECONDS); |
| 674 |
if (testSubscriber.errors().size() > 0) { |
| 675 |
testSubscriber.errors().get(0).printStackTrace(); |
| 676 |
} |
| 677 |
testSubscriber.assertNoErrors(); |
| 678 |
System.err.println(testSubscriber.values()); |
| 679 |
assertEquals(Flowable.bufferSize() * 4, testSubscriber.values().size()); |
| 680 |
// it should be between the take num and requested batch size across the async boundary |
| 681 |
System.out.println("Generated 1: " + generated1.get()); |
| 682 |
System.out.println("Generated 2: " + generated2.get()); |
| 683 |
assertTrue(generated1.get() >= Flowable.bufferSize() * 2 && generated1.get() <= Flowable.bufferSize() * 4); |
| 684 |
} |
| 685 |
|
| 686 |
@Test |
| 687 |
public void backpressureUpstream2InLoop() throws InterruptedException { |
| 688 |
for (int i = 0; i < 1000; i++) { |
| 689 |
System.err.flush(); |
| 690 |
System.out.println("---"); |
| 691 |
System.out.flush(); |
| 692 |
backpressureUpstream2(); |
| 693 |
} |
| 694 |
} |
| 695 |
|
| 696 |
@Test |
| 697 |
public void backpressureUpstream2() throws InterruptedException { |
| 698 |
final AtomicInteger generated1 = new AtomicInteger(); |
| 699 |
Flowable<Integer> f1 = createInfiniteFlowable(generated1).subscribeOn(Schedulers.computation()); |
| 700 |
|
| 701 |
TestSubscriberEx<Integer> testSubscriber = new TestSubscriberEx<Integer>() { |
| 702 |
@Override |
| 703 |
public void onNext(Integer t) { |
| 704 |
super.onNext(t); |
| 705 |
} |
| 706 |
}; |
| 707 |
|
| 708 |
Flowable.merge(f1.take(Flowable.bufferSize() * 2), Flowable.just(-99)).subscribe(testSubscriber); |
| 709 |
testSubscriber.awaitDone(10, TimeUnit.SECONDS); |
| 710 |
|
| 711 |
List<Integer> onNextEvents = testSubscriber.values(); |
| 712 |
|
| 713 |
System.out.println("Generated 1: " + generated1.get() + " / received: " + onNextEvents.size()); |
| 714 |
System.out.println(onNextEvents); |
| 715 |
|
| 716 |
if (testSubscriber.errors().size() > 0) { |
| 717 |
testSubscriber.errors().get(0).printStackTrace(); |
| 718 |
} |
| 719 |
testSubscriber.assertNoErrors(); |
| 720 |
assertEquals(Flowable.bufferSize() * 2 + 1, onNextEvents.size()); |
| 721 |
// it should be between the take num and requested batch size across the async boundary |
| 722 |
assertTrue(generated1.get() >= Flowable.bufferSize() * 2 && generated1.get() <= Flowable.bufferSize() * 3); |
| 723 |
} |
| 724 |
|
| 725 |
/** |
| 726 |
* This is the same as the upstreams ones, but now adds the downstream as well by using observeOn. |
| 727 |
* |
| 728 |
* This requires merge to also obey the Product.request values coming from it's child subscriber. |
| 729 |
* @throws InterruptedException if the test is interrupted |
| 730 |
*/ |
| 731 |
@Test |
| 732 |
public void backpressureDownstreamWithConcurrentStreams() throws InterruptedException { |
| 733 |
final AtomicInteger generated1 = new AtomicInteger(); |
| 734 |
Flowable<Integer> f1 = createInfiniteFlowable(generated1).subscribeOn(Schedulers.computation()); |
| 735 |
final AtomicInteger generated2 = new AtomicInteger(); |
| 736 |
Flowable<Integer> f2 = createInfiniteFlowable(generated2).subscribeOn(Schedulers.computation()); |
| 737 |
|
| 738 |
TestSubscriberEx<Integer> testSubscriber = new TestSubscriberEx<Integer>() { |
| 739 |
@Override |
| 740 |
public void onNext(Integer t) { |
| 741 |
if (t < 100) { |
| 742 |
try { |
| 743 |
// force a slow consumer |
| 744 |
Thread.sleep(1); |
| 745 |
} catch (InterruptedException e) { |
| 746 |
e.printStackTrace(); |
| 747 |
} |
| 748 |
} |
| 749 |
// System.err.println("testSubscriber received => " + t + " on thread " + Thread.currentThread()); |
| 750 |
super.onNext(t); |
| 751 |
} |
| 752 |
}; |
| 753 |
|
| 754 |
Flowable.merge(f1.take(Flowable.bufferSize() * 2), f2.take(Flowable.bufferSize() * 2)).observeOn(Schedulers.computation()).subscribe(testSubscriber); |
| 755 |
testSubscriber.awaitDone(10, TimeUnit.SECONDS); |
| 756 |
if (testSubscriber.errors().size() > 0) { |
| 757 |
testSubscriber.errors().get(0).printStackTrace(); |
| 758 |
} |
| 759 |
testSubscriber.assertNoErrors(); |
| 760 |
System.err.println(testSubscriber.values()); |
| 761 |
assertEquals(Flowable.bufferSize() * 4, testSubscriber.values().size()); |
| 762 |
// it should be between the take num and requested batch size across the async boundary |
| 763 |
System.out.println("Generated 1: " + generated1.get()); |
| 764 |
System.out.println("Generated 2: " + generated2.get()); |
| 765 |
assertTrue(generated1.get() >= Flowable.bufferSize() * 2 && generated1.get() <= Flowable.bufferSize() * 4); |
| 766 |
} |
| 767 |
|
| 768 |
@Test |
| 769 |
public void backpressureBothUpstreamAndDownstreamWithSynchronousScalarFlowables() throws InterruptedException { |
| 770 |
final AtomicInteger generated1 = new AtomicInteger(); |
| 771 |
Flowable<Flowable<Integer>> f1 = createInfiniteFlowable(generated1) |
| 772 |
.map(new Function<Integer, Flowable<Integer>>() { |
| 773 |
|
| 774 |
@Override |
| 775 |
public Flowable<Integer> apply(Integer t1) { |
| 776 |
return Flowable.just(t1); |
| 777 |
} |
| 778 |
|
| 779 |
}); |
| 780 |
|
| 781 |
TestSubscriberEx<Integer> testSubscriber = new TestSubscriberEx<Integer>() { |
| 782 |
@Override |
| 783 |
public void onNext(Integer t) { |
| 784 |
if (t < 100) { |
| 785 |
try { |
| 786 |
// force a slow consumer |
| 787 |
Thread.sleep(2); |
| 788 |
} catch (InterruptedException e) { |
| 789 |
e.printStackTrace(); |
| 790 |
} |
| 791 |
} |
| 792 |
// System.err.println("testSubscriber received => " + t + " on thread " + Thread.currentThread()); |
| 793 |
super.onNext(t); |
| 794 |
} |
| 795 |
}; |
| 796 |
|
| 797 |
Flowable.merge(f1).observeOn(Schedulers.computation()).take(Flowable.bufferSize() * 2).subscribe(testSubscriber); |
| 798 |
testSubscriber.awaitDone(10, TimeUnit.SECONDS); |
| 799 |
if (testSubscriber.errors().size() > 0) { |
| 800 |
testSubscriber.errors().get(0).printStackTrace(); |
| 801 |
} |
| 802 |
testSubscriber.assertNoErrors(); |
| 803 |
System.out.println("Generated 1: " + generated1.get()); |
| 804 |
System.err.println(testSubscriber.values()); |
| 805 |
assertEquals(Flowable.bufferSize() * 2, testSubscriber.values().size()); |
| 806 |
// it should be between the take num and requested batch size across the async boundary |
| 807 |
assertTrue(generated1.get() >= Flowable.bufferSize() * 2 && generated1.get() <= Flowable.bufferSize() * 4); |
| 808 |
} |
| 809 |
|
| 810 |
/** |
| 811 |
* Currently there is no solution to this ... we can't exert backpressure on the outer Flowable if we |
| 812 |
* can't know if the ones we've received so far are going to emit or not, otherwise we could starve the system. |
| 813 |
* |
| 814 |
* For example, 10,000 Flowables are being merged (bad use case to begin with, but ...) and it's only one of them |
| 815 |
* that will ever emit. If backpressure only allowed the first 1,000 to be sent, we would hang and never receive an event. |
| 816 |
* |
| 817 |
* Thus, we must allow all Flowables to be sent. The ScalarSynchronousFlowable use case is an exception to this since |
| 818 |
* we can grab the value synchronously. |
| 819 |
* |
| 820 |
* @throws InterruptedException if the await is interrupted |
| 821 |
*/ |
| 822 |
@Test |
| 823 |
public void backpressureBothUpstreamAndDownstreamWithRegularFlowables() throws InterruptedException { |
| 824 |
final AtomicInteger generated1 = new AtomicInteger(); |
| 825 |
Flowable<Flowable<Integer>> f1 = createInfiniteFlowable(generated1).map(new Function<Integer, Flowable<Integer>>() { |
| 826 |
|
| 827 |
@Override |
| 828 |
public Flowable<Integer> apply(Integer t1) { |
| 829 |
return Flowable.just(1, 2, 3); |
| 830 |
} |
| 831 |
|
| 832 |
}); |
| 833 |
|
| 834 |
TestSubscriberEx<Integer> testSubscriber = new TestSubscriberEx<Integer>() { |
| 835 |
int i; |
| 836 |
|
| 837 |
@Override |
| 838 |
public void onNext(Integer t) { |
| 839 |
if (i++ < 400) { |
| 840 |
try { |
| 841 |
// force a slow consumer |
| 842 |
Thread.sleep(1); |
| 843 |
} catch (InterruptedException e) { |
| 844 |
e.printStackTrace(); |
| 845 |
} |
| 846 |
} |
| 847 |
// System.err.println("testSubscriber received => " + t + " on thread " + Thread.currentThread()); |
| 848 |
super.onNext(t); |
| 849 |
} |
| 850 |
}; |
| 851 |
|
| 852 |
Flowable.merge(f1).observeOn(Schedulers.computation()).take(Flowable.bufferSize() * 2).subscribe(testSubscriber); |
| 853 |
testSubscriber.awaitDone(10, TimeUnit.SECONDS); |
| 854 |
if (testSubscriber.errors().size() > 0) { |
| 855 |
testSubscriber.errors().get(0).printStackTrace(); |
| 856 |
} |
| 857 |
testSubscriber.assertNoErrors(); |
| 858 |
System.out.println("Generated 1: " + generated1.get()); |
| 859 |
System.err.println(testSubscriber.values()); |
| 860 |
System.out.println("done1 testBackpressureBothUpstreamAndDownstreamWithRegularFlowables "); |
| 861 |
assertEquals(Flowable.bufferSize() * 2, testSubscriber.values().size()); |
| 862 |
System.out.println("done2 testBackpressureBothUpstreamAndDownstreamWithRegularFlowables "); |
| 863 |
// we can't restrict this ... see comment above |
| 864 |
// assertTrue(generated1.get() >= Flowable.bufferSize() && generated1.get() <= Flowable.bufferSize() * 4); |
| 865 |
} |
| 866 |
|
| 867 |
@Test |
| 868 |
public void merge1AsyncStreamOf1() { |
| 869 |
TestSubscriber<Integer> ts = new TestSubscriber<>(); |
| 870 |
mergeNAsyncStreamsOfN(1, 1).subscribe(ts); |
| 871 |
ts.awaitDone(10, TimeUnit.SECONDS); |
| 872 |
ts.assertNoErrors(); |
| 873 |
assertEquals(1, ts.values().size()); |
| 874 |
} |
| 875 |
|
| 876 |
@Test |
| 877 |
public void merge1AsyncStreamOf1000() { |
| 878 |
TestSubscriber<Integer> ts = new TestSubscriber<>(); |
| 879 |
mergeNAsyncStreamsOfN(1, 1000).subscribe(ts); |
| 880 |
ts.awaitDone(10, TimeUnit.SECONDS); |
| 881 |
ts.assertNoErrors(); |
| 882 |
assertEquals(1000, ts.values().size()); |
| 883 |
} |
| 884 |
|
| 885 |
@Test |
| 886 |
public void merge10AsyncStreamOf1000() { |
| 887 |
TestSubscriber<Integer> ts = new TestSubscriber<>(); |
| 888 |
mergeNAsyncStreamsOfN(10, 1000).subscribe(ts); |
| 889 |
ts.awaitDone(10, TimeUnit.SECONDS); |
| 890 |
ts.assertNoErrors(); |
| 891 |
assertEquals(10000, ts.values().size()); |
| 892 |
} |
| 893 |
|
| 894 |
@Test |
| 895 |
public void merge1000AsyncStreamOf1000() { |
| 896 |
TestSubscriber<Integer> ts = new TestSubscriber<>(); |
| 897 |
mergeNAsyncStreamsOfN(1000, 1000).subscribe(ts); |
| 898 |
ts.awaitDone(10, TimeUnit.SECONDS); |
| 899 |
ts.assertNoErrors(); |
| 900 |
assertEquals(1000000, ts.values().size()); |
| 901 |
} |
| 902 |
|
| 903 |
@Test |
| 904 |
public void merge2000AsyncStreamOf100() { |
| 905 |
TestSubscriber<Integer> ts = new TestSubscriber<>(); |
| 906 |
mergeNAsyncStreamsOfN(2000, 100).subscribe(ts); |
| 907 |
ts.awaitDone(10, TimeUnit.SECONDS); |
| 908 |
ts.assertNoErrors(); |
| 909 |
assertEquals(200000, ts.values().size()); |
| 910 |
} |
| 911 |
|
| 912 |
@Test |
| 913 |
public void merge100AsyncStreamOf1() { |
| 914 |
TestSubscriber<Integer> ts = new TestSubscriber<>(); |
| 915 |
mergeNAsyncStreamsOfN(100, 1).subscribe(ts); |
| 916 |
ts.awaitDone(10, TimeUnit.SECONDS); |
| 917 |
ts.assertNoErrors(); |
| 918 |
assertEquals(100, ts.values().size()); |
| 919 |
} |
| 920 |
|
| 921 |
private Flowable<Integer> mergeNAsyncStreamsOfN(final int outerSize, final int innerSize) { |
| 922 |
Flowable<Flowable<Integer>> os = Flowable.range(1, outerSize) |
| 923 |
.map(new Function<Integer, Flowable<Integer>>() { |
| 924 |
|
| 925 |
@Override |
| 926 |
public Flowable<Integer> apply(Integer i) { |
| 927 |
return Flowable.range(1, innerSize).subscribeOn(Schedulers.computation()); |
| 928 |
} |
| 929 |
|
| 930 |
}); |
| 931 |
return Flowable.merge(os); |
| 932 |
} |
| 933 |
|
| 934 |
@Test |
| 935 |
public void merge1SyncStreamOf1() { |
| 936 |
TestSubscriber<Integer> ts = new TestSubscriber<>(); |
| 937 |
mergeNSyncStreamsOfN(1, 1).subscribe(ts); |
| 938 |
ts.awaitDone(10, TimeUnit.SECONDS); |
| 939 |
ts.assertNoErrors(); |
| 940 |
assertEquals(1, ts.values().size()); |
| 941 |
} |
| 942 |
|
| 943 |
@Test |
| 944 |
public void merge1SyncStreamOf1000000() { |
| 945 |
TestSubscriber<Integer> ts = new TestSubscriber<>(); |
| 946 |
mergeNSyncStreamsOfN(1, 1000000).subscribe(ts); |
| 947 |
ts.awaitDone(10, TimeUnit.SECONDS); |
| 948 |
ts.assertNoErrors(); |
| 949 |
assertEquals(1000000, ts.values().size()); |
| 950 |
} |
| 951 |
|
| 952 |
@Test |
| 953 |
public void merge1000SyncStreamOf1000() { |
| 954 |
TestSubscriber<Integer> ts = new TestSubscriber<>(); |
| 955 |
mergeNSyncStreamsOfN(1000, 1000).subscribe(ts); |
| 956 |
ts.awaitDone(10, TimeUnit.SECONDS); |
| 957 |
ts.assertNoErrors(); |
| 958 |
assertEquals(1000000, ts.values().size()); |
| 959 |
} |
| 960 |
|
| 961 |
@Test |
| 962 |
public void merge10000SyncStreamOf10() { |
| 963 |
TestSubscriber<Integer> ts = new TestSubscriber<>(); |
| 964 |
mergeNSyncStreamsOfN(10000, 10).subscribe(ts); |
| 965 |
ts.awaitDone(10, TimeUnit.SECONDS); |
| 966 |
ts.assertNoErrors(); |
| 967 |
assertEquals(100000, ts.values().size()); |
| 968 |
} |
| 969 |
|
| 970 |
@Test |
| 971 |
public void merge1000000SyncStreamOf1() { |
| 972 |
TestSubscriber<Integer> ts = new TestSubscriber<>(); |
| 973 |
mergeNSyncStreamsOfN(1000000, 1).subscribe(ts); |
| 974 |
ts.awaitDone(10, TimeUnit.SECONDS); |
| 975 |
ts.assertNoErrors(); |
| 976 |
assertEquals(1000000, ts.values().size()); |
| 977 |
} |
| 978 |
|
| 979 |
private Flowable<Integer> mergeNSyncStreamsOfN(final int outerSize, final int innerSize) { |
| 980 |
Flowable<Flowable<Integer>> os = Flowable.range(1, outerSize) |
| 981 |
.map(new Function<Integer, Flowable<Integer>>() { |
| 982 |
|
| 983 |
@Override |
| 984 |
public Flowable<Integer> apply(Integer i) { |
| 985 |
return Flowable.range(1, innerSize); |
| 986 |
} |
| 987 |
|
| 988 |
}); |
| 989 |
return Flowable.merge(os); |
| 990 |
} |
| 991 |
|
| 992 |
private Flowable<Integer> createInfiniteFlowable(final AtomicInteger generated) { |
| 993 |
Flowable<Integer> flowable = Flowable.fromIterable(new Iterable<Integer>() { |
| 994 |
@Override |
| 995 |
public Iterator<Integer> iterator() { |
| 996 |
return new Iterator<Integer>() { |
| 997 |
|
| 998 |
@Override |
| 999 |
public void remove() { |
| 1000 |
} |
| 1001 |
|
| 1002 |
@Override |
| 1003 |
public Integer next() { |
| 1004 |
return generated.getAndIncrement(); |
| 1005 |
} |
| 1006 |
|
| 1007 |
@Override |
| 1008 |
public boolean hasNext() { |
| 1009 |
return true; |
| 1010 |
} |
| 1011 |
}; |
| 1012 |
} |
| 1013 |
}); |
| 1014 |
return flowable; |
| 1015 |
} |
| 1016 |
|
| 1017 |
@Test |
| 1018 |
public void mergeManyAsyncSingle() { |
| 1019 |
TestSubscriber<Integer> ts = new TestSubscriber<>(); |
| 1020 |
Flowable<Flowable<Integer>> os = Flowable.range(1, 10000) |
| 1021 |
.map(new Function<Integer, Flowable<Integer>>() { |
| 1022 |
|
| 1023 |
@Override |
| 1024 |
public Flowable<Integer> apply(final Integer i) { |
| 1025 |
return Flowable.unsafeCreate(new Publisher<Integer>() { |
| 1026 |
|
| 1027 |
@Override |
| 1028 |
public void subscribe(Subscriber<? super Integer> s) { |
| 1029 |
s.onSubscribe(new BooleanSubscription()); |
| 1030 |
if (i < 500) { |
| 1031 |
try { |
| 1032 |
Thread.sleep(1); |
| 1033 |
} catch (InterruptedException e) { |
| 1034 |
e.printStackTrace(); |
| 1035 |
} |
| 1036 |
} |
| 1037 |
s.onNext(i); |
| 1038 |
s.onComplete(); |
| 1039 |
} |
| 1040 |
|
| 1041 |
}).subscribeOn(Schedulers.computation()).cache(); |
| 1042 |
} |
| 1043 |
|
| 1044 |
}); |
| 1045 |
Flowable.merge(os).subscribe(ts); |
| 1046 |
ts.awaitDone(10, TimeUnit.SECONDS); |
| 1047 |
ts.assertNoErrors(); |
| 1048 |
assertEquals(10000, ts.values().size()); |
| 1049 |
} |
| 1050 |
|
| 1051 |
@Test |
| 1052 |
public void shouldCompleteAfterApplyingBackpressure_NormalPath() { |
| 1053 |
Flowable<Integer> source = Flowable.mergeDelayError(Flowable.just(Flowable.range(1, 2))); |
| 1054 |
TestSubscriberEx<Integer> subscriber = new TestSubscriberEx<>(0L); |
| 1055 |
source.subscribe(subscriber); |
| 1056 |
subscriber.request(3); // 1, 2, <complete> - with request(2) we get the 1 and 2 but not the <complete> |
| 1057 |
subscriber.assertValues(1, 2); |
| 1058 |
subscriber.assertTerminated(); |
| 1059 |
} |
| 1060 |
|
| 1061 |
@Test |
| 1062 |
public void shouldCompleteAfterApplyingBackpressure_FastPath() { |
| 1063 |
Flowable<Integer> source = Flowable.mergeDelayError(Flowable.just(Flowable.just(1))); |
| 1064 |
TestSubscriberEx<Integer> subscriber = new TestSubscriberEx<>(0L); |
| 1065 |
source.subscribe(subscriber); |
| 1066 |
subscriber.request(2); // 1, <complete> - should work as per .._NormalPath above |
| 1067 |
subscriber.assertValue(1); |
| 1068 |
subscriber.assertTerminated(); |
| 1069 |
} |
| 1070 |
|
| 1071 |
@Test |
| 1072 |
public void shouldNotCompleteIfThereArePendingScalarSynchronousEmissionsWhenTheLastInnerSubscriberCompletes() { |
| 1073 |
TestScheduler scheduler = new TestScheduler(); |
| 1074 |
Flowable<Long> source = Flowable.mergeDelayError(Flowable.just(1L), Flowable.timer(1, TimeUnit.SECONDS, scheduler).skip(1)); |
| 1075 |
TestSubscriberEx<Long> subscriber = new TestSubscriberEx<>(0L); |
| 1076 |
source.subscribe(subscriber); |
| 1077 |
scheduler.advanceTimeBy(1, TimeUnit.SECONDS); |
| 1078 |
subscriber.assertNoValues(); |
| 1079 |
subscriber.assertNotComplete(); |
| 1080 |
subscriber.request(1); |
| 1081 |
subscriber.assertValue(1L); |
| 1082 |
// TODO: it should be acceptable to get a completion event without requests |
| 1083 |
// assertEquals(Collections.<Notification<Long>>emptyList(), subscriber.getOnCompletedEvents()); |
| 1084 |
// subscriber.request(1); |
| 1085 |
subscriber.assertTerminated(); |
| 1086 |
} |
| 1087 |
|
| 1088 |
@Test |
| 1089 |
public void delayedErrorsShouldBeEmittedWhenCompleteAfterApplyingBackpressure_NormalPath() { |
| 1090 |
Throwable exception = new Throwable(); |
| 1091 |
Flowable<Integer> source = Flowable.mergeDelayError(Flowable.range(1, 2), Flowable.<Integer>error(exception)); |
| 1092 |
TestSubscriberEx<Integer> subscriber = new TestSubscriberEx<>(0L); |
| 1093 |
source.subscribe(subscriber); |
| 1094 |
subscriber.request(3); // 1, 2, <error> |
| 1095 |
subscriber.assertValues(1, 2); |
| 1096 |
subscriber.assertTerminated(); |
| 1097 |
assertEquals(asList(exception), subscriber.errors()); |
| 1098 |
} |
| 1099 |
|
| 1100 |
@Test |
| 1101 |
public void delayedErrorsShouldBeEmittedWhenCompleteAfterApplyingBackpressure_FastPath() { |
| 1102 |
Throwable exception = new Throwable(); |
| 1103 |
Flowable<Integer> source = Flowable.mergeDelayError(Flowable.just(1), Flowable.<Integer>error(exception)); |
| 1104 |
TestSubscriberEx<Integer> subscriber = new TestSubscriberEx<>(0L); |
| 1105 |
source.subscribe(subscriber); |
| 1106 |
subscriber.request(2); // 1, <error> |
| 1107 |
subscriber.assertValue(1); |
| 1108 |
subscriber.assertTerminated(); |
| 1109 |
assertEquals(asList(exception), subscriber.errors()); |
| 1110 |
} |
| 1111 |
|
| 1112 |
@Test |
| 1113 |
public void shouldNotCompleteWhileThereAreStillScalarSynchronousEmissionsInTheQueue() { |
| 1114 |
Flowable<Integer> source = Flowable.merge(Flowable.just(1), Flowable.just(2)); |
| 1115 |
TestSubscriber<Integer> subscriber = new TestSubscriber<>(1L); |
| 1116 |
source.subscribe(subscriber); |
| 1117 |
subscriber.assertValue(1); |
| 1118 |
subscriber.request(1); |
| 1119 |
subscriber.assertValues(1, 2); |
| 1120 |
} |
| 1121 |
|
| 1122 |
@Test |
| 1123 |
public void shouldNotReceivedDelayedErrorWhileThereAreStillScalarSynchronousEmissionsInTheQueue() { |
| 1124 |
Throwable exception = new Throwable(); |
| 1125 |
Flowable<Integer> source = Flowable.mergeDelayError(Flowable.just(1), Flowable.just(2), Flowable.<Integer>error(exception)); |
| 1126 |
TestSubscriberEx<Integer> subscriber = new TestSubscriberEx<>(0L); |
| 1127 |
subscriber.request(1); |
| 1128 |
source.subscribe(subscriber); |
| 1129 |
subscriber.assertValue(1); |
| 1130 |
assertEquals(Collections.<Throwable>emptyList(), subscriber.errors()); |
| 1131 |
subscriber.request(1); |
| 1132 |
subscriber.assertValues(1, 2); |
| 1133 |
assertEquals(asList(exception), subscriber.errors()); |
| 1134 |
} |
| 1135 |
|
| 1136 |
@Test |
| 1137 |
public void shouldNotReceivedDelayedErrorWhileThereAreStillNormalEmissionsInTheQueue() { |
| 1138 |
Throwable exception = new Throwable(); |
| 1139 |
Flowable<Integer> source = Flowable.mergeDelayError(Flowable.range(1, 2), Flowable.range(3, 2), Flowable.<Integer>error(exception)); |
| 1140 |
TestSubscriberEx<Integer> subscriber = new TestSubscriberEx<>(0L); |
| 1141 |
subscriber.request(3); |
| 1142 |
source.subscribe(subscriber); |
| 1143 |
subscriber.assertValues(1, 2, 3); |
| 1144 |
assertEquals(Collections.<Throwable>emptyList(), subscriber.errors()); |
| 1145 |
subscriber.request(2); |
| 1146 |
subscriber.assertValues(1, 2, 3, 4); |
| 1147 |
assertEquals(asList(exception), subscriber.errors()); |
| 1148 |
} |
| 1149 |
|
| 1150 |
@Test |
| 1151 |
public void mergeKeepsRequesting() throws InterruptedException { |
| 1152 |
//for (int i = 0; i < 5000; i++) { |
| 1153 |
//System.out.println(i + "......................................................................."); |
| 1154 |
final CountDownLatch latch = new CountDownLatch(1); |
| 1155 |
final ConcurrentLinkedQueue<String> messages = new ConcurrentLinkedQueue<>(); |
| 1156 |
|
| 1157 |
Flowable.range(1, 2) |
| 1158 |
// produce many integers per second |
| 1159 |
.flatMap(new Function<Integer, Flowable<Integer>>() { |
| 1160 |
@Override |
| 1161 |
public Flowable<Integer> apply(final Integer number) { |
| 1162 |
return Flowable.range(1, Integer.MAX_VALUE) |
| 1163 |
.doOnRequest(new LongConsumer() { |
| 1164 |
|
| 1165 |
@Override |
| 1166 |
public void accept(long n) { |
| 1167 |
messages.add(">>>>>>>> A requested[" + number + "]: " + n); |
| 1168 |
} |
| 1169 |
|
| 1170 |
}) |
| 1171 |
// pause a bit |
| 1172 |
.doOnNext(pauseForMs(3)) |
| 1173 |
// buffer on backpressure |
| 1174 |
.onBackpressureBuffer() |
| 1175 |
// do in parallel |
| 1176 |
.subscribeOn(Schedulers.computation()) |
| 1177 |
.doOnRequest(new LongConsumer() { |
| 1178 |
|
| 1179 |
@Override |
| 1180 |
public void accept(long n) { |
| 1181 |
messages.add(">>>>>>>> B requested[" + number + "]: " + n); |
| 1182 |
} |
| 1183 |
|
| 1184 |
}); |
| 1185 |
} |
| 1186 |
|
| 1187 |
}) |
| 1188 |
// take a number bigger than 2* Flowable.bufferSize() (used by OperatorMerge) |
| 1189 |
.take(Flowable.bufferSize() * 2 + 1) |
| 1190 |
// log count |
| 1191 |
.doOnNext(printCount()) |
| 1192 |
// release latch |
| 1193 |
.doOnComplete(new Action() { |
| 1194 |
@Override |
| 1195 |
public void run() { |
| 1196 |
latch.countDown(); |
| 1197 |
} |
| 1198 |
}).subscribe(); |
| 1199 |
boolean a = latch.await(10, TimeUnit.SECONDS); |
| 1200 |
if (!a) { |
| 1201 |
for (String s : messages) { |
| 1202 |
System.out.println("DEBUG => " + s); |
| 1203 |
} |
| 1204 |
} |
| 1205 |
assertTrue(a); |
| 1206 |
//} |
| 1207 |
} |
| 1208 |
|
| 1209 |
@Test |
| 1210 |
public void mergeRequestOverflow() throws InterruptedException { |
| 1211 |
//do a non-trivial merge so that future optimisations with EMPTY don't invalidate this test |
| 1212 |
Flowable<Integer> f = Flowable.fromIterable(Arrays.asList(1, 2)) |
| 1213 |
.mergeWith(Flowable.fromIterable(Arrays.asList(3, 4))); |
| 1214 |
final int expectedCount = 4; |
| 1215 |
final CountDownLatch latch = new CountDownLatch(expectedCount); |
| 1216 |
f.subscribeOn(Schedulers.computation()).subscribe(new DefaultSubscriber<Integer>() { |
| 1217 |
|
| 1218 |
@Override |
| 1219 |
public void onStart() { |
| 1220 |
request(1); |
| 1221 |
} |
| 1222 |
|
| 1223 |
@Override |
| 1224 |
public void onComplete() { |
| 1225 |
//ignore |
| 1226 |
} |
| 1227 |
|
| 1228 |
@Override |
| 1229 |
public void onError(Throwable e) { |
| 1230 |
throw new RuntimeException(e); |
| 1231 |
} |
| 1232 |
|
| 1233 |
@Override |
| 1234 |
public void onNext(Integer t) { |
| 1235 |
latch.countDown(); |
| 1236 |
request(2); |
| 1237 |
request(Long.MAX_VALUE - 1); |
| 1238 |
}}); |
| 1239 |
assertTrue(latch.await(10, TimeUnit.SECONDS)); |
| 1240 |
} |
| 1241 |
|
| 1242 |
private static Consumer<Integer> printCount() { |
| 1243 |
return new Consumer<Integer>() { |
| 1244 |
long count; |
| 1245 |
|
| 1246 |
@Override |
| 1247 |
public void accept(Integer t1) { |
| 1248 |
count++; |
| 1249 |
System.out.println("count=" + count); |
| 1250 |
} |
| 1251 |
}; |
| 1252 |
} |
| 1253 |
|
| 1254 |
private static Consumer<Integer> pauseForMs(final long time) { |
| 1255 |
return new Consumer<Integer>() { |
| 1256 |
@Override |
| 1257 |
public void accept(Integer s) { |
| 1258 |
try { |
| 1259 |
Thread.sleep(time); |
| 1260 |
} catch (InterruptedException e) { |
| 1261 |
throw new RuntimeException(e); |
| 1262 |
} |
| 1263 |
} |
| 1264 |
}; |
| 1265 |
} |
| 1266 |
|
| 1267 |
Function<Integer, Flowable<Integer>> toScalar = new Function<Integer, Flowable<Integer>>() { |
| 1268 |
@Override |
| 1269 |
public Flowable<Integer> apply(Integer v) { |
| 1270 |
return Flowable.just(v); |
| 1271 |
} |
| 1272 |
}; |
| 1273 |
|
| 1274 |
Function<Integer, Flowable<Integer>> toHiddenScalar = new Function<Integer, Flowable<Integer>>() { |
| 1275 |
@Override |
| 1276 |
public Flowable<Integer> apply(Integer t) { |
| 1277 |
return Flowable.just(t).hide(); |
| 1278 |
} |
| 1279 |
}; |
| 1280 |
; |
| 1281 |
|
| 1282 |
void runMerge(Function<Integer, Flowable<Integer>> func, TestSubscriberEx<Integer> ts) { |
| 1283 |
List<Integer> list = new ArrayList<>(); |
| 1284 |
for (int i = 0; i < 1000; i++) { |
| 1285 |
list.add(i); |
| 1286 |
} |
| 1287 |
Flowable<Integer> source = Flowable.fromIterable(list); |
| 1288 |
source.flatMap(func).subscribe(ts); |
| 1289 |
|
| 1290 |
if (ts.values().size() != 1000) { |
| 1291 |
System.out.println(ts.values()); |
| 1292 |
} |
| 1293 |
|
| 1294 |
ts.assertTerminated(); |
| 1295 |
ts.assertNoErrors(); |
| 1296 |
ts.assertValueSequence(list); |
| 1297 |
} |
| 1298 |
|
| 1299 |
@Test |
| 1300 |
public void fastMergeFullScalar() { |
| 1301 |
runMerge(toScalar, new TestSubscriberEx<>()); |
| 1302 |
} |
| 1303 |
|
| 1304 |
@Test |
| 1305 |
public void fastMergeHiddenScalar() { |
| 1306 |
runMerge(toHiddenScalar, new TestSubscriberEx<>()); |
| 1307 |
} |
| 1308 |
|
| 1309 |
@Test |
| 1310 |
public void slowMergeFullScalar() { |
| 1311 |
for (final int req : new int[] { 16, 32, 64, 128, 256 }) { |
| 1312 |
TestSubscriberEx<Integer> ts = new TestSubscriberEx<Integer>(req) { |
| 1313 |
int remaining = req; |
| 1314 |
|
| 1315 |
@Override |
| 1316 |
public void onNext(Integer t) { |
| 1317 |
super.onNext(t); |
| 1318 |
if (--remaining == 0) { |
| 1319 |
remaining = req; |
| 1320 |
request(req); |
| 1321 |
} |
| 1322 |
} |
| 1323 |
}; |
| 1324 |
runMerge(toScalar, ts); |
| 1325 |
} |
| 1326 |
} |
| 1327 |
|
| 1328 |
@Test |
| 1329 |
public void slowMergeHiddenScalar() { |
| 1330 |
for (final int req : new int[] { 16, 32, 64, 128, 256 }) { |
| 1331 |
TestSubscriberEx<Integer> ts = new TestSubscriberEx<Integer>(req) { |
| 1332 |
int remaining = req; |
| 1333 |
@Override |
| 1334 |
public void onNext(Integer t) { |
| 1335 |
super.onNext(t); |
| 1336 |
if (--remaining == 0) { |
| 1337 |
remaining = req; |
| 1338 |
request(req); |
| 1339 |
} |
| 1340 |
} |
| 1341 |
}; |
| 1342 |
runMerge(toHiddenScalar, ts); |
| 1343 |
} |
| 1344 |
} |
| 1345 |
|
| 1346 |
@Test |
| 1347 |
public void negativeMaxConcurrent() { |
| 1348 |
try { |
| 1349 |
Flowable.merge(Arrays.asList(Flowable.just(1), Flowable.just(2)), -1); |
| 1350 |
fail("Expected IllegalArgumentException"); |
| 1351 |
} catch (IllegalArgumentException e) { |
| 1352 |
assertEquals("maxConcurrency > 0 required but it was -1", e.getMessage()); |
| 1353 |
} |
| 1354 |
} |
| 1355 |
|
| 1356 |
@Test |
| 1357 |
public void zeroMaxConcurrent() { |
| 1358 |
try { |
| 1359 |
Flowable.merge(Arrays.asList(Flowable.just(1), Flowable.just(2)), 0); |
| 1360 |
fail("Expected IllegalArgumentException"); |
| 1361 |
} catch (IllegalArgumentException e) { |
| 1362 |
assertEquals("maxConcurrency > 0 required but it was 0", e.getMessage()); |
| 1363 |
} |
| 1364 |
} |
| 1365 |
|
| 1366 |
@Test |
| 1367 |
public void mergeConcurrentJustJust() { |
| 1368 |
TestSubscriber<Integer> ts = TestSubscriber.create(); |
| 1369 |
|
| 1370 |
Flowable.merge(Flowable.just(Flowable.just(1)), 5).subscribe(ts); |
| 1371 |
|
| 1372 |
ts.assertValue(1); |
| 1373 |
ts.assertNoErrors(); |
| 1374 |
ts.assertComplete(); |
| 1375 |
} |
| 1376 |
|
| 1377 |
@Test |
| 1378 |
public void mergeConcurrentJustRange() { |
| 1379 |
TestSubscriber<Integer> ts = TestSubscriber.create(); |
| 1380 |
|
| 1381 |
Flowable.merge(Flowable.just(Flowable.range(1, 5)), 5).subscribe(ts); |
| 1382 |
|
| 1383 |
ts.assertValues(1, 2, 3, 4, 5); |
| 1384 |
ts.assertNoErrors(); |
| 1385 |
ts.assertComplete(); |
| 1386 |
} |
| 1387 |
|
| 1388 |
@SuppressWarnings("unchecked") |
| 1389 |
@Test |
| 1390 |
public void mergeArrayMaxConcurrent() { |
| 1391 |
TestSubscriber<Integer> ts = TestSubscriber.create(); |
| 1392 |
|
| 1393 |
PublishProcessor<Integer> pp1 = PublishProcessor.create(); |
| 1394 |
PublishProcessor<Integer> pp2 = PublishProcessor.create(); |
| 1395 |
|
| 1396 |
Flowable.mergeArray(1, 128, new Flowable[] { pp1, pp2 }).subscribe(ts); |
| 1397 |
|
| 1398 |
assertTrue("ps1 has no subscribers?!", pp1.hasSubscribers()); |
| 1399 |
assertFalse("ps2 has subscribers?!", pp2.hasSubscribers()); |
| 1400 |
|
| 1401 |
pp1.onNext(1); |
| 1402 |
pp1.onComplete(); |
| 1403 |
|
| 1404 |
assertFalse("ps1 has subscribers?!", pp1.hasSubscribers()); |
| 1405 |
assertTrue("ps2 has no subscribers?!", pp2.hasSubscribers()); |
| 1406 |
|
| 1407 |
pp2.onNext(2); |
| 1408 |
pp2.onComplete(); |
| 1409 |
|
| 1410 |
ts.assertValues(1, 2); |
| 1411 |
ts.assertNoErrors(); |
| 1412 |
ts.assertComplete(); |
| 1413 |
} |
| 1414 |
|
| 1415 |
@SuppressWarnings({ "unchecked", "rawtypes" }) |
| 1416 |
@Test |
| 1417 |
public void flatMapJustJust() { |
| 1418 |
TestSubscriber<Integer> ts = TestSubscriber.create(); |
| 1419 |
|
| 1420 |
Flowable.just(Flowable.just(1)).flatMap((Function)Functions.identity()).subscribe(ts); |
| 1421 |
|
| 1422 |
ts.assertValue(1); |
| 1423 |
ts.assertNoErrors(); |
| 1424 |
ts.assertComplete(); |
| 1425 |
} |
| 1426 |
|
| 1427 |
@SuppressWarnings({ "unchecked", "rawtypes" }) |
| 1428 |
@Test |
| 1429 |
public void flatMapJustRange() { |
| 1430 |
TestSubscriber<Integer> ts = TestSubscriber.create(); |
| 1431 |
|
| 1432 |
Flowable.just(Flowable.range(1, 5)).flatMap((Function)Functions.identity()).subscribe(ts); |
| 1433 |
|
| 1434 |
ts.assertValues(1, 2, 3, 4, 5); |
| 1435 |
ts.assertNoErrors(); |
| 1436 |
ts.assertComplete(); |
| 1437 |
} |
| 1438 |
|
| 1439 |
@SuppressWarnings({ "unchecked", "rawtypes" }) |
| 1440 |
@Test |
| 1441 |
public void flatMapMaxConcurrentJustJust() { |
| 1442 |
TestSubscriber<Integer> ts = TestSubscriber.create(); |
| 1443 |
|
| 1444 |
Flowable.just(Flowable.just(1)).flatMap((Function)Functions.identity(), 5).subscribe(ts); |
| 1445 |
|
| 1446 |
ts.assertValue(1); |
| 1447 |
ts.assertNoErrors(); |
| 1448 |
ts.assertComplete(); |
| 1449 |
} |
| 1450 |
|
| 1451 |
@SuppressWarnings({ "unchecked", "rawtypes" }) |
| 1452 |
@Test |
| 1453 |
public void flatMapMaxConcurrentJustRange() { |
| 1454 |
TestSubscriber<Integer> ts = TestSubscriber.create(); |
| 1455 |
|
| 1456 |
Flowable.just(Flowable.range(1, 5)).flatMap((Function)Functions.identity(), 5).subscribe(ts); |
| 1457 |
|
| 1458 |
ts.assertValues(1, 2, 3, 4, 5); |
| 1459 |
ts.assertNoErrors(); |
| 1460 |
ts.assertComplete(); |
| 1461 |
} |
| 1462 |
|
| 1463 |
@Test |
| 1464 |
public void noInnerReordering() { |
| 1465 |
TestSubscriber<Integer> ts = TestSubscriber.create(0); |
| 1466 |
FlowableFlatMap.MergeSubscriber<Publisher<Integer>, Integer> ms = |
| 1467 |
new FlowableFlatMap.MergeSubscriber<>(ts, Functions.<Publisher<Integer>>identity(), false, 128, 128); |
| 1468 |
ms.onSubscribe(new BooleanSubscription()); |
| 1469 |
|
| 1470 |
PublishProcessor<Integer> pp = PublishProcessor.create(); |
| 1471 |
|
| 1472 |
ms.onNext(pp); |
| 1473 |
|
| 1474 |
pp.onNext(1); |
| 1475 |
|
| 1476 |
BackpressureHelper.add(ms.requested, 2); |
| 1477 |
|
| 1478 |
pp.onNext(2); |
| 1479 |
|
| 1480 |
ms.drain(); |
| 1481 |
|
| 1482 |
ts.assertValues(1, 2); |
| 1483 |
} |
| 1484 |
|
| 1485 |
@Test |
| 1486 |
public void noOuterScalarReordering() { |
| 1487 |
TestSubscriber<Integer> ts = TestSubscriber.create(0); |
| 1488 |
FlowableFlatMap.MergeSubscriber<Publisher<Integer>, Integer> ms = |
| 1489 |
new FlowableFlatMap.MergeSubscriber<>(ts, Functions.<Publisher<Integer>>identity(), false, 128, 128); |
| 1490 |
ms.onSubscribe(new BooleanSubscription()); |
| 1491 |
|
| 1492 |
ms.onNext(Flowable.just(1)); |
| 1493 |
|
| 1494 |
BackpressureHelper.add(ms.requested, 2); |
| 1495 |
|
| 1496 |
ms.onNext(Flowable.just(2)); |
| 1497 |
|
| 1498 |
ms.drain(); |
| 1499 |
|
| 1500 |
ts.assertValues(1, 2); |
| 1501 |
} |
| 1502 |
|
| 1503 |
@Test |
| 1504 |
public void array() { |
| 1505 |
for (int i = 1; i < 100; i++) { |
| 1506 |
|
| 1507 |
@SuppressWarnings("unchecked") |
| 1508 |
Flowable<Integer>[] sources = new Flowable[i]; |
| 1509 |
Arrays.fill(sources, Flowable.just(1)); |
| 1510 |
Integer[] expected = new Integer[i]; |
| 1511 |
for (int j = 0; j < i; j++) { |
| 1512 |
expected[j] = 1; |
| 1513 |
} |
| 1514 |
|
| 1515 |
Flowable.mergeArray(sources) |
| 1516 |
.test() |
| 1517 |
.assertResult(expected); |
| 1518 |
} |
| 1519 |
} |
| 1520 |
|
| 1521 |
@Test |
| 1522 |
public void mergeArray2() { |
| 1523 |
Flowable.mergeArray(Flowable.just(1), Flowable.just(2)) |
| 1524 |
.test() |
| 1525 |
.assertResult(1, 2); |
| 1526 |
} |
| 1527 |
|
| 1528 |
@Test |
| 1529 |
public void mergeErrors() { |
| 1530 |
List<Throwable> errors = TestHelper.trackPluginErrors(); |
| 1531 |
try { |
| 1532 |
Flowable<Integer> source1 = Flowable.error(new TestException("First")); |
| 1533 |
Flowable<Integer> source2 = Flowable.error(new TestException("Second")); |
| 1534 |
|
| 1535 |
Flowable.merge(source1, source2) |
| 1536 |
.to(TestHelper.<Integer>testConsumer()) |
| 1537 |
.assertFailureAndMessage(TestException.class, "First"); |
| 1538 |
|
| 1539 |
assertTrue(errors.toString(), errors.isEmpty()); |
| 1540 |
} finally { |
| 1541 |
RxJavaPlugins.reset(); |
| 1542 |
} |
| 1543 |
} |
| 1544 |
} |