|
15 | 15 | */
|
16 | 16 | package rx.internal.operators;
|
17 | 17 |
|
| 18 | +import static org.junit.Assert.assertEquals; |
18 | 19 | import static org.junit.Assert.assertTrue;
|
19 | 20 | import static org.mockito.Matchers.any;
|
20 | 21 | import static org.mockito.Matchers.anyString;
|
21 |
| -import static org.mockito.Mockito.*; |
| 22 | +import static org.mockito.Mockito.inOrder; |
| 23 | +import static org.mockito.Mockito.mock; |
| 24 | +import static org.mockito.Mockito.never; |
| 25 | +import static org.mockito.Mockito.times; |
| 26 | +import static org.mockito.Mockito.verify; |
22 | 27 |
|
| 28 | +import java.util.ArrayList; |
23 | 29 | import java.util.Arrays;
|
| 30 | +import java.util.List; |
| 31 | +import java.util.concurrent.CopyOnWriteArrayList; |
24 | 32 | import java.util.concurrent.TimeUnit;
|
25 | 33 | import java.util.concurrent.atomic.AtomicBoolean;
|
26 | 34 |
|
|
36 | 44 | import rx.Subscriber;
|
37 | 45 | import rx.exceptions.TestException;
|
38 | 46 | import rx.functions.Action0;
|
| 47 | +import rx.functions.Action1; |
39 | 48 | import rx.functions.Func1;
|
40 | 49 | import rx.observers.TestSubscriber;
|
41 | 50 | import rx.schedulers.TestScheduler;
|
@@ -574,4 +583,91 @@ public void onNext(String t) {
|
574 | 583 |
|
575 | 584 | Assert.assertEquals(250, ts.getOnNextEvents().size());
|
576 | 585 | }
|
| 586 | + |
| 587 | + @Test(timeout = 10000) |
| 588 | + public void testInitialRequestsAreAdditive() { |
| 589 | + TestSubscriber<Long> ts = new TestSubscriber<Long>(0); |
| 590 | + Observable.switchOnNext( |
| 591 | + Observable.interval(100, TimeUnit.MILLISECONDS) |
| 592 | + .map( |
| 593 | + new Func1<Long, Observable<Long>>() { |
| 594 | + @Override |
| 595 | + public Observable<Long> call(Long t) { |
| 596 | + return Observable.just(1L, 2L, 3L); |
| 597 | + } |
| 598 | + } |
| 599 | + ).take(3)) |
| 600 | + .subscribe(ts); |
| 601 | + ts.requestMore(Long.MAX_VALUE - 100); |
| 602 | + ts.requestMore(1); |
| 603 | + ts.awaitTerminalEvent(); |
| 604 | + } |
| 605 | + |
| 606 | + @Test(timeout = 10000) |
| 607 | + public void testInitialRequestsDontOverflow() { |
| 608 | + TestSubscriber<Long> ts = new TestSubscriber<Long>(0); |
| 609 | + Observable.switchOnNext( |
| 610 | + Observable.interval(100, TimeUnit.MILLISECONDS) |
| 611 | + .map(new Func1<Long, Observable<Long>>() { |
| 612 | + @Override |
| 613 | + public Observable<Long> call(Long t) { |
| 614 | + return Observable.from(Arrays.asList(1L, 2L, 3L)); |
| 615 | + } |
| 616 | + }).take(3)).subscribe(ts); |
| 617 | + ts.requestMore(Long.MAX_VALUE - 1); |
| 618 | + ts.requestMore(2); |
| 619 | + ts.awaitTerminalEvent(); |
| 620 | + assertTrue(ts.getOnNextEvents().size() > 0); |
| 621 | + } |
| 622 | + |
| 623 | + |
| 624 | + @Test(timeout = 10000) |
| 625 | + public void testSecondaryRequestsDontOverflow() throws InterruptedException { |
| 626 | + TestSubscriber<Long> ts = new TestSubscriber<Long>(0); |
| 627 | + Observable.switchOnNext( |
| 628 | + Observable.interval(100, TimeUnit.MILLISECONDS) |
| 629 | + .map(new Func1<Long, Observable<Long>>() { |
| 630 | + @Override |
| 631 | + public Observable<Long> call(Long t) { |
| 632 | + return Observable.from(Arrays.asList(1L, 2L, 3L)); |
| 633 | + } |
| 634 | + }).take(3)).subscribe(ts); |
| 635 | + ts.requestMore(1); |
| 636 | + //we will miss two of the first observable |
| 637 | + Thread.sleep(250); |
| 638 | + ts.requestMore(Long.MAX_VALUE - 1); |
| 639 | + ts.requestMore(Long.MAX_VALUE - 1); |
| 640 | + ts.awaitTerminalEvent(); |
| 641 | + ts.assertValueCount(7); |
| 642 | + } |
| 643 | + |
| 644 | + @Test(timeout = 10000) |
| 645 | + public void testSecondaryRequestsAdditivelyAreMoreThanLongMaxValueInducesMaxValueRequestFromUpstream() throws InterruptedException { |
| 646 | + final List<Long> requests = new CopyOnWriteArrayList<Long>(); |
| 647 | + final Action1<Long> addRequest = new Action1<Long>() { |
| 648 | + |
| 649 | + @Override |
| 650 | + public void call(Long n) { |
| 651 | + requests.add(n); |
| 652 | + }}; |
| 653 | + TestSubscriber<Long> ts = new TestSubscriber<Long>(0); |
| 654 | + Observable.switchOnNext( |
| 655 | + Observable.interval(100, TimeUnit.MILLISECONDS) |
| 656 | + .map(new Func1<Long, Observable<Long>>() { |
| 657 | + @Override |
| 658 | + public Observable<Long> call(Long t) { |
| 659 | + return Observable.from(Arrays.asList(1L, 2L, 3L)).doOnRequest(addRequest); |
| 660 | + } |
| 661 | + }).take(3)).subscribe(ts); |
| 662 | + ts.requestMore(1); |
| 663 | + //we will miss two of the first observable |
| 664 | + Thread.sleep(250); |
| 665 | + ts.requestMore(Long.MAX_VALUE - 1); |
| 666 | + ts.requestMore(Long.MAX_VALUE - 1); |
| 667 | + ts.awaitTerminalEvent(); |
| 668 | + assertTrue(ts.getOnNextEvents().size() > 0); |
| 669 | + assertEquals(5, (int) requests.size()); |
| 670 | + assertEquals(Long.MAX_VALUE, (long) requests.get(3)); |
| 671 | + assertEquals(Long.MAX_VALUE, (long) requests.get(4)); |
| 672 | + } |
577 | 673 | }
|
0 commit comments