2323
2424import org .junit .*;
2525
26+ import org .junit .rules .TestName ;
2627import rx .Observable .OnSubscribe ;
2728import rx .exceptions .MissingBackpressureException ;
2829import rx .functions .*;
3334
3435public class BackpressureTests {
3536
37+ @ Rule
38+ public TestName testName = new TestName ();
39+
3640 @ After
3741 public void doAfterTest () {
3842 TestObstructionDetection .checkObstruction ();
@@ -424,18 +428,56 @@ public void testOnBackpressureDrop() {
424428 .map (SLOW_PASS_THRU ).take (NUM ).subscribe (ts );
425429 ts .awaitTerminalEvent ();
426430 ts .assertNoErrors ();
427-
428-
431+
429432 List <Integer > onNextEvents = ts .getOnNextEvents ();
430433 assertEquals (NUM , onNextEvents .size ());
431434
432435 Integer lastEvent = onNextEvents .get (NUM - 1 );
433-
436+
434437 System .out .println ("testOnBackpressureDrop => Received: " + onNextEvents .size () + " Emitted: " + c .get () + " Last value: " + lastEvent );
435438 // it drop, so we should get some number far higher than what would have sequentially incremented
436439 assertTrue (NUM - 1 <= lastEvent .intValue ());
437440 }
438441 }
442+
443+ @ Test (timeout = 10000 )
444+ public void testOnBackpressureDropWithAction () {
445+ for (int i = 0 ; i < 100 ; i ++) {
446+ final AtomicInteger emitCount = new AtomicInteger ();
447+ final AtomicInteger dropCount = new AtomicInteger ();
448+ final AtomicInteger passCount = new AtomicInteger ();
449+ final int NUM = (int ) (RxRingBuffer .SIZE * 1.5 ); // > 1 so that take doesn't prevent buffer overflow
450+ TestSubscriber <Integer > ts = new TestSubscriber <Integer >();
451+ firehose (emitCount ).onBackpressureDrop (new Action1 <Integer >() {
452+ @ Override
453+ public void call (Integer i ) {
454+ dropCount .incrementAndGet ();
455+ }
456+ })
457+ .doOnNext (new Action1 <Integer >() {
458+ @ Override
459+ public void call (Integer integer ) {
460+ passCount .incrementAndGet ();
461+ }
462+ })
463+ .observeOn (Schedulers .computation ())
464+ .map (SLOW_PASS_THRU ).take (NUM ).subscribe (ts );
465+ ts .awaitTerminalEvent ();
466+ ts .assertNoErrors ();
467+
468+ List <Integer > onNextEvents = ts .getOnNextEvents ();
469+ Integer lastEvent = onNextEvents .get (NUM - 1 );
470+ System .out .println (testName .getMethodName () + " => Received: " + onNextEvents .size () + " Passed: " + passCount .get () + " Dropped: " + dropCount .get () + " Emitted: " + emitCount .get () + " Last value: " + lastEvent );
471+ assertEquals (NUM , onNextEvents .size ());
472+ // in reality, NUM < passCount
473+ assertTrue (NUM <= passCount .get ());
474+ // it drop, so we should get some number far higher than what would have sequentially incremented
475+ assertTrue (NUM - 1 <= lastEvent .intValue ());
476+ assertTrue (0 < dropCount .get ());
477+ assertEquals (emitCount .get (), passCount .get () + dropCount .get ());
478+ }
479+ }
480+
439481 @ Test (timeout = 10000 )
440482 public void testOnBackpressureDropSynchronous () {
441483 for (int i = 0 ; i < 100 ; i ++) {
@@ -446,18 +488,49 @@ public void testOnBackpressureDropSynchronous() {
446488 .map (SLOW_PASS_THRU ).take (NUM ).subscribe (ts );
447489 ts .awaitTerminalEvent ();
448490 ts .assertNoErrors ();
449-
491+
450492 List <Integer > onNextEvents = ts .getOnNextEvents ();
451493 assertEquals (NUM , onNextEvents .size ());
452494
453495 Integer lastEvent = onNextEvents .get (NUM - 1 );
454-
496+
455497 System .out .println ("testOnBackpressureDrop => Received: " + onNextEvents .size () + " Emitted: " + c .get () + " Last value: " + lastEvent );
456498 // it drop, so we should get some number far higher than what would have sequentially incremented
457499 assertTrue (NUM - 1 <= lastEvent .intValue ());
458500 }
459501 }
460502
503+ @ Test (timeout = 10000 )
504+ public void testOnBackpressureDropSynchronousWithAction () {
505+ for (int i = 0 ; i < 100 ; i ++) {
506+ final AtomicInteger dropCount = new AtomicInteger ();
507+ int NUM = (int ) (RxRingBuffer .SIZE * 1.1 ); // > 1 so that take doesn't prevent buffer overflow
508+ AtomicInteger c = new AtomicInteger ();
509+ TestSubscriber <Integer > ts = new TestSubscriber <Integer >();
510+ firehose (c ).onBackpressureDrop (new Action1 <Integer >() {
511+ @ Override
512+ public void call (Integer i ) {
513+ dropCount .incrementAndGet ();
514+ }
515+ })
516+ .map (SLOW_PASS_THRU ).take (NUM ).subscribe (ts );
517+ ts .awaitTerminalEvent ();
518+ ts .assertNoErrors ();
519+
520+ List <Integer > onNextEvents = ts .getOnNextEvents ();
521+ assertEquals (NUM , onNextEvents .size ());
522+
523+ Integer lastEvent = onNextEvents .get (NUM - 1 );
524+
525+ System .out .println ("testOnBackpressureDrop => Received: " + onNextEvents .size () + " Dropped: " + dropCount .get () + " Emitted: " + c .get () + " Last value: " + lastEvent );
526+ // it drop, so we should get some number far higher than what would have sequentially incremented
527+ assertTrue (NUM - 1 <= lastEvent .intValue ());
528+ // no drop in synchronous mode
529+ assertEquals (0 , dropCount .get ());
530+ assertEquals (c .get (), onNextEvents .size ());
531+ }
532+ }
533+
461534 @ Test (timeout = 2000 )
462535 public void testOnBackpressureBuffer () {
463536 int NUM = (int ) (RxRingBuffer .SIZE * 1.1 ); // > 1 so that take doesn't prevent buffer overflow
0 commit comments