23
23
24
24
import org .junit .*;
25
25
26
+ import org .junit .rules .TestName ;
26
27
import rx .Observable .OnSubscribe ;
27
28
import rx .exceptions .MissingBackpressureException ;
28
29
import rx .functions .*;
33
34
34
35
public class BackpressureTests {
35
36
37
+ @ Rule
38
+ public TestName testName = new TestName ();
39
+
36
40
@ After
37
41
public void doAfterTest () {
38
42
TestObstructionDetection .checkObstruction ();
@@ -424,18 +428,56 @@ public void testOnBackpressureDrop() {
424
428
.map (SLOW_PASS_THRU ).take (NUM ).subscribe (ts );
425
429
ts .awaitTerminalEvent ();
426
430
ts .assertNoErrors ();
427
-
428
-
431
+
429
432
List <Integer > onNextEvents = ts .getOnNextEvents ();
430
433
assertEquals (NUM , onNextEvents .size ());
431
434
432
435
Integer lastEvent = onNextEvents .get (NUM - 1 );
433
-
436
+
434
437
System .out .println ("testOnBackpressureDrop => Received: " + onNextEvents .size () + " Emitted: " + c .get () + " Last value: " + lastEvent );
435
438
// it drop, so we should get some number far higher than what would have sequentially incremented
436
439
assertTrue (NUM - 1 <= lastEvent .intValue ());
437
440
}
438
441
}
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
+
439
481
@ Test (timeout = 10000 )
440
482
public void testOnBackpressureDropSynchronous () {
441
483
for (int i = 0 ; i < 100 ; i ++) {
@@ -446,18 +488,49 @@ public void testOnBackpressureDropSynchronous() {
446
488
.map (SLOW_PASS_THRU ).take (NUM ).subscribe (ts );
447
489
ts .awaitTerminalEvent ();
448
490
ts .assertNoErrors ();
449
-
491
+
450
492
List <Integer > onNextEvents = ts .getOnNextEvents ();
451
493
assertEquals (NUM , onNextEvents .size ());
452
494
453
495
Integer lastEvent = onNextEvents .get (NUM - 1 );
454
-
496
+
455
497
System .out .println ("testOnBackpressureDrop => Received: " + onNextEvents .size () + " Emitted: " + c .get () + " Last value: " + lastEvent );
456
498
// it drop, so we should get some number far higher than what would have sequentially incremented
457
499
assertTrue (NUM - 1 <= lastEvent .intValue ());
458
500
}
459
501
}
460
502
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
+
461
534
@ Test (timeout = 2000 )
462
535
public void testOnBackpressureBuffer () {
463
536
int NUM = (int ) (RxRingBuffer .SIZE * 1.1 ); // > 1 so that take doesn't prevent buffer overflow
0 commit comments