-
Notifications
You must be signed in to change notification settings - Fork 7.6k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
issue-2764: add new operator onBackpressureDrop(Action1 onDrop) #2776
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -23,6 +23,7 @@ | |
|
||
import org.junit.*; | ||
|
||
import org.junit.rules.TestName; | ||
import rx.Observable.OnSubscribe; | ||
import rx.exceptions.MissingBackpressureException; | ||
import rx.functions.*; | ||
|
@@ -33,6 +34,9 @@ | |
|
||
public class BackpressureTests { | ||
|
||
@Rule | ||
public TestName testName = new TestName(); | ||
|
||
@After | ||
public void doAfterTest() { | ||
TestObstructionDetection.checkObstruction(); | ||
|
@@ -424,18 +428,56 @@ public void testOnBackpressureDrop() { | |
.map(SLOW_PASS_THRU).take(NUM).subscribe(ts); | ||
ts.awaitTerminalEvent(); | ||
ts.assertNoErrors(); | ||
|
||
|
||
|
||
List<Integer> onNextEvents = ts.getOnNextEvents(); | ||
assertEquals(NUM, onNextEvents.size()); | ||
|
||
Integer lastEvent = onNextEvents.get(NUM - 1); | ||
|
||
System.out.println("testOnBackpressureDrop => Received: " + onNextEvents.size() + " Emitted: " + c.get() + " Last value: " + lastEvent); | ||
// it drop, so we should get some number far higher than what would have sequentially incremented | ||
assertTrue(NUM - 1 <= lastEvent.intValue()); | ||
} | ||
} | ||
|
||
@Test(timeout = 10000) | ||
public void testOnBackpressureDropWithAction() { | ||
for (int i = 0; i < 100; i++) { | ||
final AtomicInteger emitCount = new AtomicInteger(); | ||
final AtomicInteger dropCount = new AtomicInteger(); | ||
final AtomicInteger passCount = new AtomicInteger(); | ||
final int NUM = (int) (RxRingBuffer.SIZE * 1.5); // > 1 so that take doesn't prevent buffer overflow | ||
TestSubscriber<Integer> ts = new TestSubscriber<Integer>(); | ||
firehose(emitCount).onBackpressureDrop(new Action1<Integer>() { | ||
@Override | ||
public void call(Integer i) { | ||
dropCount.incrementAndGet(); | ||
} | ||
}) | ||
.doOnNext(new Action1<Integer>() { | ||
@Override | ||
public void call(Integer integer) { | ||
passCount.incrementAndGet(); | ||
} | ||
}) | ||
.observeOn(Schedulers.computation()) | ||
.map(SLOW_PASS_THRU).take(NUM).subscribe(ts); | ||
ts.awaitTerminalEvent(); | ||
ts.assertNoErrors(); | ||
|
||
List<Integer> onNextEvents = ts.getOnNextEvents(); | ||
Integer lastEvent = onNextEvents.get(NUM - 1); | ||
System.out.println(testName.getMethodName() + " => Received: " + onNextEvents.size() + " Passed: " + passCount.get() + " Dropped: " + dropCount.get() + " Emitted: " + emitCount.get() + " Last value: " + lastEvent); | ||
assertEquals(NUM, onNextEvents.size()); | ||
// in reality, NUM < passCount | ||
assertTrue(NUM <= passCount.get()); | ||
// it drop, so we should get some number far higher than what would have sequentially incremented | ||
assertTrue(NUM - 1 <= lastEvent.intValue()); | ||
assertTrue(0 < dropCount.get()); | ||
assertEquals(emitCount.get(), passCount.get() + dropCount.get()); | ||
} | ||
} | ||
|
||
@Test(timeout = 10000) | ||
public void testOnBackpressureDropSynchronous() { | ||
for (int i = 0; i < 100; i++) { | ||
|
@@ -446,18 +488,49 @@ public void testOnBackpressureDropSynchronous() { | |
.map(SLOW_PASS_THRU).take(NUM).subscribe(ts); | ||
ts.awaitTerminalEvent(); | ||
ts.assertNoErrors(); | ||
|
||
List<Integer> onNextEvents = ts.getOnNextEvents(); | ||
assertEquals(NUM, onNextEvents.size()); | ||
|
||
Integer lastEvent = onNextEvents.get(NUM - 1); | ||
|
||
System.out.println("testOnBackpressureDrop => Received: " + onNextEvents.size() + " Emitted: " + c.get() + " Last value: " + lastEvent); | ||
// it drop, so we should get some number far higher than what would have sequentially incremented | ||
assertTrue(NUM - 1 <= lastEvent.intValue()); | ||
} | ||
} | ||
|
||
@Test(timeout = 10000) | ||
public void testOnBackpressureDropSynchronousWithAction() { | ||
for (int i = 0; i < 100; i++) { | ||
final AtomicInteger dropCount = new AtomicInteger(); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. These changes should be in a separate test method. Leave the There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. will do |
||
int NUM = (int) (RxRingBuffer.SIZE * 1.1); // > 1 so that take doesn't prevent buffer overflow | ||
AtomicInteger c = new AtomicInteger(); | ||
TestSubscriber<Integer> ts = new TestSubscriber<Integer>(); | ||
firehose(c).onBackpressureDrop(new Action1<Integer>() { | ||
@Override | ||
public void call(Integer i) { | ||
dropCount.incrementAndGet(); | ||
} | ||
}) | ||
.map(SLOW_PASS_THRU).take(NUM).subscribe(ts); | ||
ts.awaitTerminalEvent(); | ||
ts.assertNoErrors(); | ||
|
||
List<Integer> onNextEvents = ts.getOnNextEvents(); | ||
assertEquals(NUM, onNextEvents.size()); | ||
|
||
Integer lastEvent = onNextEvents.get(NUM - 1); | ||
|
||
System.out.println("testOnBackpressureDrop => Received: " + onNextEvents.size() + " Dropped: " + dropCount.get() + " Emitted: " + c.get() + " Last value: " + lastEvent); | ||
// it drop, so we should get some number far higher than what would have sequentially incremented | ||
assertTrue(NUM - 1 <= lastEvent.intValue()); | ||
// no drop in synchronous mode | ||
assertEquals(0, dropCount.get()); | ||
assertEquals(c.get(), onNextEvents.size()); | ||
} | ||
} | ||
|
||
@Test(timeout = 2000) | ||
public void testOnBackpressureBuffer() { | ||
int NUM = (int) (RxRingBuffer.SIZE * 1.1); // > 1 so that take doesn't prevent buffer overflow | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Please add
@Experimental
annotation to this method.