23
23
import rx .functions .Action0 ;
24
24
import rx .subscriptions .Subscriptions ;
25
25
26
+ import java .util .ArrayList ;
26
27
import java .util .Arrays ;
28
+ import java .util .List ;
27
29
import java .util .concurrent .atomic .AtomicBoolean ;
28
30
29
31
import static org .junit .Assert .assertEquals ;
@@ -56,13 +58,15 @@ public void testSwitchWhenEmpty() throws Exception {
56
58
57
59
@ Test
58
60
public void testSwitchWithProducer () throws Exception {
61
+ final AtomicBoolean emitted = new AtomicBoolean (false );
59
62
Observable <Long > withProducer = Observable .create (new Observable .OnSubscribe <Long >() {
60
63
@ Override
61
64
public void call (final Subscriber <? super Long > subscriber ) {
62
65
subscriber .setProducer (new Producer () {
63
66
@ Override
64
67
public void request (long n ) {
65
- if (n > 0 ) {
68
+ if (n > 0 && !emitted .get ()) {
69
+ emitted .set (true );
66
70
subscriber .onNext (42L );
67
71
subscriber .onCompleted ();
68
72
}
@@ -127,4 +131,33 @@ public void call(final Subscriber<? super Long> subscriber) {
127
131
}).switchIfEmpty (Observable .<Long >never ()).subscribe ();
128
132
assertTrue (s .isUnsubscribed ());
129
133
}
134
+
135
+ @ Test
136
+ public void testSwitchRequestAlternativeObservableWithBackpressure () {
137
+ final List <Integer > items = new ArrayList <Integer >();
138
+
139
+ Observable .<Integer >empty ().switchIfEmpty (Observable .just (1 , 2 , 3 )).subscribe (new Subscriber <Integer >() {
140
+
141
+ @ Override
142
+ public void onStart () {
143
+ request (1 );
144
+ }
145
+
146
+ @ Override
147
+ public void onCompleted () {
148
+
149
+ }
150
+
151
+ @ Override
152
+ public void onError (Throwable e ) {
153
+
154
+ }
155
+
156
+ @ Override
157
+ public void onNext (Integer integer ) {
158
+ items .add (integer );
159
+ }
160
+ });
161
+ assertEquals (Arrays .asList (1 ), items );
162
+ }
130
163
}
0 commit comments