| 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.junit.Assert.*; |
| 17 |
import static org.mockito.ArgumentMatchers.any; |
| 18 |
import static org.mockito.Mockito.*; |
| 19 |
|
| 20 |
import java.util.*; |
| 21 |
import java.util.concurrent.*; |
| 22 |
|
| 23 |
import org.junit.*; |
| 24 |
import org.reactivestreams.*; |
| 25 |
|
| 26 |
import io.reactivex.rxjava3.core.*; |
| 27 |
import io.reactivex.rxjava3.exceptions.*; |
| 28 |
import io.reactivex.rxjava3.functions.LongConsumer; |
| 29 |
import io.reactivex.rxjava3.internal.subscriptions.BooleanSubscription; |
| 30 |
import io.reactivex.rxjava3.processors.PublishProcessor; |
| 31 |
import io.reactivex.rxjava3.subscribers.*; |
| 32 |
import io.reactivex.rxjava3.testsupport.*; |
| 33 |
|
| 34 |
public class FlowableMergeDelayErrorTest extends RxJavaTest { |
| 35 |
|
| 36 |
Subscriber<String> stringSubscriber; |
| 37 |
|
| 38 |
@Before |
| 39 |
public void before() { |
| 40 |
stringSubscriber = TestHelper.mockSubscriber(); |
| 41 |
} |
| 42 |
|
| 43 |
@Test |
| 44 |
public void errorDelayed1() { |
| 45 |
final Flowable<String> f1 = Flowable.unsafeCreate(new TestErrorFlowable("four", null, "six")); // we expect to lose "six" from the source (and it should never be sent by the source since onError was called |
| 46 |
final Flowable<String> f2 = Flowable.unsafeCreate(new TestErrorFlowable("one", "two", "three")); |
| 47 |
|
| 48 |
Flowable<String> m = Flowable.mergeDelayError(f1, f2); |
| 49 |
m.subscribe(stringSubscriber); |
| 50 |
|
| 51 |
verify(stringSubscriber, times(1)).onError(any(NullPointerException.class)); |
| 52 |
verify(stringSubscriber, never()).onComplete(); |
| 53 |
verify(stringSubscriber, times(1)).onNext("one"); |
| 54 |
verify(stringSubscriber, times(1)).onNext("two"); |
| 55 |
verify(stringSubscriber, times(1)).onNext("three"); |
| 56 |
verify(stringSubscriber, times(1)).onNext("four"); |
| 57 |
verify(stringSubscriber, times(0)).onNext("five"); |
| 58 |
// despite not expecting it ... we don't do anything to prevent it if the source Flowable keeps sending after onError |
| 59 |
// inner Flowable errors are considered terminal for that source |
| 60 |
// verify(stringSubscriber, times(1)).onNext("six"); |
| 61 |
// inner Flowable errors are considered terminal for that source |
| 62 |
} |
| 63 |
|
| 64 |
@Test |
| 65 |
public void errorDelayed2() { |
| 66 |
final Flowable<String> f1 = Flowable.unsafeCreate(new TestErrorFlowable("one", "two", "three")); |
| 67 |
final Flowable<String> f2 = Flowable.unsafeCreate(new TestErrorFlowable("four", null, "six")); // we expect to lose "six" from the source (and it should never be sent by the source since onError was called |
| 68 |
final Flowable<String> f3 = Flowable.unsafeCreate(new TestErrorFlowable("seven", "eight", null)); |
| 69 |
final Flowable<String> f4 = Flowable.unsafeCreate(new TestErrorFlowable("nine")); |
| 70 |
|
| 71 |
Flowable<String> m = Flowable.mergeDelayError(f1, f2, f3, f4); |
| 72 |
m.subscribe(stringSubscriber); |
| 73 |
|
| 74 |
verify(stringSubscriber, times(1)).onError(any(CompositeException.class)); |
| 75 |
verify(stringSubscriber, never()).onComplete(); |
| 76 |
verify(stringSubscriber, times(1)).onNext("one"); |
| 77 |
verify(stringSubscriber, times(1)).onNext("two"); |
| 78 |
verify(stringSubscriber, times(1)).onNext("three"); |
| 79 |
verify(stringSubscriber, times(1)).onNext("four"); |
| 80 |
verify(stringSubscriber, times(0)).onNext("five"); |
| 81 |
// despite not expecting it ... we don't do anything to prevent it if the source Flowable keeps sending after onError |
| 82 |
// inner Flowable errors are considered terminal for that source |
| 83 |
// verify(stringSubscriber, times(1)).onNext("six"); |
| 84 |
verify(stringSubscriber, times(1)).onNext("seven"); |
| 85 |
verify(stringSubscriber, times(1)).onNext("eight"); |
| 86 |
verify(stringSubscriber, times(1)).onNext("nine"); |
| 87 |
} |
| 88 |
|
| 89 |
@Test |
| 90 |
public void errorDelayed3() { |
| 91 |
final Flowable<String> f1 = Flowable.unsafeCreate(new TestErrorFlowable("one", "two", "three")); |
| 92 |
final Flowable<String> f2 = Flowable.unsafeCreate(new TestErrorFlowable("four", "five", "six")); |
| 93 |
final Flowable<String> f3 = Flowable.unsafeCreate(new TestErrorFlowable("seven", "eight", null)); |
| 94 |
final Flowable<String> f4 = Flowable.unsafeCreate(new TestErrorFlowable("nine")); |
| 95 |
|
| 96 |
Flowable<String> m = Flowable.mergeDelayError(f1, f2, f3, f4); |
| 97 |
m.subscribe(stringSubscriber); |
| 98 |
|
| 99 |
verify(stringSubscriber, times(1)).onError(any(NullPointerException.class)); |
| 100 |
verify(stringSubscriber, never()).onComplete(); |
| 101 |
verify(stringSubscriber, times(1)).onNext("one"); |
| 102 |
verify(stringSubscriber, times(1)).onNext("two"); |
| 103 |
verify(stringSubscriber, times(1)).onNext("three"); |
| 104 |
verify(stringSubscriber, times(1)).onNext("four"); |
| 105 |
verify(stringSubscriber, times(1)).onNext("five"); |
| 106 |
verify(stringSubscriber, times(1)).onNext("six"); |
| 107 |
verify(stringSubscriber, times(1)).onNext("seven"); |
| 108 |
verify(stringSubscriber, times(1)).onNext("eight"); |
| 109 |
verify(stringSubscriber, times(1)).onNext("nine"); |
| 110 |
} |
| 111 |
|
| 112 |
@Test |
| 113 |
public void errorDelayed4() { |
| 114 |
final Flowable<String> f1 = Flowable.unsafeCreate(new TestErrorFlowable("one", "two", "three")); |
| 115 |
final Flowable<String> f2 = Flowable.unsafeCreate(new TestErrorFlowable("four", "five", "six")); |
| 116 |
final Flowable<String> f3 = Flowable.unsafeCreate(new TestErrorFlowable("seven", "eight")); |
| 117 |
final Flowable<String> f4 = Flowable.unsafeCreate(new TestErrorFlowable("nine", null)); |
| 118 |
|
| 119 |
Flowable<String> m = Flowable.mergeDelayError(f1, f2, f3, f4); |
| 120 |
m.subscribe(stringSubscriber); |
| 121 |
|
| 122 |
verify(stringSubscriber, times(1)).onError(any(NullPointerException.class)); |
| 123 |
verify(stringSubscriber, never()).onComplete(); |
| 124 |
verify(stringSubscriber, times(1)).onNext("one"); |
| 125 |
verify(stringSubscriber, times(1)).onNext("two"); |
| 126 |
verify(stringSubscriber, times(1)).onNext("three"); |
| 127 |
verify(stringSubscriber, times(1)).onNext("four"); |
| 128 |
verify(stringSubscriber, times(1)).onNext("five"); |
| 129 |
verify(stringSubscriber, times(1)).onNext("six"); |
| 130 |
verify(stringSubscriber, times(1)).onNext("seven"); |
| 131 |
verify(stringSubscriber, times(1)).onNext("eight"); |
| 132 |
verify(stringSubscriber, times(1)).onNext("nine"); |
| 133 |
} |
| 134 |
|
| 135 |
@Test |
| 136 |
public void errorDelayed4WithThreading() { |
| 137 |
final TestAsyncErrorFlowable f1 = new TestAsyncErrorFlowable("one", "two", "three"); |
| 138 |
final TestAsyncErrorFlowable f2 = new TestAsyncErrorFlowable("four", "five", "six"); |
| 139 |
final TestAsyncErrorFlowable f3 = new TestAsyncErrorFlowable("seven", "eight"); |
| 140 |
// throw the error at the very end so no onComplete will be called after it |
| 141 |
final TestAsyncErrorFlowable f4 = new TestAsyncErrorFlowable("nine", null); |
| 142 |
|
| 143 |
Flowable<String> m = Flowable.mergeDelayError(Flowable.unsafeCreate(f1), Flowable.unsafeCreate(f2), Flowable.unsafeCreate(f3), Flowable.unsafeCreate(f4)); |
| 144 |
m.subscribe(stringSubscriber); |
| 145 |
|
| 146 |
try { |
| 147 |
f1.t.join(); |
| 148 |
f2.t.join(); |
| 149 |
f3.t.join(); |
| 150 |
f4.t.join(); |
| 151 |
} catch (InterruptedException e) { |
| 152 |
throw new RuntimeException(e); |
| 153 |
} |
| 154 |
|
| 155 |
verify(stringSubscriber, times(1)).onNext("one"); |
| 156 |
verify(stringSubscriber, times(1)).onNext("two"); |
| 157 |
verify(stringSubscriber, times(1)).onNext("three"); |
| 158 |
verify(stringSubscriber, times(1)).onNext("four"); |
| 159 |
verify(stringSubscriber, times(1)).onNext("five"); |
| 160 |
verify(stringSubscriber, times(1)).onNext("six"); |
| 161 |
verify(stringSubscriber, times(1)).onNext("seven"); |
| 162 |
verify(stringSubscriber, times(1)).onNext("eight"); |
| 163 |
verify(stringSubscriber, times(1)).onNext("nine"); |
| 164 |
verify(stringSubscriber, times(1)).onError(any(NullPointerException.class)); |
| 165 |
verify(stringSubscriber, never()).onComplete(); |
| 166 |
} |
| 167 |
|
| 168 |
@Test |
| 169 |
public void compositeErrorDelayed1() { |
| 170 |
final Flowable<String> f1 = Flowable.unsafeCreate(new TestErrorFlowable("four", null, "six")); // we expect to lose "six" from the source (and it should never be sent by the source since onError was called |
| 171 |
final Flowable<String> f2 = Flowable.unsafeCreate(new TestErrorFlowable("one", "two", null)); |
| 172 |
|
| 173 |
Flowable<String> m = Flowable.mergeDelayError(f1, f2); |
| 174 |
m.subscribe(stringSubscriber); |
| 175 |
|
| 176 |
verify(stringSubscriber, times(1)).onError(any(Throwable.class)); |
| 177 |
verify(stringSubscriber, never()).onComplete(); |
| 178 |
verify(stringSubscriber, times(1)).onNext("one"); |
| 179 |
verify(stringSubscriber, times(1)).onNext("two"); |
| 180 |
verify(stringSubscriber, times(0)).onNext("three"); |
| 181 |
verify(stringSubscriber, times(1)).onNext("four"); |
| 182 |
verify(stringSubscriber, times(0)).onNext("five"); |
| 183 |
// despite not expecting it ... we don't do anything to prevent it if the source Flowable keeps sending after onError |
| 184 |
// inner Flowable errors are considered terminal for that source |
| 185 |
// verify(stringSubscriber, times(1)).onNext("six"); |
| 186 |
} |
| 187 |
|
| 188 |
@Test |
| 189 |
public void compositeErrorDelayed2() { |
| 190 |
final Flowable<String> f1 = Flowable.unsafeCreate(new TestErrorFlowable("four", null, "six")); // we expect to lose "six" from the source (and it should never be sent by the source since onError was called |
| 191 |
final Flowable<String> f2 = Flowable.unsafeCreate(new TestErrorFlowable("one", "two", null)); |
| 192 |
|
| 193 |
Flowable<String> m = Flowable.mergeDelayError(f1, f2); |
| 194 |
CaptureObserver w = new CaptureObserver(); |
| 195 |
m.subscribe(w); |
| 196 |
|
| 197 |
assertNotNull(w.e); |
| 198 |
|
| 199 |
int size = ((CompositeException)w.e).size(); |
| 200 |
if (size != 2) { |
| 201 |
w.e.printStackTrace(); |
| 202 |
} |
| 203 |
assertEquals(2, size); |
| 204 |
|
| 205 |
// if (w.e instanceof CompositeException) { |
| 206 |
// assertEquals(2, ((CompositeException) w.e).getExceptions().size()); |
| 207 |
// w.e.printStackTrace(); |
| 208 |
// } else { |
| 209 |
// fail("Expecting CompositeException"); |
| 210 |
// } |
| 211 |
|
| 212 |
} |
| 213 |
|
| 214 |
/** |
| 215 |
* The unit tests below are from OperationMerge and should ensure the normal merge functionality is correct. |
| 216 |
*/ |
| 217 |
|
| 218 |
@Test |
| 219 |
public void mergeFlowableOfFlowables() { |
| 220 |
final Flowable<String> f1 = Flowable.unsafeCreate(new TestSynchronousFlowable()); |
| 221 |
final Flowable<String> f2 = Flowable.unsafeCreate(new TestSynchronousFlowable()); |
| 222 |
|
| 223 |
Flowable<Flowable<String>> flowableOfFlowables = Flowable.unsafeCreate(new Publisher<Flowable<String>>() { |
| 224 |
|
| 225 |
@Override |
| 226 |
public void subscribe(Subscriber<? super Flowable<String>> subscriber) { |
| 227 |
subscriber.onSubscribe(new BooleanSubscription()); |
| 228 |
// simulate what would happen in a Flowable |
| 229 |
subscriber.onNext(f1); |
| 230 |
subscriber.onNext(f2); |
| 231 |
subscriber.onComplete(); |
| 232 |
} |
| 233 |
|
| 234 |
}); |
| 235 |
Flowable<String> m = Flowable.mergeDelayError(flowableOfFlowables); |
| 236 |
m.subscribe(stringSubscriber); |
| 237 |
|
| 238 |
verify(stringSubscriber, never()).onError(any(Throwable.class)); |
| 239 |
verify(stringSubscriber, times(1)).onComplete(); |
| 240 |
verify(stringSubscriber, times(2)).onNext("hello"); |
| 241 |
} |
| 242 |
|
| 243 |
@Test |
| 244 |
public void mergeArray() { |
| 245 |
final Flowable<String> f1 = Flowable.unsafeCreate(new TestSynchronousFlowable()); |
| 246 |
final Flowable<String> f2 = Flowable.unsafeCreate(new TestSynchronousFlowable()); |
| 247 |
|
| 248 |
Flowable<String> m = Flowable.mergeDelayError(f1, f2); |
| 249 |
m.subscribe(stringSubscriber); |
| 250 |
|
| 251 |
verify(stringSubscriber, never()).onError(any(Throwable.class)); |
| 252 |
verify(stringSubscriber, times(2)).onNext("hello"); |
| 253 |
verify(stringSubscriber, times(1)).onComplete(); |
| 254 |
} |
| 255 |
|
| 256 |
@Test |
| 257 |
public void mergeList() { |
| 258 |
final Flowable<String> f1 = Flowable.unsafeCreate(new TestSynchronousFlowable()); |
| 259 |
final Flowable<String> f2 = Flowable.unsafeCreate(new TestSynchronousFlowable()); |
| 260 |
List<Flowable<String>> listOfFlowables = new ArrayList<>(); |
| 261 |
listOfFlowables.add(f1); |
| 262 |
listOfFlowables.add(f2); |
| 263 |
|
| 264 |
Flowable<String> m = Flowable.mergeDelayError(Flowable.fromIterable(listOfFlowables)); |
| 265 |
m.subscribe(stringSubscriber); |
| 266 |
|
| 267 |
verify(stringSubscriber, never()).onError(any(Throwable.class)); |
| 268 |
verify(stringSubscriber, times(1)).onComplete(); |
| 269 |
verify(stringSubscriber, times(2)).onNext("hello"); |
| 270 |
} |
| 271 |
|
| 272 |
@Test |
| 273 |
public void mergeArrayWithThreading() { |
| 274 |
final TestASynchronousFlowable f1 = new TestASynchronousFlowable(); |
| 275 |
final TestASynchronousFlowable f2 = new TestASynchronousFlowable(); |
| 276 |
|
| 277 |
Flowable<String> m = Flowable.mergeDelayError(Flowable.unsafeCreate(f1), Flowable.unsafeCreate(f2)); |
| 278 |
m.subscribe(stringSubscriber); |
| 279 |
|
| 280 |
try { |
| 281 |
f1.t.join(); |
| 282 |
f2.t.join(); |
| 283 |
} catch (InterruptedException e) { |
| 284 |
throw new RuntimeException(e); |
| 285 |
} |
| 286 |
|
| 287 |
verify(stringSubscriber, never()).onError(any(Throwable.class)); |
| 288 |
verify(stringSubscriber, times(2)).onNext("hello"); |
| 289 |
verify(stringSubscriber, times(1)).onComplete(); |
| 290 |
} |
| 291 |
|
| 292 |
@Test |
| 293 |
public void synchronousError() { |
| 294 |
final Flowable<Flowable<String>> f1 = Flowable.error(new RuntimeException("unit test")); |
| 295 |
|
| 296 |
final CountDownLatch latch = new CountDownLatch(1); |
| 297 |
Flowable.mergeDelayError(f1).subscribe(new DefaultSubscriber<String>() { |
| 298 |
@Override |
| 299 |
public void onComplete() { |
| 300 |
fail("Expected onError path"); |
| 301 |
} |
| 302 |
|
| 303 |
@Override |
| 304 |
public void onError(Throwable e) { |
| 305 |
latch.countDown(); |
| 306 |
} |
| 307 |
|
| 308 |
@Override |
| 309 |
public void onNext(String s) { |
| 310 |
fail("Expected onError path"); |
| 311 |
} |
| 312 |
}); |
| 313 |
|
| 314 |
try { |
| 315 |
latch.await(); |
| 316 |
} catch (InterruptedException ex) { |
| 317 |
fail("interrupted"); |
| 318 |
} |
| 319 |
} |
| 320 |
|
| 321 |
private static class TestSynchronousFlowable implements Publisher<String> { |
| 322 |
|
| 323 |
@Override |
| 324 |
public void subscribe(Subscriber<? super String> subscriber) { |
| 325 |
subscriber.onSubscribe(new BooleanSubscription()); |
| 326 |
subscriber.onNext("hello"); |
| 327 |
subscriber.onComplete(); |
| 328 |
} |
| 329 |
} |
| 330 |
|
| 331 |
private static class TestASynchronousFlowable implements Publisher<String> { |
| 332 |
Thread t; |
| 333 |
|
| 334 |
@Override |
| 335 |
public void subscribe(final Subscriber<? super String> subscriber) { |
| 336 |
subscriber.onSubscribe(new BooleanSubscription()); |
| 337 |
t = new Thread(new Runnable() { |
| 338 |
|
| 339 |
@Override |
| 340 |
public void run() { |
| 341 |
subscriber.onNext("hello"); |
| 342 |
subscriber.onComplete(); |
| 343 |
} |
| 344 |
|
| 345 |
}); |
| 346 |
t.start(); |
| 347 |
} |
| 348 |
} |
| 349 |
|
| 350 |
private static class TestErrorFlowable implements Publisher<String> { |
| 351 |
|
| 352 |
String[] valuesToReturn; |
| 353 |
|
| 354 |
TestErrorFlowable(String... values) { |
| 355 |
valuesToReturn = values; |
| 356 |
} |
| 357 |
|
| 358 |
@Override |
| 359 |
public void subscribe(Subscriber<? super String> subscriber) { |
| 360 |
subscriber.onSubscribe(new BooleanSubscription()); |
| 361 |
boolean errorThrown = false; |
| 362 |
for (String s : valuesToReturn) { |
| 363 |
if (s == null) { |
| 364 |
System.out.println("throwing exception"); |
| 365 |
subscriber.onError(new NullPointerException()); |
| 366 |
errorThrown = true; |
| 367 |
// purposefully not returning here so it will continue calling onNext |
| 368 |
// so that we also test that we handle bad sequences like this |
| 369 |
} else { |
| 370 |
subscriber.onNext(s); |
| 371 |
} |
| 372 |
} |
| 373 |
if (!errorThrown) { |
| 374 |
subscriber.onComplete(); |
| 375 |
} |
| 376 |
} |
| 377 |
} |
| 378 |
|
| 379 |
private static class TestAsyncErrorFlowable implements Publisher<String> { |
| 380 |
|
| 381 |
String[] valuesToReturn; |
| 382 |
|
| 383 |
TestAsyncErrorFlowable(String... values) { |
| 384 |
valuesToReturn = values; |
| 385 |
} |
| 386 |
|
| 387 |
Thread t; |
| 388 |
|
| 389 |
@Override |
| 390 |
public void subscribe(final Subscriber<? super String> subscriber) { |
| 391 |
subscriber.onSubscribe(new BooleanSubscription()); |
| 392 |
t = new Thread(new Runnable() { |
| 393 |
|
| 394 |
@Override |
| 395 |
public void run() { |
| 396 |
for (String s : valuesToReturn) { |
| 397 |
if (s == null) { |
| 398 |
System.out.println("throwing exception"); |
| 399 |
try { |
| 400 |
Thread.sleep(100); |
| 401 |
} catch (Throwable e) { |
| 402 |
|
| 403 |
} |
| 404 |
subscriber.onError(new NullPointerException()); |
| 405 |
return; |
| 406 |
} else { |
| 407 |
subscriber.onNext(s); |
| 408 |
} |
| 409 |
} |
| 410 |
System.out.println("subscription complete"); |
| 411 |
subscriber.onComplete(); |
| 412 |
} |
| 413 |
|
| 414 |
}); |
| 415 |
t.start(); |
| 416 |
} |
| 417 |
} |
| 418 |
|
| 419 |
private static class CaptureObserver extends DefaultSubscriber<String> { |
| 420 |
volatile Throwable e; |
| 421 |
|
| 422 |
@Override |
| 423 |
public void onComplete() { |
| 424 |
|
| 425 |
} |
| 426 |
|
| 427 |
@Override |
| 428 |
public void onError(Throwable e) { |
| 429 |
this.e = e; |
| 430 |
} |
| 431 |
|
| 432 |
@Override |
| 433 |
public void onNext(String args) { |
| 434 |
|
| 435 |
} |
| 436 |
|
| 437 |
} |
| 438 |
|
| 439 |
@Test |
| 440 |
public void errorInParentFlowable() { |
| 441 |
TestSubscriberEx<Integer> ts = new TestSubscriberEx<>(); |
| 442 |
Flowable.mergeDelayError( |
| 443 |
Flowable.just(Flowable.just(1), Flowable.just(2)) |
| 444 |
.startWithItem(Flowable.<Integer> error(new RuntimeException())) |
| 445 |
).subscribe(ts); |
| 446 |
ts.awaitDone(5, TimeUnit.SECONDS); |
| 447 |
ts.assertTerminated(); |
| 448 |
ts.assertValues(1, 2); |
| 449 |
assertEquals(1, ts.errors().size()); |
| 450 |
|
| 451 |
} |
| 452 |
|
| 453 |
@Test |
| 454 |
public void errorInParentFlowableDelayed() throws Exception { |
| 455 |
for (int i = 0; i < 50; i++) { |
| 456 |
final TestASynchronous1sDelayedFlowable f1 = new TestASynchronous1sDelayedFlowable(); |
| 457 |
final TestASynchronous1sDelayedFlowable f2 = new TestASynchronous1sDelayedFlowable(); |
| 458 |
Flowable<Flowable<String>> parentFlowable = Flowable.unsafeCreate(new Publisher<Flowable<String>>() { |
| 459 |
@Override |
| 460 |
public void subscribe(Subscriber<? super Flowable<String>> op) { |
| 461 |
op.onSubscribe(new BooleanSubscription()); |
| 462 |
op.onNext(Flowable.unsafeCreate(f1)); |
| 463 |
op.onNext(Flowable.unsafeCreate(f2)); |
| 464 |
op.onError(new NullPointerException("throwing exception in parent")); |
| 465 |
} |
| 466 |
}); |
| 467 |
|
| 468 |
stringSubscriber = TestHelper.mockSubscriber(); |
| 469 |
|
| 470 |
TestSubscriberEx<String> ts = new TestSubscriberEx<>(stringSubscriber); |
| 471 |
Flowable<String> m = Flowable.mergeDelayError(parentFlowable); |
| 472 |
m.subscribe(ts); |
| 473 |
System.out.println("testErrorInParentFlowableDelayed | " + i); |
| 474 |
ts.awaitDone(2000, TimeUnit.MILLISECONDS); |
| 475 |
ts.assertTerminated(); |
| 476 |
|
| 477 |
verify(stringSubscriber, times(2)).onNext("hello"); |
| 478 |
verify(stringSubscriber, times(1)).onError(any(NullPointerException.class)); |
| 479 |
verify(stringSubscriber, never()).onComplete(); |
| 480 |
} |
| 481 |
} |
| 482 |
|
| 483 |
private static class TestASynchronous1sDelayedFlowable implements Publisher<String> { |
| 484 |
Thread t; |
| 485 |
|
| 486 |
@Override |
| 487 |
public void subscribe(final Subscriber<? super String> subscriber) { |
| 488 |
subscriber.onSubscribe(new BooleanSubscription()); |
| 489 |
t = new Thread(new Runnable() { |
| 490 |
|
| 491 |
@Override |
| 492 |
public void run() { |
| 493 |
try { |
| 494 |
Thread.sleep(100); |
| 495 |
} catch (InterruptedException e) { |
| 496 |
subscriber.onError(e); |
| 497 |
} |
| 498 |
subscriber.onNext("hello"); |
| 499 |
subscriber.onComplete(); |
| 500 |
} |
| 501 |
|
| 502 |
}); |
| 503 |
t.start(); |
| 504 |
} |
| 505 |
} |
| 506 |
|
| 507 |
@Test |
| 508 |
public void delayErrorMaxConcurrent() { |
| 509 |
final List<Long> requests = new ArrayList<>(); |
| 510 |
Flowable<Integer> source = Flowable.mergeDelayError(Flowable.just( |
| 511 |
Flowable.just(1).hide(), |
| 512 |
Flowable.<Integer>error(new TestException())) |
| 513 |
.doOnRequest(new LongConsumer() { |
| 514 |
@Override |
| 515 |
public void accept(long t1) { |
| 516 |
requests.add(t1); |
| 517 |
} |
| 518 |
}), 1); |
| 519 |
|
| 520 |
TestSubscriberEx<Integer> ts = new TestSubscriberEx<>(); |
| 521 |
|
| 522 |
source.subscribe(ts); |
| 523 |
|
| 524 |
ts.assertValue(1); |
| 525 |
ts.assertTerminated(); |
| 526 |
ts.assertError(TestException.class); |
| 527 |
assertEquals(Arrays.asList(1L, 1L, 1L), requests); |
| 528 |
} |
| 529 |
|
| 530 |
// This is pretty much a clone of testMergeList but with the overloaded MergeDelayError for Iterables |
| 531 |
@Test |
| 532 |
public void mergeIterable() { |
| 533 |
final Flowable<String> f1 = Flowable.unsafeCreate(new TestSynchronousFlowable()); |
| 534 |
final Flowable<String> f2 = Flowable.unsafeCreate(new TestSynchronousFlowable()); |
| 535 |
List<Flowable<String>> listOfFlowables = new ArrayList<>(); |
| 536 |
listOfFlowables.add(f1); |
| 537 |
listOfFlowables.add(f2); |
| 538 |
|
| 539 |
Flowable<String> m = Flowable.mergeDelayError(listOfFlowables); |
| 540 |
m.subscribe(stringSubscriber); |
| 541 |
|
| 542 |
verify(stringSubscriber, never()).onError(any(Throwable.class)); |
| 543 |
verify(stringSubscriber, times(1)).onComplete(); |
| 544 |
verify(stringSubscriber, times(2)).onNext("hello"); |
| 545 |
} |
| 546 |
|
| 547 |
@Test |
| 548 |
public void iterableMaxConcurrent() { |
| 549 |
TestSubscriber<Integer> ts = TestSubscriber.create(); |
| 550 |
|
| 551 |
PublishProcessor<Integer> pp1 = PublishProcessor.create(); |
| 552 |
PublishProcessor<Integer> pp2 = PublishProcessor.create(); |
| 553 |
|
| 554 |
Flowable.mergeDelayError(Arrays.asList(pp1, pp2), 1).subscribe(ts); |
| 555 |
|
| 556 |
assertTrue("ps1 has no subscribers?!", pp1.hasSubscribers()); |
| 557 |
assertFalse("ps2 has subscribers?!", pp2.hasSubscribers()); |
| 558 |
|
| 559 |
pp1.onNext(1); |
| 560 |
pp1.onComplete(); |
| 561 |
|
| 562 |
assertFalse("ps1 has subscribers?!", pp1.hasSubscribers()); |
| 563 |
assertTrue("ps2 has no subscribers?!", pp2.hasSubscribers()); |
| 564 |
|
| 565 |
pp2.onNext(2); |
| 566 |
pp2.onComplete(); |
| 567 |
|
| 568 |
ts.assertValues(1, 2); |
| 569 |
ts.assertNoErrors(); |
| 570 |
ts.assertComplete(); |
| 571 |
} |
| 572 |
|
| 573 |
@Test |
| 574 |
public void iterableMaxConcurrentError() { |
| 575 |
TestSubscriberEx<Integer> ts = new TestSubscriberEx<>(); |
| 576 |
|
| 577 |
PublishProcessor<Integer> pp1 = PublishProcessor.create(); |
| 578 |
PublishProcessor<Integer> pp2 = PublishProcessor.create(); |
| 579 |
|
| 580 |
Flowable.mergeDelayError(Arrays.asList(pp1, pp2), 1).subscribe(ts); |
| 581 |
|
| 582 |
assertTrue("ps1 has no subscribers?!", pp1.hasSubscribers()); |
| 583 |
assertFalse("ps2 has subscribers?!", pp2.hasSubscribers()); |
| 584 |
|
| 585 |
pp1.onNext(1); |
| 586 |
pp1.onError(new TestException()); |
| 587 |
|
| 588 |
assertFalse("ps1 has subscribers?!", pp1.hasSubscribers()); |
| 589 |
assertTrue("ps2 has no subscribers?!", pp2.hasSubscribers()); |
| 590 |
|
| 591 |
pp2.onNext(2); |
| 592 |
pp2.onError(new TestException()); |
| 593 |
|
| 594 |
ts.assertValues(1, 2); |
| 595 |
ts.assertError(CompositeException.class); |
| 596 |
ts.assertNotComplete(); |
| 597 |
|
| 598 |
CompositeException ce = (CompositeException)ts.errors().get(0); |
| 599 |
|
| 600 |
assertEquals(2, ce.getExceptions().size()); |
| 601 |
} |
| 602 |
|
| 603 |
static <T> Flowable<T> withError(Flowable<T> source) { |
| 604 |
return source.concatWith(Flowable.<T>error(new TestException())); |
| 605 |
} |
| 606 |
|
| 607 |
@Test |
| 608 |
public void array() { |
| 609 |
for (int i = 1; i < 100; i++) { |
| 610 |
|
| 611 |
@SuppressWarnings("unchecked") |
| 612 |
Flowable<Integer>[] sources = new Flowable[i]; |
| 613 |
Arrays.fill(sources, Flowable.just(1)); |
| 614 |
Integer[] expected = new Integer[i]; |
| 615 |
for (int j = 0; j < i; j++) { |
| 616 |
expected[j] = 1; |
| 617 |
} |
| 618 |
|
| 619 |
Flowable.mergeArrayDelayError(sources) |
| 620 |
.test() |
| 621 |
.assertResult(expected); |
| 622 |
} |
| 623 |
} |
| 624 |
|
| 625 |
@Test |
| 626 |
public void mergeArrayDelayError() { |
| 627 |
Flowable.mergeArrayDelayError(Flowable.just(1), Flowable.just(2)) |
| 628 |
.test() |
| 629 |
.assertResult(1, 2); |
| 630 |
} |
| 631 |
|
| 632 |
@Test |
| 633 |
public void mergeIterableDelayErrorWithError() { |
| 634 |
Flowable.mergeDelayError( |
| 635 |
Arrays.asList(Flowable.just(1).concatWith(Flowable.<Integer>error(new TestException())), |
| 636 |
Flowable.just(2))) |
| 637 |
.test() |
| 638 |
.assertFailure(TestException.class, 1, 2); |
| 639 |
} |
| 640 |
|
| 641 |
@Test |
| 642 |
public void mergeDelayError() { |
| 643 |
Flowable.mergeDelayError( |
| 644 |
Flowable.just(Flowable.just(1), |
| 645 |
Flowable.just(2))) |
| 646 |
.test() |
| 647 |
.assertResult(1, 2); |
| 648 |
} |
| 649 |
|
| 650 |
@Test |
| 651 |
public void mergeDelayErrorWithError() { |
| 652 |
Flowable.mergeDelayError( |
| 653 |
Flowable.just(Flowable.just(1).concatWith(Flowable.<Integer>error(new TestException())), |
| 654 |
Flowable.just(2))) |
| 655 |
.test() |
| 656 |
.assertFailure(TestException.class, 1, 2); |
| 657 |
} |
| 658 |
|
| 659 |
@Test |
| 660 |
public void mergeDelayErrorMaxConcurrency() { |
| 661 |
Flowable.mergeDelayError( |
| 662 |
Flowable.just(Flowable.just(1), |
| 663 |
Flowable.just(2)), 1) |
| 664 |
.test() |
| 665 |
.assertResult(1, 2); |
| 666 |
} |
| 667 |
|
| 668 |
@Test |
| 669 |
public void mergeDelayErrorWithErrorMaxConcurrency() { |
| 670 |
Flowable.mergeDelayError( |
| 671 |
Flowable.just(Flowable.just(1).concatWith(Flowable.<Integer>error(new TestException())), |
| 672 |
Flowable.just(2)), 1) |
| 673 |
.test() |
| 674 |
.assertFailure(TestException.class, 1, 2); |
| 675 |
} |
| 676 |
|
| 677 |
@Test |
| 678 |
public void mergeIterableDelayErrorMaxConcurrency() { |
| 679 |
Flowable.mergeDelayError( |
| 680 |
Arrays.asList(Flowable.just(1), |
| 681 |
Flowable.just(2)), 1) |
| 682 |
.test() |
| 683 |
.assertResult(1, 2); |
| 684 |
} |
| 685 |
|
| 686 |
@Test |
| 687 |
public void mergeIterableDelayErrorWithErrorMaxConcurrency() { |
| 688 |
Flowable.mergeDelayError( |
| 689 |
Arrays.asList(Flowable.just(1).concatWith(Flowable.<Integer>error(new TestException())), |
| 690 |
Flowable.just(2)), 1) |
| 691 |
.test() |
| 692 |
.assertFailure(TestException.class, 1, 2); |
| 693 |
} |
| 694 |
|
| 695 |
@Test |
| 696 |
public void mergeDelayError3() { |
| 697 |
Flowable.mergeDelayError( |
| 698 |
Flowable.just(1), |
| 699 |
Flowable.just(2), |
| 700 |
Flowable.just(3) |
| 701 |
) |
| 702 |
.test() |
| 703 |
.assertResult(1, 2, 3); |
| 704 |
} |
| 705 |
|
| 706 |
@Test |
| 707 |
public void mergeDelayError3WithError() { |
| 708 |
Flowable.mergeDelayError( |
| 709 |
Flowable.just(1), |
| 710 |
Flowable.just(2).concatWith(Flowable.<Integer>error(new TestException())), |
| 711 |
Flowable.just(3) |
| 712 |
) |
| 713 |
.test() |
| 714 |
.assertFailure(TestException.class, 1, 2, 3); |
| 715 |
} |
| 716 |
|
| 717 |
@Test |
| 718 |
public void mergeIterableDelayError() { |
| 719 |
Flowable.mergeDelayError(Arrays.asList(Flowable.just(1), Flowable.just(2))) |
| 720 |
.test() |
| 721 |
.assertResult(1, 2); |
| 722 |
} |
| 723 |
} |