|
29 | 29 | import org.mockito.stubbing.Answer;
|
30 | 30 |
|
31 | 31 | import rx.Observable;
|
| 32 | +import rx.Observable.OnSubscribeFunc; |
32 | 33 | import rx.Observer;
|
33 | 34 | import rx.Scheduler;
|
| 35 | +import rx.Subscription; |
| 36 | +import rx.schedulers.ImmediateScheduler; |
34 | 37 | import rx.schedulers.Schedulers;
|
35 | 38 | import rx.schedulers.TestScheduler;
|
| 39 | +import rx.schedulers.TrampolineScheduler; |
| 40 | +import rx.subscriptions.BooleanSubscription; |
36 | 41 | import rx.util.functions.Action0;
|
37 | 42 | import rx.util.functions.Action1;
|
38 | 43 | import rx.util.functions.Func1;
|
@@ -305,6 +310,98 @@ public void call(Integer t1) {
|
305 | 310 | });
|
306 | 311 | }
|
307 | 312 |
|
| 313 | + @Test |
| 314 | + public final void testBackpressureOnFastProducerSlowConsumerWithUnsubscribeNewThread() throws InterruptedException { |
| 315 | + testBackpressureOnFastProducerSlowConsumerWithUnsubscribe(Schedulers.newThread()); |
| 316 | + } |
| 317 | + |
| 318 | + @Test |
| 319 | + public final void testBackpressureOnFastProducerSlowConsumerWithUnsubscribeIO() throws InterruptedException { |
| 320 | + testBackpressureOnFastProducerSlowConsumerWithUnsubscribe(Schedulers.io()); |
| 321 | + } |
| 322 | + |
| 323 | + @Test |
| 324 | + public final void testBackpressureOnFastProducerSlowConsumerWithUnsubscribeTrampoline() throws InterruptedException { |
| 325 | + testBackpressureOnFastProducerSlowConsumerWithUnsubscribe(Schedulers.trampoline()); |
| 326 | + } |
| 327 | + |
| 328 | + @Test |
| 329 | + public final void testBackpressureOnFastProducerSlowConsumerWithUnsubscribeTestScheduler() throws InterruptedException { |
| 330 | + testBackpressureOnFastProducerSlowConsumerWithUnsubscribe(Schedulers.test()); |
| 331 | + } |
| 332 | + |
| 333 | + @Test |
| 334 | + public final void testBackpressureOnFastProducerSlowConsumerWithUnsubscribeComputation() throws InterruptedException { |
| 335 | + testBackpressureOnFastProducerSlowConsumerWithUnsubscribe(Schedulers.computation()); |
| 336 | + } |
| 337 | + |
| 338 | + private final void testBackpressureOnFastProducerSlowConsumerWithUnsubscribe(Scheduler scheduler) throws InterruptedException { |
| 339 | + final AtomicInteger countEmitted = new AtomicInteger(); |
| 340 | + final AtomicInteger countTaken = new AtomicInteger(); |
| 341 | + int value = Observable.create(new OnSubscribeFunc<Integer>() { |
| 342 | + |
| 343 | + @Override |
| 344 | + public Subscription onSubscribe(final Observer<? super Integer> o) { |
| 345 | + final BooleanSubscription s = BooleanSubscription.create(); |
| 346 | + Thread t = new Thread(new Runnable() { |
| 347 | + |
| 348 | + @Override |
| 349 | + public void run() { |
| 350 | + int i = 1; |
| 351 | + while (!s.isUnsubscribed() && i <= 100) { |
| 352 | + System.out.println("onNext from fast producer [" + Thread.currentThread() + "]: " + i); |
| 353 | + o.onNext(i++); |
| 354 | + } |
| 355 | + o.onCompleted(); |
| 356 | + } |
| 357 | + }); |
| 358 | + t.setDaemon(true); |
| 359 | + t.start(); |
| 360 | + return s; |
| 361 | + } |
| 362 | + }).doOnNext(new Action1<Integer>() { |
| 363 | + |
| 364 | + @Override |
| 365 | + public void call(Integer i) { |
| 366 | + countEmitted.incrementAndGet(); |
| 367 | + } |
| 368 | + }).doOnCompleted(new Action0() { |
| 369 | + |
| 370 | + @Override |
| 371 | + public void call() { |
| 372 | + System.out.println("-------- Done Emitting from Source ---------"); |
| 373 | + } |
| 374 | + }).observeOn(scheduler).doOnNext(new Action1<Integer>() { |
| 375 | + |
| 376 | + @Override |
| 377 | + public void call(Integer i) { |
| 378 | + System.out.println(">> onNext to slowConsumer [" + Thread.currentThread() + "] pre-take: " + i); |
| 379 | + //force it to be slower than the producer |
| 380 | + try { |
| 381 | + Thread.sleep(10); |
| 382 | + } catch (InterruptedException e) { |
| 383 | + e.printStackTrace(); |
| 384 | + } |
| 385 | + countTaken.incrementAndGet(); |
| 386 | + } |
| 387 | + }).take(10).toBlockingObservable().last(); |
| 388 | + |
| 389 | + if (scheduler instanceof TrampolineScheduler || scheduler instanceof ImmediateScheduler || scheduler instanceof TestScheduler) { |
| 390 | + // since there is no concurrency it will block and only emit as many as it can process |
| 391 | + assertEquals(10, countEmitted.get()); |
| 392 | + } else { |
| 393 | + // the others with concurrency should not emit all 100 ... but 10 + 2 in the pipeline |
| 394 | + // NOTE: The +2 could change if the implementation of the queue logic changes. See Javadoc at top of class. |
| 395 | + assertEquals(12, countEmitted.get()); |
| 396 | + } |
| 397 | + // number received after take (but take will filter any extra) |
| 398 | + assertEquals(10, value); |
| 399 | + // so we also want to check the doOnNext after observeOn to see if it got unsubscribed |
| 400 | + Thread.sleep(200); // let time pass to see if the scheduler is still doing work |
| 401 | + // we expect only 10 to make it through the observeOn side |
| 402 | + assertEquals(10, countTaken.get()); |
| 403 | + } |
| 404 | + |
308 | 405 | private static int randomIntFrom0to100() {
|
309 | 406 | // XORShift instead of Math.random http://javamex.com/tutorials/random_numbers/xorshift.shtml
|
310 | 407 | long x = System.nanoTime();
|
|
0 commit comments