1
+ /**
2
+ * Copyright 2014 Netflix, Inc.
3
+ *
4
+ * Licensed under the Apache License, Version 2.0 (the "License");
5
+ * you may not use this file except in compliance with the License.
6
+ * You may obtain a copy of the License at
7
+ *
8
+ * http://www.apache.org/licenses/LICENSE-2.0
9
+ *
10
+ * Unless required by applicable law or agreed to in writing, software
11
+ * distributed under the License is distributed on an "AS IS" BASIS,
12
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13
+ * See the License for the specific language governing permissions and
14
+ * limitations under the License.
15
+ */
16
+ package rx .internal .operators ;
17
+
18
+ import static org .junit .Assert .*;
19
+
20
+ import java .util .*;
21
+ import java .util .concurrent .atomic .AtomicBoolean ;
22
+
23
+ import org .junit .Test ;
24
+
25
+ import rx .*;
26
+ import rx .Observable ;
27
+ import rx .functions .Action0 ;
28
+ import rx .observers .TestSubscriber ;
29
+ import rx .subscriptions .Subscriptions ;
30
+
31
+ public class OperatorSwitchIfEmptyTest {
32
+
33
+ @ Test
34
+ public void testSwitchWhenNotEmpty () throws Exception {
35
+ final AtomicBoolean subscribed = new AtomicBoolean (false );
36
+ final Observable <Integer > observable = Observable .just (4 ).switchIfEmpty (Observable .just (2 )
37
+ .doOnSubscribe (new Action0 () {
38
+ @ Override
39
+ public void call () {
40
+ subscribed .set (true );
41
+ }
42
+ }));
43
+
44
+ assertEquals (4 , observable .toBlocking ().single ().intValue ());
45
+ assertFalse (subscribed .get ());
46
+ }
47
+
48
+ @ Test
49
+ public void testSwitchWhenEmpty () throws Exception {
50
+ final Observable <Integer > observable = Observable .<Integer >empty ().switchIfEmpty (Observable .from (Arrays .asList (42 )));
51
+
52
+ assertEquals (42 , observable .toBlocking ().single ().intValue ());
53
+ }
54
+
55
+ @ Test
56
+ public void testSwitchWithProducer () throws Exception {
57
+ final AtomicBoolean emitted = new AtomicBoolean (false );
58
+ Observable <Long > withProducer = Observable .create (new Observable .OnSubscribe <Long >() {
59
+ @ Override
60
+ public void call (final Subscriber <? super Long > subscriber ) {
61
+ subscriber .setProducer (new Producer () {
62
+ @ Override
63
+ public void request (long n ) {
64
+ if (n > 0 && !emitted .get ()) {
65
+ emitted .set (true );
66
+ subscriber .onNext (42L );
67
+ subscriber .onCompleted ();
68
+ }
69
+ }
70
+ });
71
+ }
72
+ });
73
+
74
+ final Observable <Long > observable = Observable .<Long >empty ().switchIfEmpty (withProducer );
75
+ assertEquals (42 , observable .toBlocking ().single ().intValue ());
76
+ }
77
+
78
+ @ Test
79
+ public void testSwitchTriggerUnsubscribe () throws Exception {
80
+ final Subscription empty = Subscriptions .empty ();
81
+
82
+ Observable <Long > withProducer = Observable .create (new Observable .OnSubscribe <Long >() {
83
+ @ Override
84
+ public void call (final Subscriber <? super Long > subscriber ) {
85
+ subscriber .add (empty );
86
+ subscriber .onNext (42L );
87
+ }
88
+ });
89
+
90
+ final Subscription sub = Observable .<Long >empty ().switchIfEmpty (withProducer ).lift (new Observable .Operator <Long , Long >() {
91
+ @ Override
92
+ public Subscriber <? super Long > call (final Subscriber <? super Long > child ) {
93
+ return new Subscriber <Long >(child ) {
94
+ @ Override
95
+ public void onCompleted () {
96
+
97
+ }
98
+
99
+ @ Override
100
+ public void onError (Throwable e ) {
101
+
102
+ }
103
+
104
+ @ Override
105
+ public void onNext (Long aLong ) {
106
+ unsubscribe ();
107
+ }
108
+ };
109
+ }
110
+ }).subscribe ();
111
+
112
+
113
+ assertTrue (empty .isUnsubscribed ());
114
+ assertTrue (sub .isUnsubscribed ());
115
+ }
116
+
117
+ @ Test
118
+ public void testSwitchShouldTriggerUnsubscribe () {
119
+ final Subscription s = Subscriptions .empty ();
120
+
121
+ Observable .create (new Observable .OnSubscribe <Long >() {
122
+ @ Override
123
+ public void call (final Subscriber <? super Long > subscriber ) {
124
+ subscriber .add (s );
125
+ subscriber .onCompleted ();
126
+ }
127
+ }).switchIfEmpty (Observable .<Long >never ()).subscribe ();
128
+ assertTrue (s .isUnsubscribed ());
129
+ }
130
+
131
+ @ Test
132
+ public void testSwitchRequestAlternativeObservableWithBackpressure () {
133
+
134
+ TestSubscriber <Integer > ts = new TestSubscriber <Integer >() {
135
+
136
+ @ Override
137
+ public void onStart () {
138
+ request (1 );
139
+ }
140
+ };
141
+ Observable .<Integer >empty ().switchIfEmpty (Observable .just (1 , 2 , 3 )).subscribe (ts );
142
+
143
+ assertEquals (Arrays .asList (1 ), ts .getOnNextEvents ());
144
+ ts .assertNoErrors ();
145
+ }
146
+ @ Test
147
+ public void testBackpressureNoRequest () {
148
+ TestSubscriber <Integer > ts = new TestSubscriber <Integer >() {
149
+
150
+ @ Override
151
+ public void onStart () {
152
+ request (0 );
153
+ }
154
+ };
155
+ Observable .<Integer >empty ().switchIfEmpty (Observable .just (1 , 2 , 3 )).subscribe (ts );
156
+
157
+ assertTrue (ts .getOnNextEvents ().isEmpty ());
158
+ ts .assertNoErrors ();
159
+ }
160
+ }
0 commit comments