Debian Codegraph @

src/test/java/io/reactivex/rxjava3/internal/operators/observable/ObservableTakeUntilTest.java

raw
1 /*
2 * Copyright (c) 2016-present, RxJava Contributors.
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in
5 * compliance with the License. You may obtain a copy of the License at
6 *
7 * http://www.apache.org/licenses/LICENSE-2.0
8 *
9 * Unless required by applicable law or agreed to in writing, software distributed under the License is
10 * distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See
11 * the License for the specific language governing permissions and limitations under the License.
12 */
13
14 package io.reactivex.rxjava3.internal.operators.observable;
15
16 import static org.junit.Assert.*;
17 import static org.mockito.Mockito.*;
18
19 import org.junit.Test;
20
21 import io.reactivex.rxjava3.core.*;
22 import io.reactivex.rxjava3.disposables.Disposable;
23 import io.reactivex.rxjava3.exceptions.TestException;
24 import io.reactivex.rxjava3.functions.Function;
25 import io.reactivex.rxjava3.observers.TestObserver;
26 import io.reactivex.rxjava3.subjects.PublishSubject;
27 import io.reactivex.rxjava3.testsupport.*;
28
29 public class ObservableTakeUntilTest extends RxJavaTest {
30
31 @Test
32 public void takeUntil() {
33 Disposable sSource = mock(Disposable.class);
34 Disposable sOther = mock(Disposable.class);
35 TestObservable source = new TestObservable(sSource);
36 TestObservable other = new TestObservable(sOther);
37
38 Observer<String> result = TestHelper.mockObserver();
39 Observable<String> stringObservable = Observable.unsafeCreate(source)
40 .takeUntil(Observable.unsafeCreate(other));
41 stringObservable.subscribe(result);
42 source.sendOnNext("one");
43 source.sendOnNext("two");
44 other.sendOnNext("three");
45 source.sendOnNext("four");
46 source.sendOnCompleted();
47 other.sendOnCompleted();
48
49 verify(result, times(1)).onNext("one");
50 verify(result, times(1)).onNext("two");
51 verify(result, times(0)).onNext("three");
52 verify(result, times(0)).onNext("four");
53 verify(sSource, times(1)).dispose();
54 verify(sOther, times(1)).dispose();
55
56 }
57
58 @Test
59 public void takeUntilSourceCompleted() {
60 Disposable sSource = mock(Disposable.class);
61 Disposable sOther = mock(Disposable.class);
62 TestObservable source = new TestObservable(sSource);
63 TestObservable other = new TestObservable(sOther);
64
65 Observer<String> result = TestHelper.mockObserver();
66 Observable<String> stringObservable = Observable.unsafeCreate(source).takeUntil(Observable.unsafeCreate(other));
67 stringObservable.subscribe(result);
68 source.sendOnNext("one");
69 source.sendOnNext("two");
70 source.sendOnCompleted();
71
72 verify(result, times(1)).onNext("one");
73 verify(result, times(1)).onNext("two");
74 verify(sSource, never()).dispose(); // no longer disposing itself on terminal events
75 verify(sOther, times(1)).dispose();
76
77 }
78
79 @Test
80 public void takeUntilSourceError() {
81 Disposable sSource = mock(Disposable.class);
82 Disposable sOther = mock(Disposable.class);
83 TestObservable source = new TestObservable(sSource);
84 TestObservable other = new TestObservable(sOther);
85 Throwable error = new Throwable();
86
87 Observer<String> result = TestHelper.mockObserver();
88 Observable<String> stringObservable = Observable.unsafeCreate(source).takeUntil(Observable.unsafeCreate(other));
89 stringObservable.subscribe(result);
90 source.sendOnNext("one");
91 source.sendOnNext("two");
92 source.sendOnError(error);
93 source.sendOnNext("three");
94
95 verify(result, times(1)).onNext("one");
96 verify(result, times(1)).onNext("two");
97 verify(result, times(0)).onNext("three");
98 verify(result, times(1)).onError(error);
99 verify(sSource, never()).dispose(); // no longer disposing itself on terminal events
100 verify(sOther, times(1)).dispose();
101
102 }
103
104 @Test
105 public void takeUntilOtherError() {
106 Disposable sSource = mock(Disposable.class);
107 Disposable sOther = mock(Disposable.class);
108 TestObservable source = new TestObservable(sSource);
109 TestObservable other = new TestObservable(sOther);
110 Throwable error = new Throwable();
111
112 Observer<String> result = TestHelper.mockObserver();
113 Observable<String> stringObservable = Observable.unsafeCreate(source).takeUntil(Observable.unsafeCreate(other));
114 stringObservable.subscribe(result);
115 source.sendOnNext("one");
116 source.sendOnNext("two");
117 other.sendOnError(error);
118 source.sendOnNext("three");
119
120 verify(result, times(1)).onNext("one");
121 verify(result, times(1)).onNext("two");
122 verify(result, times(0)).onNext("three");
123 verify(result, times(1)).onError(error);
124 verify(result, times(0)).onComplete();
125 verify(sSource, times(1)).dispose();
126 verify(sOther, never()).dispose(); // no longer disposing itself on termination
127
128 }
129
130 /**
131 * If the 'other' onCompletes then we unsubscribe from the source and onComplete.
132 */
133 @Test
134 public void takeUntilOtherCompleted() {
135 Disposable sSource = mock(Disposable.class);
136 Disposable sOther = mock(Disposable.class);
137 TestObservable source = new TestObservable(sSource);
138 TestObservable other = new TestObservable(sOther);
139
140 Observer<String> result = TestHelper.mockObserver();
141 Observable<String> stringObservable = Observable.unsafeCreate(source).takeUntil(Observable.unsafeCreate(other));
142 stringObservable.subscribe(result);
143 source.sendOnNext("one");
144 source.sendOnNext("two");
145 other.sendOnCompleted();
146 source.sendOnNext("three");
147
148 verify(result, times(1)).onNext("one");
149 verify(result, times(1)).onNext("two");
150 verify(result, times(0)).onNext("three");
151 verify(result, times(1)).onComplete();
152 verify(sSource, times(1)).dispose();
153 verify(sOther, never()).dispose(); // no longer disposing itself on terminal events
154
155 }
156
157 private static class TestObservable implements ObservableSource<String> {
158
159 Observer<? super String> observer;
160 Disposable upstream;
161
162 TestObservable(Disposable d) {
163 this.upstream = d;
164 }
165
166 /* used to simulate subscription */
167 public void sendOnCompleted() {
168 observer.onComplete();
169 }
170
171 /* used to simulate subscription */
172 public void sendOnNext(String value) {
173 observer.onNext(value);
174 }
175
176 /* used to simulate subscription */
177 public void sendOnError(Throwable e) {
178 observer.onError(e);
179 }
180
181 @Override
182 public void subscribe(Observer<? super String> observer) {
183 this.observer = observer;
184 observer.onSubscribe(upstream);
185 }
186 }
187
188 @Test
189 public void untilFires() {
190 PublishSubject<Integer> source = PublishSubject.create();
191 PublishSubject<Integer> until = PublishSubject.create();
192
193 TestObserverEx<Integer> to = new TestObserverEx<>();
194
195 source.takeUntil(until).subscribe(to);
196
197 assertTrue(source.hasObservers());
198 assertTrue(until.hasObservers());
199
200 source.onNext(1);
201
202 to.assertValue(1);
203 until.onNext(1);
204
205 to.assertValue(1);
206 to.assertNoErrors();
207 to.assertTerminated();
208
209 assertFalse("Source still has observers", source.hasObservers());
210 assertFalse("Until still has observers", until.hasObservers());
211 // 2.0.2 - not anymore
212 // assertTrue("Not cancelled!", ts.isCancelled());
213 }
214
215 @Test
216 public void mainCompletes() {
217 PublishSubject<Integer> source = PublishSubject.create();
218 PublishSubject<Integer> until = PublishSubject.create();
219
220 TestObserverEx<Integer> to = new TestObserverEx<>();
221
222 source.takeUntil(until).subscribe(to);
223
224 assertTrue(source.hasObservers());
225 assertTrue(until.hasObservers());
226
227 source.onNext(1);
228 source.onComplete();
229
230 to.assertValue(1);
231 to.assertNoErrors();
232 to.assertTerminated();
233
234 assertFalse("Source still has observers", source.hasObservers());
235 assertFalse("Until still has observers", until.hasObservers());
236 // 2.0.2 - not anymore
237 // assertTrue("Not cancelled!", ts.isCancelled());
238 }
239
240 @Test
241 public void downstreamUnsubscribes() {
242 PublishSubject<Integer> source = PublishSubject.create();
243 PublishSubject<Integer> until = PublishSubject.create();
244
245 TestObserverEx<Integer> to = new TestObserverEx<>();
246
247 source.takeUntil(until).take(1).subscribe(to);
248
249 assertTrue(source.hasObservers());
250 assertTrue(until.hasObservers());
251
252 source.onNext(1);
253
254 to.assertValue(1);
255 to.assertNoErrors();
256 to.assertTerminated();
257
258 assertFalse("Source still has observers", source.hasObservers());
259 assertFalse("Until still has observers", until.hasObservers());
260 // 2.0.2 - not anymore
261 // assertTrue("Not cancelled!", ts.isCancelled());
262 }
263
264 @Test
265 public void dispose() {
266 TestHelper.checkDisposed(PublishSubject.create().takeUntil(Observable.never()));
267 }
268
269 @Test
270 public void doubleOnSubscribe() {
271 TestHelper.checkDoubleOnSubscribeObservable(new Function<Observable<Integer>, Observable<Integer>>() {
272 @Override
273 public Observable<Integer> apply(Observable<Integer> o) throws Exception {
274 return o.takeUntil(Observable.never());
275 }
276 });
277 }
278
279 @Test
280 public void untilPublisherMainSuccess() {
281 PublishSubject<Integer> main = PublishSubject.create();
282 PublishSubject<Integer> other = PublishSubject.create();
283
284 TestObserver<Integer> to = main.takeUntil(other).test();
285
286 assertTrue("Main no observers?", main.hasObservers());
287 assertTrue("Other no observers?", other.hasObservers());
288
289 main.onNext(1);
290 main.onNext(2);
291 main.onComplete();
292
293 assertFalse("Main has observers?", main.hasObservers());
294 assertFalse("Other has observers?", other.hasObservers());
295
296 to.assertResult(1, 2);
297 }
298
299 @Test
300 public void untilPublisherMainComplete() {
301 PublishSubject<Integer> main = PublishSubject.create();
302 PublishSubject<Integer> other = PublishSubject.create();
303
304 TestObserver<Integer> to = main.takeUntil(other).test();
305
306 assertTrue("Main no observers?", main.hasObservers());
307 assertTrue("Other no observers?", other.hasObservers());
308
309 main.onComplete();
310
311 assertFalse("Main has observers?", main.hasObservers());
312 assertFalse("Other has observers?", other.hasObservers());
313
314 to.assertResult();
315 }
316
317 @Test
318 public void untilPublisherMainError() {
319 PublishSubject<Integer> main = PublishSubject.create();
320 PublishSubject<Integer> other = PublishSubject.create();
321
322 TestObserver<Integer> to = main.takeUntil(other).test();
323
324 assertTrue("Main no observers?", main.hasObservers());
325 assertTrue("Other no observers?", other.hasObservers());
326
327 main.onError(new TestException());
328
329 assertFalse("Main has observers?", main.hasObservers());
330 assertFalse("Other has observers?", other.hasObservers());
331
332 to.assertFailure(TestException.class);
333 }
334
335 @Test
336 public void untilPublisherOtherOnNext() {
337 PublishSubject<Integer> main = PublishSubject.create();
338 PublishSubject<Integer> other = PublishSubject.create();
339
340 TestObserver<Integer> to = main.takeUntil(other).test();
341
342 assertTrue("Main no observers?", main.hasObservers());
343 assertTrue("Other no observers?", other.hasObservers());
344
345 other.onNext(1);
346
347 assertFalse("Main has observers?", main.hasObservers());
348 assertFalse("Other has observers?", other.hasObservers());
349
350 to.assertResult();
351 }
352
353 @Test
354 public void untilPublisherOtherOnComplete() {
355 PublishSubject<Integer> main = PublishSubject.create();
356 PublishSubject<Integer> other = PublishSubject.create();
357
358 TestObserver<Integer> to = main.takeUntil(other).test();
359
360 assertTrue("Main no observers?", main.hasObservers());
361 assertTrue("Other no observers?", other.hasObservers());
362
363 other.onComplete();
364
365 assertFalse("Main has observers?", main.hasObservers());
366 assertFalse("Other has observers?", other.hasObservers());
367
368 to.assertResult();
369 }
370
371 @Test
372 public void untilPublisherOtherError() {
373 PublishSubject<Integer> main = PublishSubject.create();
374 PublishSubject<Integer> other = PublishSubject.create();
375
376 TestObserver<Integer> to = main.takeUntil(other).test();
377
378 assertTrue("Main no observers?", main.hasObservers());
379 assertTrue("Other no observers?", other.hasObservers());
380
381 other.onError(new TestException());
382
383 assertFalse("Main has observers?", main.hasObservers());
384 assertFalse("Other has observers?", other.hasObservers());
385
386 to.assertFailure(TestException.class);
387 }
388
389 @Test
390 public void untilPublisherDispose() {
391 PublishSubject<Integer> main = PublishSubject.create();
392 PublishSubject<Integer> other = PublishSubject.create();
393
394 TestObserver<Integer> to = main.takeUntil(other).test();
395
396 assertTrue("Main no observers?", main.hasObservers());
397 assertTrue("Other no observers?", other.hasObservers());
398
399 to.dispose();
400
401 assertFalse("Main has observers?", main.hasObservers());
402 assertFalse("Other has observers?", other.hasObservers());
403
404 to.assertEmpty();
405 }
406
407 }