| 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.flowable; |
| 15 |
|
| 16 |
import static org.junit.Assert.*; |
| 17 |
|
| 18 |
import java.util.*; |
| 19 |
import java.util.concurrent.atomic.*; |
| 20 |
|
| 21 |
import org.junit.Test; |
| 22 |
import org.reactivestreams.*; |
| 23 |
|
| 24 |
import io.reactivex.rxjava3.core.*; |
| 25 |
import io.reactivex.rxjava3.disposables.Disposable; |
| 26 |
import io.reactivex.rxjava3.exceptions.*; |
| 27 |
import io.reactivex.rxjava3.functions.*; |
| 28 |
import io.reactivex.rxjava3.internal.functions.Functions; |
| 29 |
import io.reactivex.rxjava3.internal.subscribers.ForEachWhileSubscriber; |
| 30 |
import io.reactivex.rxjava3.internal.subscriptions.BooleanSubscription; |
| 31 |
import io.reactivex.rxjava3.plugins.RxJavaPlugins; |
| 32 |
import io.reactivex.rxjava3.processors.PublishProcessor; |
| 33 |
import io.reactivex.rxjava3.subscribers.*; |
| 34 |
import io.reactivex.rxjava3.testsupport.TestHelper; |
| 35 |
|
| 36 |
public class FlowableSubscriberTest { |
| 37 |
|
| 38 |
/** |
| 39 |
* Should request n for whatever the final Subscriber asks for. |
| 40 |
*/ |
| 41 |
@Test |
| 42 |
public void requestFromFinalSubscribeWithRequestValue() { |
| 43 |
TestSubscriber<String> s = new TestSubscriber<>(0L); |
| 44 |
s.request(10); |
| 45 |
final AtomicLong r = new AtomicLong(); |
| 46 |
s.onSubscribe(new Subscription() { |
| 47 |
|
| 48 |
@Override |
| 49 |
public void request(long n) { |
| 50 |
r.set(n); |
| 51 |
} |
| 52 |
|
| 53 |
@Override |
| 54 |
public void cancel() { |
| 55 |
|
| 56 |
} |
| 57 |
|
| 58 |
}); |
| 59 |
assertEquals(10, r.get()); |
| 60 |
} |
| 61 |
|
| 62 |
/** |
| 63 |
* Should request -1 for infinite. |
| 64 |
*/ |
| 65 |
@Test |
| 66 |
public void requestFromFinalSubscribeWithoutRequestValue() { |
| 67 |
TestSubscriber<String> s = new TestSubscriber<>(); |
| 68 |
final AtomicLong r = new AtomicLong(); |
| 69 |
s.onSubscribe(new Subscription() { |
| 70 |
|
| 71 |
@Override |
| 72 |
public void request(long n) { |
| 73 |
r.set(n); |
| 74 |
} |
| 75 |
|
| 76 |
@Override |
| 77 |
public void cancel() { |
| 78 |
|
| 79 |
} |
| 80 |
|
| 81 |
}); |
| 82 |
assertEquals(Long.MAX_VALUE, r.get()); |
| 83 |
} |
| 84 |
|
| 85 |
@Test |
| 86 |
public void requestFromChainedOperator() throws Throwable { |
| 87 |
TestSubscriber<String> s = new TestSubscriber<>(10L); |
| 88 |
FlowableOperator<String, String> o = new FlowableOperator<String, String>() { |
| 89 |
@Override |
| 90 |
public Subscriber<? super String> apply(final Subscriber<? super String> s1) { |
| 91 |
return new FlowableSubscriber<String>() { |
| 92 |
|
| 93 |
@Override |
| 94 |
public void onSubscribe(Subscription a) { |
| 95 |
s1.onSubscribe(a); |
| 96 |
} |
| 97 |
|
| 98 |
@Override |
| 99 |
public void onComplete() { |
| 100 |
|
| 101 |
} |
| 102 |
|
| 103 |
@Override |
| 104 |
public void onError(Throwable e) { |
| 105 |
|
| 106 |
} |
| 107 |
|
| 108 |
@Override |
| 109 |
public void onNext(String t) { |
| 110 |
|
| 111 |
} |
| 112 |
|
| 113 |
}; |
| 114 |
} |
| 115 |
}; |
| 116 |
Subscriber<? super String> ns = o.apply(s); |
| 117 |
|
| 118 |
final AtomicLong r = new AtomicLong(); |
| 119 |
// set set the producer at the top of the chain (ns) and it should flow through the operator to the (s) subscriber |
| 120 |
// and then it should request up with the value set on the final Subscriber (s) |
| 121 |
ns.onSubscribe(new Subscription() { |
| 122 |
|
| 123 |
@Override |
| 124 |
public void request(long n) { |
| 125 |
r.set(n); |
| 126 |
} |
| 127 |
|
| 128 |
@Override |
| 129 |
public void cancel() { |
| 130 |
|
| 131 |
} |
| 132 |
|
| 133 |
}); |
| 134 |
assertEquals(10, r.get()); |
| 135 |
} |
| 136 |
|
| 137 |
@Test |
| 138 |
public void requestFromDecoupledOperator() throws Throwable { |
| 139 |
TestSubscriber<String> s = new TestSubscriber<>(0L); |
| 140 |
FlowableOperator<String, String> o = new FlowableOperator<String, String>() { |
| 141 |
@Override |
| 142 |
public Subscriber<? super String> apply(final Subscriber<? super String> s1) { |
| 143 |
return new FlowableSubscriber<String>() { |
| 144 |
|
| 145 |
@Override |
| 146 |
public void onSubscribe(Subscription a) { |
| 147 |
s1.onSubscribe(a); |
| 148 |
} |
| 149 |
|
| 150 |
@Override |
| 151 |
public void onComplete() { |
| 152 |
|
| 153 |
} |
| 154 |
|
| 155 |
@Override |
| 156 |
public void onError(Throwable e) { |
| 157 |
|
| 158 |
} |
| 159 |
|
| 160 |
@Override |
| 161 |
public void onNext(String t) { |
| 162 |
|
| 163 |
} |
| 164 |
|
| 165 |
}; |
| 166 |
} |
| 167 |
}; |
| 168 |
s.request(10); |
| 169 |
Subscriber<? super String> ns = o.apply(s); |
| 170 |
|
| 171 |
final AtomicLong r = new AtomicLong(); |
| 172 |
// set set the producer at the top of the chain (ns) and it should flow through the operator to the (s) subscriber |
| 173 |
// and then it should request up with the value set on the final Subscriber (s) |
| 174 |
ns.onSubscribe(new Subscription() { |
| 175 |
|
| 176 |
@Override |
| 177 |
public void request(long n) { |
| 178 |
r.set(n); |
| 179 |
} |
| 180 |
|
| 181 |
@Override |
| 182 |
public void cancel() { |
| 183 |
|
| 184 |
} |
| 185 |
|
| 186 |
}); |
| 187 |
assertEquals(10, r.get()); |
| 188 |
} |
| 189 |
|
| 190 |
@Test |
| 191 |
public void requestFromDecoupledOperatorThatRequestsN() throws Throwable { |
| 192 |
TestSubscriber<String> s = new TestSubscriber<>(10L); |
| 193 |
final AtomicLong innerR = new AtomicLong(); |
| 194 |
FlowableOperator<String, String> o = new FlowableOperator<String, String>() { |
| 195 |
@Override |
| 196 |
public Subscriber<? super String> apply(Subscriber<? super String> child) { |
| 197 |
// we want to decouple the chain so set our own Producer on the child instead of it coming from the parent |
| 198 |
child.onSubscribe(new Subscription() { |
| 199 |
|
| 200 |
@Override |
| 201 |
public void request(long n) { |
| 202 |
innerR.set(n); |
| 203 |
} |
| 204 |
|
| 205 |
@Override |
| 206 |
public void cancel() { |
| 207 |
|
| 208 |
} |
| 209 |
|
| 210 |
}); |
| 211 |
|
| 212 |
ResourceSubscriber<String> as = new ResourceSubscriber<String>() { |
| 213 |
|
| 214 |
@Override |
| 215 |
protected void onStart() { |
| 216 |
// we request 99 up to the parent |
| 217 |
request(99); |
| 218 |
} |
| 219 |
|
| 220 |
@Override |
| 221 |
public void onComplete() { |
| 222 |
|
| 223 |
} |
| 224 |
|
| 225 |
@Override |
| 226 |
public void onError(Throwable e) { |
| 227 |
|
| 228 |
} |
| 229 |
|
| 230 |
@Override |
| 231 |
public void onNext(String t) { |
| 232 |
|
| 233 |
} |
| 234 |
}; |
| 235 |
|
| 236 |
return as; |
| 237 |
} |
| 238 |
}; |
| 239 |
Subscriber<? super String> ns = o.apply(s); |
| 240 |
|
| 241 |
final AtomicLong r = new AtomicLong(); |
| 242 |
// set set the producer at the top of the chain (ns) and it should flow through the operator to the (s) subscriber |
| 243 |
// and then it should request up with the value set on the final Subscriber (s) |
| 244 |
ns.onSubscribe(new Subscription() { |
| 245 |
|
| 246 |
@Override |
| 247 |
public void request(long n) { |
| 248 |
r.set(n); |
| 249 |
} |
| 250 |
|
| 251 |
@Override |
| 252 |
public void cancel() { |
| 253 |
|
| 254 |
} |
| 255 |
|
| 256 |
}); |
| 257 |
assertEquals(99, r.get()); |
| 258 |
assertEquals(10, innerR.get()); |
| 259 |
} |
| 260 |
|
| 261 |
@Test |
| 262 |
public void requestToFlowable() { |
| 263 |
TestSubscriber<Integer> ts = new TestSubscriber<>(3L); |
| 264 |
final AtomicLong requested = new AtomicLong(); |
| 265 |
Flowable.<Integer>unsafeCreate(new Publisher<Integer>() { |
| 266 |
@Override |
| 267 |
public void subscribe(Subscriber<? super Integer> s) { |
| 268 |
s.onSubscribe(new Subscription() { |
| 269 |
|
| 270 |
@Override |
| 271 |
public void request(long n) { |
| 272 |
requested.set(n); |
| 273 |
} |
| 274 |
|
| 275 |
@Override |
| 276 |
public void cancel() { |
| 277 |
|
| 278 |
} |
| 279 |
}); |
| 280 |
} |
| 281 |
}).subscribe(ts); |
| 282 |
assertEquals(3, requested.get()); |
| 283 |
} |
| 284 |
|
| 285 |
@Test |
| 286 |
public void requestThroughMap() { |
| 287 |
TestSubscriber<Integer> ts = new TestSubscriber<>(0L); |
| 288 |
ts.request(3); |
| 289 |
final AtomicLong requested = new AtomicLong(); |
| 290 |
Flowable.<Integer>unsafeCreate(new Publisher<Integer>() { |
| 291 |
@Override |
| 292 |
public void subscribe(Subscriber<? super Integer> s) { |
| 293 |
s.onSubscribe(new Subscription() { |
| 294 |
|
| 295 |
@Override |
| 296 |
public void request(long n) { |
| 297 |
requested.set(n); |
| 298 |
} |
| 299 |
|
| 300 |
@Override |
| 301 |
public void cancel() { |
| 302 |
|
| 303 |
} |
| 304 |
}); |
| 305 |
} |
| 306 |
}).map(Functions.<Integer>identity()).subscribe(ts); |
| 307 |
assertEquals(3, requested.get()); |
| 308 |
} |
| 309 |
|
| 310 |
@Test |
| 311 |
public void requestThroughTakeThatReducesRequest() { |
| 312 |
TestSubscriber<Integer> ts = new TestSubscriber<>(0L); |
| 313 |
ts.request(3); |
| 314 |
final AtomicLong requested = new AtomicLong(); |
| 315 |
Flowable.<Integer>unsafeCreate(new Publisher<Integer>() { |
| 316 |
@Override |
| 317 |
public void subscribe(Subscriber<? super Integer> s) { |
| 318 |
s.onSubscribe(new Subscription() { |
| 319 |
|
| 320 |
@Override |
| 321 |
public void request(long n) { |
| 322 |
requested.set(n); |
| 323 |
} |
| 324 |
|
| 325 |
@Override |
| 326 |
public void cancel() { |
| 327 |
|
| 328 |
} |
| 329 |
|
| 330 |
}); |
| 331 |
} |
| 332 |
}).take(2).subscribe(ts); |
| 333 |
|
| 334 |
assertEquals(2, requested.get()); |
| 335 |
} |
| 336 |
|
| 337 |
@Test |
| 338 |
public void requestThroughTakeWhereRequestIsSmallerThanTake() { |
| 339 |
TestSubscriber<Integer> ts = new TestSubscriber<>(0L); |
| 340 |
ts.request(3); |
| 341 |
final AtomicLong requested = new AtomicLong(); |
| 342 |
Flowable.<Integer>unsafeCreate(new Publisher<Integer>() { |
| 343 |
@Override |
| 344 |
public void subscribe(Subscriber<? super Integer> s) { |
| 345 |
s.onSubscribe(new Subscription() { |
| 346 |
|
| 347 |
@Override |
| 348 |
public void request(long n) { |
| 349 |
requested.set(n); |
| 350 |
} |
| 351 |
|
| 352 |
@Override |
| 353 |
public void cancel() { |
| 354 |
|
| 355 |
} |
| 356 |
|
| 357 |
}); |
| 358 |
} |
| 359 |
}).take(10).subscribe(ts); |
| 360 |
assertEquals(3, requested.get()); |
| 361 |
} |
| 362 |
|
| 363 |
@Test |
| 364 |
public void onStartCalledOnceViaSubscribe() { |
| 365 |
final AtomicInteger c = new AtomicInteger(); |
| 366 |
Flowable.just(1, 2, 3, 4).take(2).subscribe(new DefaultSubscriber<Integer>() { |
| 367 |
|
| 368 |
@Override |
| 369 |
public void onStart() { |
| 370 |
c.incrementAndGet(); |
| 371 |
request(1); |
| 372 |
} |
| 373 |
|
| 374 |
@Override |
| 375 |
public void onComplete() { |
| 376 |
|
| 377 |
} |
| 378 |
|
| 379 |
@Override |
| 380 |
public void onError(Throwable e) { |
| 381 |
|
| 382 |
} |
| 383 |
|
| 384 |
@Override |
| 385 |
public void onNext(Integer t) { |
| 386 |
request(1); |
| 387 |
} |
| 388 |
|
| 389 |
}); |
| 390 |
|
| 391 |
assertEquals(1, c.get()); |
| 392 |
} |
| 393 |
|
| 394 |
@Test |
| 395 |
public void onStartCalledOnceViaUnsafeSubscribe() { |
| 396 |
final AtomicInteger c = new AtomicInteger(); |
| 397 |
Flowable.just(1, 2, 3, 4).take(2).subscribe(new DefaultSubscriber<Integer>() { |
| 398 |
|
| 399 |
@Override |
| 400 |
public void onStart() { |
| 401 |
c.incrementAndGet(); |
| 402 |
request(1); |
| 403 |
} |
| 404 |
|
| 405 |
@Override |
| 406 |
public void onComplete() { |
| 407 |
|
| 408 |
} |
| 409 |
|
| 410 |
@Override |
| 411 |
public void onError(Throwable e) { |
| 412 |
|
| 413 |
} |
| 414 |
|
| 415 |
@Override |
| 416 |
public void onNext(Integer t) { |
| 417 |
request(1); |
| 418 |
} |
| 419 |
|
| 420 |
}); |
| 421 |
|
| 422 |
assertEquals(1, c.get()); |
| 423 |
} |
| 424 |
|
| 425 |
@Test |
| 426 |
public void onStartCalledOnceViaLift() { |
| 427 |
final AtomicInteger c = new AtomicInteger(); |
| 428 |
Flowable.just(1, 2, 3, 4).lift(new FlowableOperator<Integer, Integer>() { |
| 429 |
|
| 430 |
@Override |
| 431 |
public Subscriber<? super Integer> apply(final Subscriber<? super Integer> child) { |
| 432 |
return new DefaultSubscriber<Integer>() { |
| 433 |
|
| 434 |
@Override |
| 435 |
public void onStart() { |
| 436 |
c.incrementAndGet(); |
| 437 |
request(1); |
| 438 |
} |
| 439 |
|
| 440 |
@Override |
| 441 |
public void onComplete() { |
| 442 |
child.onComplete(); |
| 443 |
} |
| 444 |
|
| 445 |
@Override |
| 446 |
public void onError(Throwable e) { |
| 447 |
child.onError(e); |
| 448 |
} |
| 449 |
|
| 450 |
@Override |
| 451 |
public void onNext(Integer t) { |
| 452 |
child.onNext(t); |
| 453 |
request(1); |
| 454 |
} |
| 455 |
|
| 456 |
}; |
| 457 |
} |
| 458 |
|
| 459 |
}).subscribe(); |
| 460 |
|
| 461 |
assertEquals(1, c.get()); |
| 462 |
} |
| 463 |
|
| 464 |
@Test |
| 465 |
public void onStartRequestsAreAdditive() { |
| 466 |
final List<Integer> list = new ArrayList<>(); |
| 467 |
Flowable.just(1, 2, 3, 4, 5) |
| 468 |
.subscribe(new DefaultSubscriber<Integer>() { |
| 469 |
@Override |
| 470 |
public void onStart() { |
| 471 |
request(3); |
| 472 |
request(2); |
| 473 |
} |
| 474 |
|
| 475 |
@Override |
| 476 |
public void onComplete() { |
| 477 |
|
| 478 |
} |
| 479 |
|
| 480 |
@Override |
| 481 |
public void onError(Throwable e) { |
| 482 |
|
| 483 |
} |
| 484 |
|
| 485 |
@Override |
| 486 |
public void onNext(Integer t) { |
| 487 |
list.add(t); |
| 488 |
}}); |
| 489 |
assertEquals(Arrays.asList(1, 2, 3, 4, 5), list); |
| 490 |
} |
| 491 |
|
| 492 |
@Test |
| 493 |
public void onStartRequestsAreAdditiveAndOverflowBecomesMaxValue() { |
| 494 |
final List<Integer> list = new ArrayList<>(); |
| 495 |
Flowable.just(1, 2, 3, 4, 5).subscribe(new DefaultSubscriber<Integer>() { |
| 496 |
@Override |
| 497 |
public void onStart() { |
| 498 |
request(2); |
| 499 |
request(Long.MAX_VALUE - 1); |
| 500 |
} |
| 501 |
|
| 502 |
@Override |
| 503 |
public void onComplete() { |
| 504 |
|
| 505 |
} |
| 506 |
|
| 507 |
@Override |
| 508 |
public void onError(Throwable e) { |
| 509 |
|
| 510 |
} |
| 511 |
|
| 512 |
@Override |
| 513 |
public void onNext(Integer t) { |
| 514 |
list.add(t); |
| 515 |
}}); |
| 516 |
assertEquals(Arrays.asList(1, 2, 3, 4, 5), list); |
| 517 |
} |
| 518 |
|
| 519 |
@Test |
| 520 |
public void forEachWhile() { |
| 521 |
PublishProcessor<Integer> pp = PublishProcessor.create(); |
| 522 |
|
| 523 |
final List<Integer> list = new ArrayList<>(); |
| 524 |
|
| 525 |
Disposable d = pp.forEachWhile(new Predicate<Integer>() { |
| 526 |
@Override |
| 527 |
public boolean test(Integer v) throws Exception { |
| 528 |
list.add(v); |
| 529 |
return v < 3; |
| 530 |
} |
| 531 |
}); |
| 532 |
|
| 533 |
assertFalse(d.isDisposed()); |
| 534 |
|
| 535 |
pp.onNext(1); |
| 536 |
pp.onNext(2); |
| 537 |
pp.onNext(3); |
| 538 |
|
| 539 |
assertFalse(pp.hasSubscribers()); |
| 540 |
|
| 541 |
assertEquals(Arrays.asList(1, 2, 3), list); |
| 542 |
} |
| 543 |
|
| 544 |
@Test |
| 545 |
public void doubleSubscribe() { |
| 546 |
ForEachWhileSubscriber<Integer> s = new ForEachWhileSubscriber<>(new Predicate<Integer>() { |
| 547 |
@Override |
| 548 |
public boolean test(Integer v) throws Exception { |
| 549 |
return true; |
| 550 |
} |
| 551 |
}, Functions.<Throwable>emptyConsumer(), Functions.EMPTY_ACTION); |
| 552 |
|
| 553 |
List<Throwable> list = TestHelper.trackPluginErrors(); |
| 554 |
|
| 555 |
try { |
| 556 |
s.onSubscribe(new BooleanSubscription()); |
| 557 |
|
| 558 |
BooleanSubscription bs = new BooleanSubscription(); |
| 559 |
s.onSubscribe(bs); |
| 560 |
|
| 561 |
assertTrue(bs.isCancelled()); |
| 562 |
|
| 563 |
TestHelper.assertError(list, 0, IllegalStateException.class, "Subscription already set!"); |
| 564 |
} finally { |
| 565 |
RxJavaPlugins.reset(); |
| 566 |
} |
| 567 |
} |
| 568 |
|
| 569 |
@Test |
| 570 |
public void suppressAfterCompleteEvents() { |
| 571 |
List<Throwable> errors = TestHelper.trackPluginErrors(); |
| 572 |
try { |
| 573 |
final TestSubscriber<Integer> ts = new TestSubscriber<>(); |
| 574 |
ts.onSubscribe(new BooleanSubscription()); |
| 575 |
|
| 576 |
ForEachWhileSubscriber<Integer> s = new ForEachWhileSubscriber<>(new Predicate<Integer>() { |
| 577 |
@Override |
| 578 |
public boolean test(Integer v) throws Exception { |
| 579 |
ts.onNext(v); |
| 580 |
return true; |
| 581 |
} |
| 582 |
}, new Consumer<Throwable>() { |
| 583 |
@Override |
| 584 |
public void accept(Throwable e) throws Exception { |
| 585 |
ts.onError(e); |
| 586 |
} |
| 587 |
}, new Action() { |
| 588 |
@Override |
| 589 |
public void run() throws Exception { |
| 590 |
ts.onComplete(); |
| 591 |
} |
| 592 |
}); |
| 593 |
|
| 594 |
s.onComplete(); |
| 595 |
s.onNext(1); |
| 596 |
s.onError(new TestException()); |
| 597 |
s.onComplete(); |
| 598 |
|
| 599 |
ts.assertResult(); |
| 600 |
|
| 601 |
TestHelper.assertUndeliverable(errors, 0, TestException.class); |
| 602 |
} finally { |
| 603 |
RxJavaPlugins.reset(); |
| 604 |
} |
| 605 |
} |
| 606 |
|
| 607 |
@Test |
| 608 |
public void onNextCrashes() { |
| 609 |
final TestSubscriber<Integer> ts = new TestSubscriber<>(); |
| 610 |
ts.onSubscribe(new BooleanSubscription()); |
| 611 |
|
| 612 |
ForEachWhileSubscriber<Integer> s = new ForEachWhileSubscriber<>(new Predicate<Integer>() { |
| 613 |
@Override |
| 614 |
public boolean test(Integer v) throws Exception { |
| 615 |
throw new TestException(); |
| 616 |
} |
| 617 |
}, new Consumer<Throwable>() { |
| 618 |
@Override |
| 619 |
public void accept(Throwable e) throws Exception { |
| 620 |
ts.onError(e); |
| 621 |
} |
| 622 |
}, new Action() { |
| 623 |
@Override |
| 624 |
public void run() throws Exception { |
| 625 |
ts.onComplete(); |
| 626 |
} |
| 627 |
}); |
| 628 |
|
| 629 |
BooleanSubscription b = new BooleanSubscription(); |
| 630 |
|
| 631 |
s.onSubscribe(b); |
| 632 |
s.onNext(1); |
| 633 |
|
| 634 |
assertTrue(b.isCancelled()); |
| 635 |
ts.assertFailure(TestException.class); |
| 636 |
} |
| 637 |
|
| 638 |
@Test |
| 639 |
public void onErrorThrows() { |
| 640 |
ForEachWhileSubscriber<Integer> s = new ForEachWhileSubscriber<>(new Predicate<Integer>() { |
| 641 |
@Override |
| 642 |
public boolean test(Integer v) throws Exception { |
| 643 |
return true; |
| 644 |
} |
| 645 |
}, new Consumer<Throwable>() { |
| 646 |
@Override |
| 647 |
public void accept(Throwable e) throws Exception { |
| 648 |
throw new TestException("Inner"); |
| 649 |
} |
| 650 |
}, new Action() { |
| 651 |
@Override |
| 652 |
public void run() throws Exception { |
| 653 |
|
| 654 |
} |
| 655 |
}); |
| 656 |
|
| 657 |
List<Throwable> list = TestHelper.trackPluginErrors(); |
| 658 |
|
| 659 |
try { |
| 660 |
s.onSubscribe(new BooleanSubscription()); |
| 661 |
|
| 662 |
s.onError(new TestException("Outer")); |
| 663 |
|
| 664 |
TestHelper.assertError(list, 0, CompositeException.class); |
| 665 |
List<Throwable> cel = TestHelper.compositeList(list.get(0)); |
| 666 |
TestHelper.assertError(cel, 0, TestException.class, "Outer"); |
| 667 |
TestHelper.assertError(cel, 1, TestException.class, "Inner"); |
| 668 |
} finally { |
| 669 |
RxJavaPlugins.reset(); |
| 670 |
} |
| 671 |
} |
| 672 |
|
| 673 |
@Test |
| 674 |
public void onCompleteThrows() { |
| 675 |
ForEachWhileSubscriber<Integer> s = new ForEachWhileSubscriber<>(new Predicate<Integer>() { |
| 676 |
@Override |
| 677 |
public boolean test(Integer v) throws Exception { |
| 678 |
return true; |
| 679 |
} |
| 680 |
}, new Consumer<Throwable>() { |
| 681 |
@Override |
| 682 |
public void accept(Throwable e) throws Exception { |
| 683 |
} |
| 684 |
}, new Action() { |
| 685 |
@Override |
| 686 |
public void run() throws Exception { |
| 687 |
throw new TestException("Inner"); |
| 688 |
} |
| 689 |
}); |
| 690 |
|
| 691 |
List<Throwable> list = TestHelper.trackPluginErrors(); |
| 692 |
|
| 693 |
try { |
| 694 |
s.onSubscribe(new BooleanSubscription()); |
| 695 |
|
| 696 |
s.onComplete(); |
| 697 |
|
| 698 |
TestHelper.assertUndeliverable(list, 0, TestException.class, "Inner"); |
| 699 |
} finally { |
| 700 |
RxJavaPlugins.reset(); |
| 701 |
} |
| 702 |
} |
| 703 |
|
| 704 |
@Test |
| 705 |
public void subscribeConsumerConsumerWithError() { |
| 706 |
final List<Integer> list = new ArrayList<>(); |
| 707 |
|
| 708 |
Flowable.<Integer>error(new TestException()).subscribe(new Consumer<Integer>() { |
| 709 |
@Override |
| 710 |
public void accept(Integer v) throws Exception { |
| 711 |
list.add(v); |
| 712 |
} |
| 713 |
}, new Consumer<Throwable>() { |
| 714 |
@Override |
| 715 |
public void accept(Throwable e) throws Exception { |
| 716 |
list.add(100); |
| 717 |
} |
| 718 |
}); |
| 719 |
|
| 720 |
assertEquals(Arrays.asList(100), list); |
| 721 |
} |
| 722 |
|
| 723 |
@Test |
| 724 |
public void methodTestCancelled() { |
| 725 |
PublishProcessor<Integer> pp = PublishProcessor.create(); |
| 726 |
|
| 727 |
pp.test(Long.MAX_VALUE, true); |
| 728 |
|
| 729 |
assertFalse(pp.hasSubscribers()); |
| 730 |
} |
| 731 |
|
| 732 |
@Test |
| 733 |
public void safeSubscriberAlreadySafe() { |
| 734 |
TestSubscriber<Integer> ts = new TestSubscriber<>(); |
| 735 |
Flowable.just(1).safeSubscribe(new SafeSubscriber<>(ts)); |
| 736 |
|
| 737 |
ts.assertResult(1); |
| 738 |
} |
| 739 |
|
| 740 |
@Test |
| 741 |
public void methodTestNoCancel() { |
| 742 |
PublishProcessor<Integer> pp = PublishProcessor.create(); |
| 743 |
|
| 744 |
pp.test(Long.MAX_VALUE, false); |
| 745 |
|
| 746 |
assertTrue(pp.hasSubscribers()); |
| 747 |
} |
| 748 |
|
| 749 |
@Test |
| 750 |
public void subscribeConsumerConsumer() { |
| 751 |
final List<Integer> list = new ArrayList<>(); |
| 752 |
|
| 753 |
Flowable.just(1).subscribe(new Consumer<Integer>() { |
| 754 |
@Override |
| 755 |
public void accept(Integer v) throws Exception { |
| 756 |
list.add(v); |
| 757 |
} |
| 758 |
}, new Consumer<Throwable>() { |
| 759 |
@Override |
| 760 |
public void accept(Throwable e) throws Exception { |
| 761 |
list.add(100); |
| 762 |
} |
| 763 |
}); |
| 764 |
|
| 765 |
assertEquals(Arrays.asList(1), list); |
| 766 |
} |
| 767 |
|
| 768 |
@SuppressWarnings("rawtypes") |
| 769 |
@Test |
| 770 |
public void pluginNull() { |
| 771 |
RxJavaPlugins.setOnFlowableSubscribe(new BiFunction<Flowable, Subscriber, Subscriber>() { |
| 772 |
@Override |
| 773 |
public Subscriber apply(Flowable a, Subscriber b) throws Exception { |
| 774 |
return null; |
| 775 |
} |
| 776 |
}); |
| 777 |
|
| 778 |
try { |
| 779 |
try { |
| 780 |
|
| 781 |
Flowable.just(1).test(); |
| 782 |
fail("Should have thrown"); |
| 783 |
} catch (NullPointerException ex) { |
| 784 |
assertEquals("The RxJavaPlugins.onSubscribe hook returned a null FlowableSubscriber. Please check the handler provided to RxJavaPlugins.setOnFlowableSubscribe for invalid null returns. Further reading: https://github.com/ReactiveX/RxJava/wiki/Plugins", ex.getMessage()); |
| 785 |
} |
| 786 |
} finally { |
| 787 |
RxJavaPlugins.reset(); |
| 788 |
} |
| 789 |
} |
| 790 |
|
| 791 |
static final class BadFlowable extends Flowable<Integer> { |
| 792 |
@Override |
| 793 |
protected void subscribeActual(Subscriber<? super Integer> s) { |
| 794 |
throw new IllegalArgumentException(); |
| 795 |
} |
| 796 |
} |
| 797 |
|
| 798 |
@Test |
| 799 |
public void subscribeActualThrows() { |
| 800 |
List<Throwable> list = TestHelper.trackPluginErrors(); |
| 801 |
try { |
| 802 |
try { |
| 803 |
new BadFlowable().test(); |
| 804 |
fail("Should have thrown!"); |
| 805 |
} catch (NullPointerException ex) { |
| 806 |
if (!(ex.getCause() instanceof IllegalArgumentException)) { |
| 807 |
fail(ex.toString() + ": Should be NPE(IAE)"); |
| 808 |
} |
| 809 |
} |
| 810 |
|
| 811 |
TestHelper.assertError(list, 0, IllegalArgumentException.class); |
| 812 |
} finally { |
| 813 |
RxJavaPlugins.reset(); |
| 814 |
} |
| 815 |
} |
| 816 |
} |