File tree Expand file tree Collapse file tree 2 files changed +21
-10
lines changed
main/java/rx/internal/operators
test/java/rx/internal/operators Expand file tree Collapse file tree 2 files changed +21
-10
lines changed Original file line number Diff line number Diff line change 24
24
import rx .Observable ;
25
25
import rx .Subscriber ;
26
26
import rx .exceptions .Exceptions ;
27
+ import rx .internal .util .RxRingBuffer ;
27
28
28
29
/**
29
30
* Returns an Iterator that iterates over all items emitted by a specified Observable.
@@ -56,17 +57,19 @@ public static <T> Iterator<T> toIterator(Observable<? extends T> source) {
56
57
public static final class SubscriberIterator <T >
57
58
extends Subscriber <Notification <? extends T >> implements Iterator <T > {
58
59
60
+ static final int LIMIT = 3 * RxRingBuffer .SIZE / 4 ;
61
+
59
62
private final BlockingQueue <Notification <? extends T >> notifications ;
60
63
private Notification <? extends T > buf ;
64
+ private int received ;
61
65
62
66
public SubscriberIterator () {
63
67
this .notifications = new LinkedBlockingQueue <Notification <? extends T >>();
64
- this .buf = null ;
65
68
}
66
69
67
70
@ Override
68
71
public void onStart () {
69
- request (0 );
72
+ request (RxRingBuffer . SIZE );
70
73
}
71
74
72
75
@ Override
@@ -87,8 +90,12 @@ public void onNext(Notification<? extends T> args) {
87
90
@ Override
88
91
public boolean hasNext () {
89
92
if (buf == null ) {
90
- request (1 );
91
93
buf = take ();
94
+ received ++;
95
+ if (received >= LIMIT ) {
96
+ request (received );
97
+ received = 0 ;
98
+ }
92
99
}
93
100
if (buf .isOnError ()) {
94
101
throw Exceptions .propagate (buf .getThrowable ());
Original file line number Diff line number Diff line change 26
26
import rx .Observable .OnSubscribe ;
27
27
import rx .Subscriber ;
28
28
import rx .exceptions .TestException ;
29
+ import rx .internal .operators .BlockingOperatorToIterator .SubscriberIterator ;
30
+ import rx .internal .util .RxRingBuffer ;
29
31
30
32
public class BlockingOperatorToIteratorTest {
31
33
@@ -96,26 +98,28 @@ public Iterator<Integer> iterator() {
96
98
Iterator <Integer > it = toIterator (obs );
97
99
while (it .hasNext ()) {
98
100
// Correct backpressure should cause this interleaved behavior.
101
+ // We first request RxRingBuffer.SIZE. Then in increments of
102
+ // SubscriberIterator.LIMIT.
99
103
int i = it .next ();
100
- assertEquals (i + 1 , src .count );
104
+ int expected = i - (i % SubscriberIterator .LIMIT ) + RxRingBuffer .SIZE ;
105
+ expected = Math .min (expected , Counter .MAX );
106
+
107
+ assertEquals (expected , src .count );
101
108
}
102
109
}
103
110
104
111
public static final class Counter implements Iterator <Integer > {
112
+ static final int MAX = 5 * RxRingBuffer .SIZE ;
105
113
public int count ;
106
114
107
- public Counter () {
108
- this .count = 0 ;
109
- }
110
-
111
115
@ Override
112
116
public boolean hasNext () {
113
- return count < 5 ;
117
+ return count < MAX ;
114
118
}
115
119
116
120
@ Override
117
121
public Integer next () {
118
- return count ++ ;
122
+ return ++ count ;
119
123
}
120
124
121
125
@ Override
You can’t perform that action at this time.
0 commit comments