Debian Codegraph @

src/test/java/io/reactivex/rxjava3/internal/operators/observable/ObservableWithLatestFromTest.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.ArgumentMatchers.*;
18 import static org.mockito.Mockito.*;
19
20 import java.util.*;
21
22 import io.reactivex.rxjava3.disposables.Disposable;
23 import org.junit.Test;
24 import org.mockito.InOrder;
25
26 import io.reactivex.rxjava3.core.Observable;
27 import io.reactivex.rxjava3.core.Observer;
28 import io.reactivex.rxjava3.core.RxJavaTest;
29 import io.reactivex.rxjava3.exceptions.TestException;
30 import io.reactivex.rxjava3.functions.*;
31 import io.reactivex.rxjava3.internal.functions.Functions;
32 import io.reactivex.rxjava3.internal.util.CrashingMappedIterable;
33 import io.reactivex.rxjava3.observers.TestObserver;
34 import io.reactivex.rxjava3.plugins.RxJavaPlugins;
35 import io.reactivex.rxjava3.subjects.PublishSubject;
36 import io.reactivex.rxjava3.testsupport.*;
37
38 public class ObservableWithLatestFromTest extends RxJavaTest {
39 static final BiFunction<Integer, Integer, Integer> COMBINER = new BiFunction<Integer, Integer, Integer>() {
40 @Override
41 public Integer apply(Integer t1, Integer t2) {
42 return (t1 << 8) + t2;
43 }
44 };
45 static final BiFunction<Integer, Integer, Integer> COMBINER_ERROR = new BiFunction<Integer, Integer, Integer>() {
46 @Override
47 public Integer apply(Integer t1, Integer t2) {
48 throw new TestException("Forced failure");
49 }
50 };
51 @Test
52 public void simple() {
53 PublishSubject<Integer> source = PublishSubject.create();
54 PublishSubject<Integer> other = PublishSubject.create();
55
56 Observer<Integer> o = TestHelper.mockObserver();
57 InOrder inOrder = inOrder(o);
58
59 Observable<Integer> result = source.withLatestFrom(other, COMBINER);
60
61 result.subscribe(o);
62
63 source.onNext(1);
64 inOrder.verify(o, never()).onNext(anyInt());
65
66 other.onNext(1);
67 inOrder.verify(o, never()).onNext(anyInt());
68
69 source.onNext(2);
70 inOrder.verify(o).onNext((2 << 8) + 1);
71
72 other.onNext(2);
73 inOrder.verify(o, never()).onNext(anyInt());
74
75 other.onComplete();
76 inOrder.verify(o, never()).onComplete();
77
78 source.onNext(3);
79 inOrder.verify(o).onNext((3 << 8) + 2);
80
81 source.onComplete();
82 inOrder.verify(o).onComplete();
83
84 verify(o, never()).onError(any(Throwable.class));
85 }
86
87 @Test
88 public void emptySource() {
89 PublishSubject<Integer> source = PublishSubject.create();
90 PublishSubject<Integer> other = PublishSubject.create();
91
92 Observable<Integer> result = source.withLatestFrom(other, COMBINER);
93
94 TestObserverEx<Integer> to = new TestObserverEx<>();
95
96 result.subscribe(to);
97
98 assertTrue(source.hasObservers());
99 assertTrue(other.hasObservers());
100
101 other.onNext(1);
102
103 source.onComplete();
104
105 to.assertNoErrors();
106 to.assertTerminated();
107 to.assertNoValues();
108
109 assertFalse(source.hasObservers());
110 assertFalse(other.hasObservers());
111 }
112
113 @Test
114 public void emptyOther() {
115 PublishSubject<Integer> source = PublishSubject.create();
116 PublishSubject<Integer> other = PublishSubject.create();
117
118 Observable<Integer> result = source.withLatestFrom(other, COMBINER);
119
120 TestObserverEx<Integer> to = new TestObserverEx<>();
121
122 result.subscribe(to);
123
124 assertTrue(source.hasObservers());
125 assertTrue(other.hasObservers());
126
127 source.onNext(1);
128
129 source.onComplete();
130
131 to.assertNoErrors();
132 to.assertTerminated();
133 to.assertNoValues();
134
135 assertFalse(source.hasObservers());
136 assertFalse(other.hasObservers());
137 }
138
139 @Test
140 public void unsubscription() {
141 PublishSubject<Integer> source = PublishSubject.create();
142 PublishSubject<Integer> other = PublishSubject.create();
143
144 Observable<Integer> result = source.withLatestFrom(other, COMBINER);
145
146 TestObserver<Integer> to = new TestObserver<>();
147
148 result.subscribe(to);
149
150 assertTrue(source.hasObservers());
151 assertTrue(other.hasObservers());
152
153 other.onNext(1);
154 source.onNext(1);
155
156 to.dispose();
157
158 to.assertValue((1 << 8) + 1);
159 to.assertNoErrors();
160 to.assertNotComplete();
161
162 assertFalse(source.hasObservers());
163 assertFalse(other.hasObservers());
164 }
165
166 @Test
167 public void sourceThrows() {
168 PublishSubject<Integer> source = PublishSubject.create();
169 PublishSubject<Integer> other = PublishSubject.create();
170
171 Observable<Integer> result = source.withLatestFrom(other, COMBINER);
172
173 TestObserverEx<Integer> to = new TestObserverEx<>();
174
175 result.subscribe(to);
176
177 assertTrue(source.hasObservers());
178 assertTrue(other.hasObservers());
179
180 other.onNext(1);
181 source.onNext(1);
182
183 source.onError(new TestException());
184
185 to.assertTerminated();
186 to.assertValue((1 << 8) + 1);
187 to.assertError(TestException.class);
188 to.assertNotComplete();
189
190 assertFalse(source.hasObservers());
191 assertFalse(other.hasObservers());
192 }
193
194 @Test
195 public void otherThrows() {
196 PublishSubject<Integer> source = PublishSubject.create();
197 PublishSubject<Integer> other = PublishSubject.create();
198
199 Observable<Integer> result = source.withLatestFrom(other, COMBINER);
200
201 TestObserverEx<Integer> to = new TestObserverEx<>();
202
203 result.subscribe(to);
204
205 assertTrue(source.hasObservers());
206 assertTrue(other.hasObservers());
207
208 other.onNext(1);
209 source.onNext(1);
210
211 other.onError(new TestException());
212
213 to.assertTerminated();
214 to.assertValue((1 << 8) + 1);
215 to.assertNotComplete();
216 to.assertError(TestException.class);
217
218 assertFalse(source.hasObservers());
219 assertFalse(other.hasObservers());
220 }
221
222 @Test
223 public void functionThrows() {
224 PublishSubject<Integer> source = PublishSubject.create();
225 PublishSubject<Integer> other = PublishSubject.create();
226
227 Observable<Integer> result = source.withLatestFrom(other, COMBINER_ERROR);
228
229 TestObserverEx<Integer> to = new TestObserverEx<>();
230
231 result.subscribe(to);
232
233 assertTrue(source.hasObservers());
234 assertTrue(other.hasObservers());
235
236 other.onNext(1);
237 source.onNext(1);
238
239 to.assertTerminated();
240 to.assertNotComplete();
241 to.assertNoValues();
242 to.assertError(TestException.class);
243
244 assertFalse(source.hasObservers());
245 assertFalse(other.hasObservers());
246 }
247
248 @Test
249 public void noDownstreamUnsubscribe() {
250 PublishSubject<Integer> source = PublishSubject.create();
251 PublishSubject<Integer> other = PublishSubject.create();
252
253 Observable<Integer> result = source.withLatestFrom(other, COMBINER);
254
255 TestObserver<Integer> to = new TestObserver<>();
256
257 result.subscribe(to);
258
259 source.onComplete();
260
261 // 2.0.2 - not anymore
262 // assertTrue("Not cancelled!", ts.isCancelled());
263 }
264
265 static final Function<Object[], String> toArray = new Function<Object[], String>() {
266 @Override
267 public String apply(Object[] args) {
268 return Arrays.toString(args);
269 }
270 };
271
272 @Test
273 public void manySources() {
274 PublishSubject<String> ps1 = PublishSubject.create();
275 PublishSubject<String> ps2 = PublishSubject.create();
276 PublishSubject<String> ps3 = PublishSubject.create();
277 PublishSubject<String> main = PublishSubject.create();
278
279 TestObserver<String> to = new TestObserver<>();
280
281 main.withLatestFrom(new Observable[] { ps1, ps2, ps3 }, toArray)
282 .subscribe(to);
283
284 main.onNext("1");
285 to.assertNoValues();
286 ps1.onNext("a");
287 to.assertNoValues();
288 ps2.onNext("A");
289 to.assertNoValues();
290 ps3.onNext("=");
291 to.assertNoValues();
292
293 main.onNext("2");
294 to.assertValues("[2, a, A, =]");
295
296 ps2.onNext("B");
297
298 to.assertValues("[2, a, A, =]");
299
300 ps3.onComplete();
301 to.assertValues("[2, a, A, =]");
302
303 ps1.onNext("b");
304
305 main.onNext("3");
306
307 to.assertValues("[2, a, A, =]", "[3, b, B, =]");
308
309 main.onComplete();
310 to.assertValues("[2, a, A, =]", "[3, b, B, =]");
311 to.assertNoErrors();
312 to.assertComplete();
313
314 assertFalse("ps1 has subscribers?", ps1.hasObservers());
315 assertFalse("ps2 has subscribers?", ps2.hasObservers());
316 assertFalse("ps3 has subscribers?", ps3.hasObservers());
317 }
318
319 @Test
320 public void manySourcesIterable() {
321 PublishSubject<String> ps1 = PublishSubject.create();
322 PublishSubject<String> ps2 = PublishSubject.create();
323 PublishSubject<String> ps3 = PublishSubject.create();
324 PublishSubject<String> main = PublishSubject.create();
325
326 TestObserver<String> to = new TestObserver<>();
327
328 main.withLatestFrom(Arrays.<Observable<?>>asList(ps1, ps2, ps3), toArray)
329 .subscribe(to);
330
331 main.onNext("1");
332 to.assertNoValues();
333 ps1.onNext("a");
334 to.assertNoValues();
335 ps2.onNext("A");
336 to.assertNoValues();
337 ps3.onNext("=");
338 to.assertNoValues();
339
340 main.onNext("2");
341 to.assertValues("[2, a, A, =]");
342
343 ps2.onNext("B");
344
345 to.assertValues("[2, a, A, =]");
346
347 ps3.onComplete();
348 to.assertValues("[2, a, A, =]");
349
350 ps1.onNext("b");
351
352 main.onNext("3");
353
354 to.assertValues("[2, a, A, =]", "[3, b, B, =]");
355
356 main.onComplete();
357 to.assertValues("[2, a, A, =]", "[3, b, B, =]");
358 to.assertNoErrors();
359 to.assertComplete();
360
361 assertFalse("ps1 has subscribers?", ps1.hasObservers());
362 assertFalse("ps2 has subscribers?", ps2.hasObservers());
363 assertFalse("ps3 has subscribers?", ps3.hasObservers());
364 }
365
366 @Test
367 public void manySourcesIterableSweep() {
368 for (String val : new String[] { "1" /*, null*/ }) {
369 int n = 35;
370 for (int i = 0; i < n; i++) {
371 List<Observable<?>> sources = new ArrayList<>();
372 List<String> expected = new ArrayList<>();
373 expected.add(val);
374
375 for (int j = 0; j < i; j++) {
376 sources.add(Observable.just(val));
377 expected.add(String.valueOf(val));
378 }
379
380 TestObserver<String> to = new TestObserver<>();
381
382 PublishSubject<String> main = PublishSubject.create();
383
384 main.withLatestFrom(sources, toArray).subscribe(to);
385
386 to.assertNoValues();
387
388 main.onNext(val);
389 main.onComplete();
390
391 to.assertValue(expected.toString());
392 to.assertNoErrors();
393 to.assertComplete();
394 }
395 }
396 }
397
398 @Test
399 public void withEmpty() {
400 TestObserver<String> to = new TestObserver<>();
401
402 Observable.range(1, 3).withLatestFrom(
403 new Observable<?>[] { Observable.just(1), Observable.empty() }, toArray)
404 .subscribe(to);
405
406 to.assertNoValues();
407 to.assertNoErrors();
408 to.assertComplete();
409 }
410
411 @Test
412 public void withError() {
413 TestObserver<String> to = new TestObserver<>();
414
415 Observable.range(1, 3).withLatestFrom(
416 new Observable<?>[] { Observable.just(1), Observable.error(new TestException()) }, toArray)
417 .subscribe(to);
418
419 to.assertNoValues();
420 to.assertError(TestException.class);
421 to.assertNotComplete();
422 }
423
424 @Test
425 public void withMainError() {
426 TestObserver<String> to = new TestObserver<>();
427
428 Observable.error(new TestException()).withLatestFrom(
429 new Observable<?>[] { Observable.just(1), Observable.just(1) }, toArray)
430 .subscribe(to);
431
432 to.assertNoValues();
433 to.assertError(TestException.class);
434 to.assertNotComplete();
435 }
436
437 @Test
438 public void with2Others() {
439 Observable<Integer> just = Observable.just(1);
440
441 TestObserver<List<Integer>> to = new TestObserver<>();
442
443 just.withLatestFrom(just, just, new Function3<Integer, Integer, Integer, List<Integer>>() {
444 @Override
445 public List<Integer> apply(Integer a, Integer b, Integer c) {
446 return Arrays.asList(a, b, c);
447 }
448 })
449 .subscribe(to);
450
451 to.assertValue(Arrays.asList(1, 1, 1));
452 to.assertNoErrors();
453 to.assertComplete();
454 }
455
456 @Test
457 public void with3Others() {
458 Observable<Integer> just = Observable.just(1);
459
460 TestObserver<List<Integer>> to = new TestObserver<>();
461
462 just.withLatestFrom(just, just, just, new Function4<Integer, Integer, Integer, Integer, List<Integer>>() {
463 @Override
464 public List<Integer> apply(Integer a, Integer b, Integer c, Integer d) {
465 return Arrays.asList(a, b, c, d);
466 }
467 })
468 .subscribe(to);
469
470 to.assertValue(Arrays.asList(1, 1, 1, 1));
471 to.assertNoErrors();
472 to.assertComplete();
473 }
474
475 @Test
476 public void with4Others() {
477 Observable<Integer> just = Observable.just(1);
478
479 TestObserver<List<Integer>> to = new TestObserver<>();
480
481 just.withLatestFrom(just, just, just, just, new Function5<Integer, Integer, Integer, Integer, Integer, List<Integer>>() {
482 @Override
483 public List<Integer> apply(Integer a, Integer b, Integer c, Integer d, Integer e) {
484 return Arrays.asList(a, b, c, d, e);
485 }
486 })
487 .subscribe(to);
488
489 to.assertValue(Arrays.asList(1, 1, 1, 1, 1));
490 to.assertNoErrors();
491 to.assertComplete();
492 }
493
494 @Test
495 public void dispose() {
496 TestHelper.checkDisposed(Observable.just(1).withLatestFrom(Observable.just(2), new BiFunction<Integer, Integer, Object>() {
497 @Override
498 public Object apply(Integer a, Integer b) throws Exception {
499 return a;
500 }
501 }));
502
503 TestHelper.checkDisposed(Observable.just(1).withLatestFrom(Observable.just(2), Observable.just(3), new Function3<Integer, Integer, Integer, Object>() {
504 @Override
505 public Object apply(Integer a, Integer b, Integer c) throws Exception {
506 return a;
507 }
508 }));
509 }
510
511 @Test
512 public void manyIteratorThrows() {
513 Observable.just(1)
514 .withLatestFrom(new CrashingMappedIterable<>(1, 100, 100, new Function<Integer, Observable<Integer>>() {
515 @Override
516 public Observable<Integer> apply(Integer v) throws Exception {
517 return Observable.just(2);
518 }
519 }), new Function<Object[], Object>() {
520 @Override
521 public Object apply(Object[] a) throws Exception {
522 return a;
523 }
524 })
525 .to(TestHelper.<Object>testConsumer())
526 .assertFailureAndMessage(TestException.class, "iterator()");
527 }
528
529 @Test
530 public void manyCombinerThrows() {
531 Observable.just(1).withLatestFrom(Observable.just(2), Observable.just(3), new Function3<Integer, Integer, Integer, Object>() {
532 @Override
533 public Object apply(Integer a, Integer b, Integer c) throws Exception {
534 throw new TestException();
535 }
536 })
537 .test()
538 .assertFailure(TestException.class);
539 }
540
541 @Test
542 public void manyErrors() {
543 List<Throwable> errors = TestHelper.trackPluginErrors();
544 try {
545 new Observable<Integer>() {
546 @Override
547 protected void subscribeActual(Observer<? super Integer> observer) {
548 observer.onSubscribe(Disposable.empty());
549 observer.onError(new TestException("First"));
550 observer.onNext(1);
551 observer.onError(new TestException("Second"));
552 observer.onComplete();
553 }
554 }.withLatestFrom(Observable.just(2), Observable.just(3), new Function3<Integer, Integer, Integer, Object>() {
555 @Override
556 public Object apply(Integer a, Integer b, Integer c) throws Exception {
557 return a;
558 }
559 })
560 .to(TestHelper.<Object>testConsumer())
561 .assertFailureAndMessage(TestException.class, "First");
562
563 TestHelper.assertUndeliverable(errors, 0, TestException.class, "Second");
564 } finally {
565 RxJavaPlugins.reset();
566 }
567 }
568
569 @Test
570 public void combineToNull1() {
571 Observable.just(1)
572 .withLatestFrom(Observable.just(2), new BiFunction<Integer, Integer, Object>() {
573 @Override
574 public Object apply(Integer a, Integer b) throws Exception {
575 return null;
576 }
577 })
578 .test()
579 .assertFailure(NullPointerException.class);
580 }
581
582 @Test
583 public void combineToNull2() {
584 Observable.just(1)
585 .withLatestFrom(Arrays.asList(Observable.just(2), Observable.just(3)), new Function<Object[], Object>() {
586 @Override
587 public Object apply(Object[] o) throws Exception {
588 return null;
589 }
590 })
591 .test()
592 .assertFailure(NullPointerException.class);
593 }
594
595 @Test
596 public void zeroOtherCombinerReturnsNull() {
597 Observable.just(1)
598 .withLatestFrom(new Observable[0], Functions.justFunction(null))
599 .to(TestHelper.<Object>testConsumer())
600 .assertFailureAndMessage(NullPointerException.class, "The combiner returned a null value");
601 }
602 }