17
17
18
18
import static org .junit .Assert .assertEquals ;
19
19
20
+ import java .util .concurrent .CountDownLatch ;
21
+ import java .util .concurrent .atomic .AtomicInteger ;
22
+
20
23
import org .junit .Test ;
21
24
22
25
import rx .Observable ;
27
30
import rx .observers .TestSubscriber ;
28
31
import rx .schedulers .Schedulers ;
29
32
30
- import java .util .concurrent .CountDownLatch ;
31
-
32
33
public class OperatorOnBackpressureDropTest {
33
34
34
35
@ Test
@@ -87,6 +88,35 @@ public void onNext(Long t) {
87
88
ts .assertNoErrors ();
88
89
assertEquals (0 , ts .getOnNextEvents ().get (0 ).intValue ());
89
90
}
91
+
92
+ @ Test
93
+ public void testRequestOverflow () throws InterruptedException {
94
+ final AtomicInteger count = new AtomicInteger ();
95
+ int n = 10 ;
96
+ range (n ).onBackpressureDrop ().subscribe (new Subscriber <Long >() {
97
+
98
+ @ Override
99
+ public void onStart () {
100
+ request (10 );
101
+ }
102
+
103
+ @ Override
104
+ public void onCompleted () {
105
+ }
106
+
107
+ @ Override
108
+ public void onError (Throwable e ) {
109
+ throw new RuntimeException (e );
110
+ }
111
+
112
+ @ Override
113
+ public void onNext (Long t ) {
114
+ count .incrementAndGet ();
115
+ //cause overflow of requested if not handled properly in onBackpressureDrop operator
116
+ request (Long .MAX_VALUE -1 );
117
+ }});
118
+ assertEquals (n , count .get ());
119
+ }
90
120
91
121
static final Observable <Long > infinite = Observable .create (new OnSubscribe <Long >() {
92
122
@@ -99,4 +129,22 @@ public void call(Subscriber<? super Long> s) {
99
129
}
100
130
101
131
});
132
+
133
+ private static final Observable <Long > range (final long n ) {
134
+ return Observable .create (new OnSubscribe <Long >() {
135
+
136
+ @ Override
137
+ public void call (Subscriber <? super Long > s ) {
138
+ for (long i =0 ;i < n ;i ++) {
139
+ if (s .isUnsubscribed ()) {
140
+ break ;
141
+ }
142
+ s .onNext (i );
143
+ }
144
+ s .onCompleted ();
145
+ }
146
+
147
+ });
148
+ }
149
+
102
150
}
0 commit comments