Debian Codegraph @

src/test/java/io/reactivex/rxjava3/flowable/FlowableTests.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.flowable;
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 import java.util.concurrent.*;
22 import java.util.concurrent.atomic.*;
23
24 import org.junit.*;
25 import org.mockito.InOrder;
26 import org.reactivestreams.*;
27
28 import io.reactivex.rxjava3.core.*;
29 import io.reactivex.rxjava3.core.Observable;
30 import io.reactivex.rxjava3.disposables.Disposable;
31 import io.reactivex.rxjava3.exceptions.*;
32 import io.reactivex.rxjava3.flowables.ConnectableFlowable;
33 import io.reactivex.rxjava3.functions.*;
34 import io.reactivex.rxjava3.internal.functions.Functions;
35 import io.reactivex.rxjava3.internal.subscriptions.BooleanSubscription;
36 import io.reactivex.rxjava3.plugins.RxJavaPlugins;
37 import io.reactivex.rxjava3.processors.*;
38 import io.reactivex.rxjava3.schedulers.*;
39 import io.reactivex.rxjava3.subscribers.*;
40 import io.reactivex.rxjava3.testsupport.*;
41
42 public class FlowableTests extends RxJavaTest {
43
44 Subscriber<Number> w;
45
46 SingleObserver<Number> wo;
47
48 MaybeObserver<Number> wm;
49
50 private static final Predicate<Integer> IS_EVEN = new Predicate<Integer>() {
51 @Override
52 public boolean test(Integer v) {
53 return v % 2 == 0;
54 }
55 };
56
57 @Before
58 public void before() {
59 w = TestHelper.mockSubscriber();
60 wo = TestHelper.mockSingleObserver();
61 wm = TestHelper.mockMaybeObserver();
62 }
63
64 @Test
65 public void fromArray() {
66 String[] items = new String[] { "one", "two", "three" };
67 assertEquals((Long)3L, Flowable.fromArray(items).count().blockingGet());
68 assertEquals("two", Flowable.fromArray(items).skip(1).take(1).blockingSingle());
69 assertEquals("three", Flowable.fromArray(items).takeLast(1).blockingSingle());
70 }
71
72 @Test
73 public void fromIterable() {
74 ArrayList<String> items = new ArrayList<>();
75 items.add("one");
76 items.add("two");
77 items.add("three");
78
79 assertEquals((Long)3L, Flowable.fromIterable(items).count().blockingGet());
80 assertEquals("two", Flowable.fromIterable(items).skip(1).take(1).blockingSingle());
81 assertEquals("three", Flowable.fromIterable(items).takeLast(1).blockingSingle());
82 }
83
84 @Test
85 public void fromArityArgs3() {
86 Flowable<String> items = Flowable.just("one", "two", "three");
87
88 assertEquals((Long)3L, items.count().blockingGet());
89 assertEquals("two", items.skip(1).take(1).blockingSingle());
90 assertEquals("three", items.takeLast(1).blockingSingle());
91 }
92
93 @Test
94 public void fromArityArgs1() {
95 Flowable<String> items = Flowable.just("one");
96
97 assertEquals((Long)1L, items.count().blockingGet());
98 assertEquals("one", items.takeLast(1).blockingSingle());
99 }
100
101 @Test
102 public void create() {
103
104 Flowable<String> flowable = Flowable.just("one", "two", "three");
105
106 Subscriber<String> subscriber = TestHelper.mockSubscriber();
107
108 flowable.subscribe(subscriber);
109
110 verify(subscriber, times(1)).onNext("one");
111 verify(subscriber, times(1)).onNext("two");
112 verify(subscriber, times(1)).onNext("three");
113 verify(subscriber, never()).onError(any(Throwable.class));
114 verify(subscriber, times(1)).onComplete();
115 }
116
117 @Test
118 public void countAFewItemsFlowable() {
119 Flowable<String> flowable = Flowable.just("a", "b", "c", "d");
120
121 flowable.count().toFlowable().subscribe(w);
122
123 // we should be called only once
124 verify(w).onNext(4L);
125 verify(w, never()).onError(any(Throwable.class));
126 verify(w, times(1)).onComplete();
127 }
128
129 @Test
130 public void countZeroItemsFlowable() {
131 Flowable<String> flowable = Flowable.empty();
132 flowable.count().toFlowable().subscribe(w);
133 // we should be called only once
134 verify(w).onNext(0L);
135 verify(w, never()).onError(any(Throwable.class));
136 verify(w, times(1)).onComplete();
137 }
138
139 @Test
140 public void countErrorFlowable() {
141 Flowable<String> f = Flowable.error(new Supplier<Throwable>() {
142 @Override
143 public Throwable get() {
144 return new RuntimeException();
145 }
146 });
147
148 f.count().toFlowable().subscribe(w);
149 verify(w, never()).onNext(anyInt());
150 verify(w, never()).onComplete();
151 verify(w, times(1)).onError(any(RuntimeException.class));
152 }
153
154 @Test
155 public void countAFewItems() {
156 Flowable<String> flowable = Flowable.just("a", "b", "c", "d");
157
158 flowable.count().subscribe(wo);
159
160 // we should be called only once
161 verify(wo).onSuccess(4L);
162 verify(wo, never()).onError(any(Throwable.class));
163 }
164
165 @Test
166 public void countZeroItems() {
167 Flowable<String> flowable = Flowable.empty();
168 flowable.count().subscribe(wo);
169 // we should be called only once
170 verify(wo).onSuccess(0L);
171 verify(wo, never()).onError(any(Throwable.class));
172 }
173
174 @Test
175 public void countError() {
176 Flowable<String> f = Flowable.error(new Supplier<Throwable>() {
177 @Override
178 public Throwable get() {
179 return new RuntimeException();
180 }
181 });
182
183 f.count().subscribe(wo);
184 verify(wo, never()).onSuccess(anyInt());
185 verify(wo, times(1)).onError(any(RuntimeException.class));
186 }
187
188 @Test
189 public void takeFirstWithPredicateOfSome() {
190 Flowable<Integer> flowable = Flowable.just(1, 3, 5, 4, 6, 3);
191 flowable.filter(IS_EVEN).take(1).subscribe(w);
192 verify(w, times(1)).onNext(anyInt());
193 verify(w).onNext(4);
194 verify(w, times(1)).onComplete();
195 verify(w, never()).onError(any(Throwable.class));
196 }
197
198 @Test
199 public void takeFirstWithPredicateOfNoneMatchingThePredicate() {
200 Flowable<Integer> flowable = Flowable.just(1, 3, 5, 7, 9, 7, 5, 3, 1);
201 flowable.filter(IS_EVEN).take(1).subscribe(w);
202 verify(w, never()).onNext(anyInt());
203 verify(w, times(1)).onComplete();
204 verify(w, never()).onError(any(Throwable.class));
205 }
206
207 @Test
208 public void takeFirstOfSome() {
209 Flowable<Integer> flowable = Flowable.just(1, 2, 3);
210 flowable.take(1).subscribe(w);
211 verify(w, times(1)).onNext(anyInt());
212 verify(w).onNext(1);
213 verify(w, times(1)).onComplete();
214 verify(w, never()).onError(any(Throwable.class));
215 }
216
217 @Test
218 public void takeFirstOfNone() {
219 Flowable<Integer> flowable = Flowable.empty();
220 flowable.take(1).subscribe(w);
221 verify(w, never()).onNext(anyInt());
222 verify(w, times(1)).onComplete();
223 verify(w, never()).onError(any(Throwable.class));
224 }
225
226 @Test
227 public void firstOfNoneFlowable() {
228 Flowable<Integer> flowable = Flowable.empty();
229 flowable.firstElement().toFlowable().subscribe(w);
230 verify(w, never()).onNext(anyInt());
231 verify(w).onComplete();
232 verify(w, never()).onError(any(Throwable.class));
233 }
234
235 @Test
236 public void firstWithPredicateOfNoneMatchingThePredicateFlowable() {
237 Flowable<Integer> flowable = Flowable.just(1, 3, 5, 7, 9, 7, 5, 3, 1);
238 flowable.filter(IS_EVEN).firstElement().toFlowable().subscribe(w);
239 verify(w, never()).onNext(anyInt());
240 verify(w).onComplete();
241 verify(w, never()).onError(any(Throwable.class));
242 }
243
244 @Test
245 public void firstOfNone() {
246 Flowable<Integer> flowable = Flowable.empty();
247 flowable.firstElement().subscribe(wm);
248 verify(wm, never()).onSuccess(anyInt());
249 verify(wm).onComplete();
250 verify(wm, never()).onError(isA(NoSuchElementException.class));
251 }
252
253 @Test
254 public void firstWithPredicateOfNoneMatchingThePredicate() {
255 Flowable<Integer> flowable = Flowable.just(1, 3, 5, 7, 9, 7, 5, 3, 1);
256 flowable.filter(IS_EVEN).firstElement().subscribe(wm);
257 verify(wm, never()).onSuccess(anyInt());
258 verify(wm, times(1)).onComplete();
259 verify(wm, never()).onError(isA(NoSuchElementException.class));
260 }
261
262 @Test
263 public void reduce() {
264 Flowable<Integer> flowable = Flowable.just(1, 2, 3, 4);
265 flowable.reduce(new BiFunction<Integer, Integer, Integer>() {
266 @Override
267 public Integer apply(Integer t1, Integer t2) {
268 return t1 + t2;
269 }
270 })
271 .toFlowable()
272 .subscribe(w);
273 // we should be called only once
274 verify(w, times(1)).onNext(anyInt());
275 verify(w).onNext(10);
276 }
277
278 @Test
279 public void reduceWithEmptyObservable() {
280 Flowable<Integer> flowable = Flowable.range(1, 0);
281 flowable.reduce(new BiFunction<Integer, Integer, Integer>() {
282 @Override
283 public Integer apply(Integer t1, Integer t2) {
284 return t1 + t2;
285 }
286 })
287 .toFlowable()
288 .test()
289 .assertResult();
290 }
291
292 /**
293 * A reduce on an empty Observable and a seed should just pass the seed through.
294 *
295 * This is confirmed at https://github.com/ReactiveX/RxJava/issues/423#issuecomment-27642456
296 */
297 @Test
298 public void reduceWithEmptyObservableAndSeed() {
299 Flowable<Integer> flowable = Flowable.range(1, 0);
300 int value = flowable.reduce(1, new BiFunction<Integer, Integer, Integer>() {
301 @Override
302 public Integer apply(Integer t1, Integer t2) {
303 return t1 + t2;
304 }
305 })
306 .blockingGet();
307
308 assertEquals(1, value);
309 }
310
311 @Test
312 public void reduceWithInitialValue() {
313 Flowable<Integer> flowable = Flowable.just(1, 2, 3, 4);
314 flowable.reduce(50, new BiFunction<Integer, Integer, Integer>() {
315 @Override
316 public Integer apply(Integer t1, Integer t2) {
317 return t1 + t2;
318 }
319 })
320 .subscribe(wo);
321 // we should be called only once
322 verify(wo, times(1)).onSuccess(anyInt());
323 verify(wo).onSuccess(60);
324 }
325
326 @Test
327 public void materializeDematerializeChaining() {
328 Flowable<Integer> obs = Flowable.just(1);
329 Flowable<Integer> chained = obs.materialize()
330 .dematerialize(Functions.<Notification<Integer>>identity());
331
332 Subscriber<Integer> subscriber = TestHelper.mockSubscriber();
333
334 chained.subscribe(subscriber);
335
336 verify(subscriber, times(1)).onNext(1);
337 verify(subscriber, times(1)).onComplete();
338 verify(subscriber, times(0)).onError(any(Throwable.class));
339 }
340
341 /**
342 * The error from the user provided Observer is not handled by the subscribe method try/catch.
343 *
344 * It is handled by the AtomicObserver that wraps the provided Observer.
345 *
346 * Result: Passes (if AtomicObserver functionality exists)
347 * @throws InterruptedException if the test is interrupted
348 */
349 @Test
350 public void customObservableWithErrorInObserverAsynchronous() throws InterruptedException {
351 final CountDownLatch latch = new CountDownLatch(1);
352 final AtomicInteger count = new AtomicInteger();
353 final AtomicReference<Throwable> error = new AtomicReference<>();
354
355 // FIXME custom built???
356 Flowable.just("1", "2", "three", "4")
357 .subscribeOn(Schedulers.newThread())
358 .safeSubscribe(new DefaultSubscriber<String>() {
359 @Override
360 public void onComplete() {
361 System.out.println("completed");
362 latch.countDown();
363 }
364
365 @Override
366 public void onError(Throwable e) {
367 error.set(e);
368 System.out.println("error");
369 e.printStackTrace();
370 latch.countDown();
371 }
372
373 @Override
374 public void onNext(String v) {
375 int num = Integer.parseInt(v);
376 System.out.println(num);
377 // doSomething(num);
378 count.incrementAndGet();
379 }
380
381 });
382
383 // wait for async sequence to complete
384 latch.await();
385
386 assertEquals(2, count.get());
387 assertNotNull(error.get());
388 if (!(error.get() instanceof NumberFormatException)) {
389 fail("It should be a NumberFormatException");
390 }
391 }
392
393 /**
394 * The error from the user provided Observer is handled by the subscribe try/catch because this is synchronous.
395 *
396 * Result: Passes
397 */
398 @Test
399 public void customObservableWithErrorInObserverSynchronous() {
400 final AtomicInteger count = new AtomicInteger();
401 final AtomicReference<Throwable> error = new AtomicReference<>();
402
403 // FIXME custom built???
404 Flowable.just("1", "2", "three", "4")
405 .safeSubscribe(new DefaultSubscriber<String>() {
406
407 @Override
408 public void onComplete() {
409 System.out.println("completed");
410 }
411
412 @Override
413 public void onError(Throwable e) {
414 error.set(e);
415 System.out.println("error");
416 e.printStackTrace();
417 }
418
419 @Override
420 public void onNext(String v) {
421 int num = Integer.parseInt(v);
422 System.out.println(num);
423 // doSomething(num);
424 count.incrementAndGet();
425 }
426
427 });
428 assertEquals(2, count.get());
429 assertNotNull(error.get());
430 if (!(error.get() instanceof NumberFormatException)) {
431 fail("It should be a NumberFormatException");
432 }
433 }
434
435 /**
436 * The error from the user provided Observable is handled by the subscribe try/catch because this is synchronous.
437 *
438 * Result: Passes
439 */
440 @Test
441 public void customObservableWithErrorInObservableSynchronous() {
442 final AtomicInteger count = new AtomicInteger();
443 final AtomicReference<Throwable> error = new AtomicReference<>();
444 // FIXME custom built???
445 Flowable.just("1", "2").concatWith(Flowable.<String>error(new Supplier<Throwable>() {
446 @Override
447 public Throwable get() {
448 return new NumberFormatException();
449 }
450 }))
451 .subscribe(new DefaultSubscriber<String>() {
452
453 @Override
454 public void onComplete() {
455 System.out.println("completed");
456 }
457
458 @Override
459 public void onError(Throwable e) {
460 error.set(e);
461 System.out.println("error");
462 e.printStackTrace();
463 }
464
465 @Override
466 public void onNext(String v) {
467 System.out.println(v);
468 count.incrementAndGet();
469 }
470
471 });
472 assertEquals(2, count.get());
473 assertNotNull(error.get());
474 if (!(error.get() instanceof NumberFormatException)) {
475 fail("It should be a NumberFormatException");
476 }
477 }
478
479 @Test
480 public void publishLast() throws InterruptedException {
481 final AtomicInteger count = new AtomicInteger();
482 ConnectableFlowable<String> connectable = Flowable.<String>unsafeCreate(new Publisher<String>() {
483 @Override
484 public void subscribe(final Subscriber<? super String> subscriber) {
485 subscriber.onSubscribe(new BooleanSubscription());
486 count.incrementAndGet();
487 new Thread(new Runnable() {
488 @Override
489 public void run() {
490 subscriber.onNext("first");
491 subscriber.onNext("last");
492 subscriber.onComplete();
493 }
494 }).start();
495 }
496 }).takeLast(1).publish();
497
498 // subscribe once
499 final CountDownLatch latch = new CountDownLatch(1);
500 connectable.subscribe(new Consumer<String>() {
501 @Override
502 public void accept(String value) {
503 assertEquals("last", value);
504 latch.countDown();
505 }
506 });
507
508 // subscribe twice
509 connectable.subscribe();
510
511 Disposable subscription = connectable.connect();
512 assertTrue(latch.await(1000, TimeUnit.MILLISECONDS));
513 assertEquals(1, count.get());
514 subscription.dispose();
515 }
516
517 @Test
518 public void replay() throws InterruptedException {
519 final AtomicInteger counter = new AtomicInteger();
520 ConnectableFlowable<String> f = Flowable.<String>unsafeCreate(new Publisher<String>() {
521 @Override
522 public void subscribe(final Subscriber<? super String> subscriber) {
523 subscriber.onSubscribe(new BooleanSubscription());
524 new Thread(new Runnable() {
525
526 @Override
527 public void run() {
528 counter.incrementAndGet();
529 subscriber.onNext("one");
530 subscriber.onComplete();
531 }
532 }).start();
533 }
534 }).replay();
535
536 // we connect immediately and it will emit the value
537 Disposable connection = f.connect();
538 try {
539
540 // we then expect the following 2 subscriptions to get that same value
541 final CountDownLatch latch = new CountDownLatch(2);
542
543 // subscribe once
544 f.subscribe(new Consumer<String>() {
545 @Override
546 public void accept(String v) {
547 assertEquals("one", v);
548 latch.countDown();
549 }
550 });
551
552 // subscribe again
553 f.subscribe(new Consumer<String>() {
554 @Override
555 public void accept(String v) {
556 assertEquals("one", v);
557 latch.countDown();
558 }
559 });
560
561 if (!latch.await(1000, TimeUnit.MILLISECONDS)) {
562 fail("subscriptions did not receive values");
563 }
564 assertEquals(1, counter.get());
565 } finally {
566 connection.dispose();
567 }
568 }
569
570 @Test
571 public void cache() throws InterruptedException {
572 final AtomicInteger counter = new AtomicInteger();
573 Flowable<String> f = Flowable.<String>unsafeCreate(new Publisher<String>() {
574 @Override
575 public void subscribe(final Subscriber<? super String> subscriber) {
576 subscriber.onSubscribe(new BooleanSubscription());
577 new Thread(new Runnable() {
578 @Override
579 public void run() {
580 counter.incrementAndGet();
581 subscriber.onNext("one");
582 subscriber.onComplete();
583 }
584 }).start();
585 }
586 }).cache();
587
588 // we then expect the following 2 subscriptions to get that same value
589 final CountDownLatch latch = new CountDownLatch(2);
590
591 // subscribe once
592 f.subscribe(new Consumer<String>() {
593 @Override
594 public void accept(String v) {
595 assertEquals("one", v);
596 latch.countDown();
597 }
598 });
599
600 // subscribe again
601 f.subscribe(new Consumer<String>() {
602 @Override
603 public void accept(String v) {
604 assertEquals("one", v);
605 latch.countDown();
606 }
607 });
608
609 if (!latch.await(1000, TimeUnit.MILLISECONDS)) {
610 fail("subscriptions did not receive values");
611 }
612 assertEquals(1, counter.get());
613 }
614
615 @Test
616 public void cacheWithCapacity() throws InterruptedException {
617 final AtomicInteger counter = new AtomicInteger();
618 Flowable<String> f = Flowable.<String>unsafeCreate(new Publisher<String>() {
619 @Override
620 public void subscribe(final Subscriber<? super String> subscriber) {
621 subscriber.onSubscribe(new BooleanSubscription());
622 new Thread(new Runnable() {
623 @Override
624 public void run() {
625 counter.incrementAndGet();
626 subscriber.onNext("one");
627 subscriber.onComplete();
628 }
629 }).start();
630 }
631 }).cacheWithInitialCapacity(1);
632
633 // we then expect the following 2 subscriptions to get that same value
634 final CountDownLatch latch = new CountDownLatch(2);
635
636 // subscribe once
637 f.subscribe(new Consumer<String>() {
638 @Override
639 public void accept(String v) {
640 assertEquals("one", v);
641 latch.countDown();
642 }
643 });
644
645 // subscribe again
646 f.subscribe(new Consumer<String>() {
647 @Override
648 public void accept(String v) {
649 assertEquals("one", v);
650 latch.countDown();
651 }
652 });
653
654 if (!latch.await(1000, TimeUnit.MILLISECONDS)) {
655 fail("subscriptions did not receive values");
656 }
657 assertEquals(1, counter.get());
658 }
659
660 @Test
661 public void takeWithErrorInObserver() {
662 final AtomicInteger count = new AtomicInteger();
663 final AtomicReference<Throwable> error = new AtomicReference<>();
664 Flowable.just("1", "2", "three", "4").take(3)
665 .safeSubscribe(new DefaultSubscriber<String>() {
666
667 @Override
668 public void onComplete() {
669 System.out.println("completed");
670 }
671
672 @Override
673 public void onError(Throwable e) {
674 error.set(e);
675 System.out.println("error");
676 e.printStackTrace();
677 }
678
679 @Override
680 public void onNext(String v) {
681 int num = Integer.parseInt(v);
682 System.out.println(num);
683 // doSomething(num);
684 count.incrementAndGet();
685 }
686
687 });
688 assertEquals(2, count.get());
689 assertNotNull(error.get());
690 if (!(error.get() instanceof NumberFormatException)) {
691 fail("It should be a NumberFormatException");
692 }
693 }
694
695 @Test
696 public void ofType() {
697 Flowable<String> flowable = Flowable.just(1, "abc", false, 2L).ofType(String.class);
698
699 Subscriber<Object> subscriber = TestHelper.mockSubscriber();
700
701 flowable.subscribe(subscriber);
702
703 verify(subscriber, never()).onNext(1);
704 verify(subscriber, times(1)).onNext("abc");
705 verify(subscriber, never()).onNext(false);
706 verify(subscriber, never()).onNext(2L);
707 verify(subscriber, never()).onError(any(Throwable.class));
708 verify(subscriber, times(1)).onComplete();
709 }
710
711 @Test
712 public void ofTypeWithPolymorphism() {
713 ArrayList<Integer> l1 = new ArrayList<>();
714 l1.add(1);
715 LinkedList<Integer> l2 = new LinkedList<>();
716 l2.add(2);
717
718 @SuppressWarnings("rawtypes")
719 Flowable<List> flowable = Flowable.<Object> just(l1, l2, "123").ofType(List.class);
720
721 Subscriber<Object> subscriber = TestHelper.mockSubscriber();
722
723 flowable.subscribe(subscriber);
724
725 verify(subscriber, times(1)).onNext(l1);
726 verify(subscriber, times(1)).onNext(l2);
727 verify(subscriber, never()).onNext("123");
728 verify(subscriber, never()).onError(any(Throwable.class));
729 verify(subscriber, times(1)).onComplete();
730 }
731
732 @Test
733 public void containsFlowable() {
734 Flowable<Boolean> flowable = Flowable.just("a", "b", "c").contains("b").toFlowable();
735
736 FlowableSubscriber<Boolean> subscriber = TestHelper.mockSubscriber();
737
738 flowable.subscribe(subscriber);
739
740 verify(subscriber, times(1)).onNext(true);
741 verify(subscriber, never()).onNext(false);
742 verify(subscriber, never()).onError(any(Throwable.class));
743 verify(subscriber, times(1)).onComplete();
744 }
745
746 @Test
747 public void containsWithInexistenceFlowable() {
748 Flowable<Boolean> flowable = Flowable.just("a", "b").contains("c").toFlowable();
749
750 Subscriber<Object> subscriber = TestHelper.mockSubscriber();
751
752 flowable.subscribe(subscriber);
753
754 verify(subscriber, times(1)).onNext(false);
755 verify(subscriber, never()).onNext(true);
756 verify(subscriber, never()).onError(any(Throwable.class));
757 verify(subscriber, times(1)).onComplete();
758 }
759
760 @Test
761 public void containsWithEmptyObservableFlowable() {
762 Flowable<Boolean> flowable = Flowable.<String> empty().contains("a").toFlowable();
763
764 FlowableSubscriber<Object> subscriber = TestHelper.mockSubscriber();
765
766 flowable.subscribe(subscriber);
767
768 verify(subscriber, times(1)).onNext(false);
769 verify(subscriber, never()).onNext(true);
770 verify(subscriber, never()).onError(any(Throwable.class));
771 verify(subscriber, times(1)).onComplete();
772 }
773
774 @Test
775 public void contains() {
776 Single<Boolean> single = Flowable.just("a", "b", "c").contains("b"); // FIXME nulls not allowed, changed to "c"
777
778 SingleObserver<Boolean> observer = TestHelper.mockSingleObserver();
779
780 single.subscribe(observer);
781
782 verify(observer, times(1)).onSuccess(true);
783 verify(observer, never()).onSuccess(false);
784 verify(observer, never()).onError(
785 any(Throwable.class));
786 }
787
788 @Test
789 public void containsWithInexistence() {
790 Single<Boolean> single = Flowable.just("a", "b").contains("c"); // FIXME null values are not allowed, removed
791
792 SingleObserver<Boolean> observer = TestHelper.mockSingleObserver();
793
794 single.subscribe(observer);
795
796 verify(observer, times(1)).onSuccess(false);
797 verify(observer, never()).onSuccess(true);
798 verify(observer, never()).onError(
799 any(Throwable.class));
800 }
801
802 @Test
803 public void containsWithEmptyObservable() {
804 Single<Boolean> single = Flowable.<String> empty().contains("a");
805
806 SingleObserver<Boolean> observer = TestHelper.mockSingleObserver();
807
808 single.subscribe(observer);
809
810 verify(observer, times(1)).onSuccess(false);
811 verify(observer, never()).onSuccess(true);
812 verify(observer, never()).onError(
813 any(Throwable.class));
814 }
815
816 @Test
817 public void ignoreElementsFlowable() {
818 Flowable<Integer> flowable = Flowable.just(1, 2, 3).ignoreElements().toFlowable();
819
820 Subscriber<Object> subscriber = TestHelper.mockSubscriber();
821
822 flowable.subscribe(subscriber);
823
824 verify(subscriber, never()).onNext(any(Integer.class));
825 verify(subscriber, never()).onError(any(Throwable.class));
826 verify(subscriber, times(1)).onComplete();
827 }
828
829 @Test
830 public void ignoreElements() {
831 Completable completable = Flowable.just(1, 2, 3).ignoreElements();
832
833 CompletableObserver observer = TestHelper.mockCompletableObserver();
834
835 completable.subscribe(observer);
836
837 verify(observer, never()).onError(any(Throwable.class));
838 verify(observer, times(1)).onComplete();
839 }
840
841 @Test
842 public void justWithScheduler() {
843 TestScheduler scheduler = new TestScheduler();
844 Flowable<Integer> flowable = Flowable.fromArray(1, 2).subscribeOn(scheduler);
845
846 Subscriber<Integer> subscriber = TestHelper.mockSubscriber();
847
848 flowable.subscribe(subscriber);
849
850 scheduler.advanceTimeBy(1, TimeUnit.MILLISECONDS);
851
852 InOrder inOrder = inOrder(subscriber);
853 inOrder.verify(subscriber, times(1)).onNext(1);
854 inOrder.verify(subscriber, times(1)).onNext(2);
855 inOrder.verify(subscriber, times(1)).onComplete();
856 inOrder.verifyNoMoreInteractions();
857 }
858
859 @Test
860 public void startWithWithScheduler() {
861 TestScheduler scheduler = new TestScheduler();
862 Flowable<Integer> flowable = Flowable.just(3, 4).startWithIterable(Arrays.asList(1, 2)).subscribeOn(scheduler);
863
864 Subscriber<Integer> subscriber = TestHelper.mockSubscriber();
865
866 flowable.subscribe(subscriber);
867
868 scheduler.advanceTimeBy(1, TimeUnit.MILLISECONDS);
869
870 InOrder inOrder = inOrder(subscriber);
871 inOrder.verify(subscriber, times(1)).onNext(1);
872 inOrder.verify(subscriber, times(1)).onNext(2);
873 inOrder.verify(subscriber, times(1)).onNext(3);
874 inOrder.verify(subscriber, times(1)).onNext(4);
875 inOrder.verify(subscriber, times(1)).onComplete();
876 inOrder.verifyNoMoreInteractions();
877 }
878
879 @Test
880 public void rangeWithScheduler() {
881 TestScheduler scheduler = new TestScheduler();
882 Flowable<Integer> flowable = Flowable.range(3, 4).subscribeOn(scheduler);
883
884 Subscriber<Integer> subscriber = TestHelper.mockSubscriber();
885
886 flowable.subscribe(subscriber);
887
888 scheduler.advanceTimeBy(1, TimeUnit.MILLISECONDS);
889
890 InOrder inOrder = inOrder(subscriber);
891 inOrder.verify(subscriber, times(1)).onNext(3);
892 inOrder.verify(subscriber, times(1)).onNext(4);
893 inOrder.verify(subscriber, times(1)).onNext(5);
894 inOrder.verify(subscriber, times(1)).onNext(6);
895 inOrder.verify(subscriber, times(1)).onComplete();
896 inOrder.verifyNoMoreInteractions();
897 }
898
899 @Test
900 public void mergeWith() {
901 TestSubscriber<Integer> ts = new TestSubscriber<>();
902 Flowable.just(1).mergeWith(Flowable.just(2)).subscribe(ts);
903 ts.assertValues(1, 2);
904 }
905
906 @Test
907 public void concatWith() {
908 TestSubscriber<Integer> ts = new TestSubscriber<>();
909 Flowable.just(1).concatWith(Flowable.just(2)).subscribe(ts);
910 ts.assertValues(1, 2);
911 }
912
913 @Test
914 public void ambWith() {
915 TestSubscriber<Integer> ts = new TestSubscriber<>();
916 Flowable.just(1).ambWith(Flowable.just(2)).subscribe(ts);
917 ts.assertValue(1);
918 }
919
920 @Test
921 public void takeWhileToList() {
922 final int expectedCount = 3;
923 final AtomicInteger count = new AtomicInteger();
924 for (int i = 0; i < expectedCount; i++) {
925 Flowable
926 .just(Boolean.TRUE, Boolean.FALSE)
927 .takeWhile(new Predicate<Boolean>() {
928 @Override
929 public boolean test(Boolean v) {
930 return v;
931 }
932 })
933 .toList()
934 .doOnSuccess(new Consumer<List<Boolean>>() {
935 @Override
936 public void accept(List<Boolean> booleans) {
937 count.incrementAndGet();
938 }
939 })
940 .subscribe();
941 }
942 assertEquals(expectedCount, count.get());
943 }
944
945 @Test
946 public void compose() {
947 TestSubscriberEx<String> ts = new TestSubscriberEx<>();
948 Flowable.just(1, 2, 3).compose(new FlowableTransformer<Integer, String>() {
949 @Override
950 public Publisher<String> apply(Flowable<Integer> t1) {
951 return t1.map(new Function<Integer, String>() {
952 @Override
953 public String apply(Integer v) {
954 return String.valueOf(v);
955 }
956 });
957 }
958 })
959 .subscribe(ts);
960 ts.assertTerminated();
961 ts.assertNoErrors();
962 ts.assertValues("1", "2", "3");
963 }
964
965 @Test
966 public void errorThrownIssue1685() {
967 List<Throwable> errors = TestHelper.trackPluginErrors();
968 try {
969 FlowableProcessor<Object> processor = ReplayProcessor.create();
970
971 Flowable.error(new RuntimeException("oops"))
972 .materialize()
973 .delay(1, TimeUnit.SECONDS)
974 .dematerialize(Functions.<Notification<Object>>identity())
975 .subscribe(processor);
976
977 processor.subscribe();
978 processor.materialize().blockingFirst();
979
980 System.out.println("Done");
981
982 TestHelper.assertError(errors, 0, OnErrorNotImplementedException.class);
983 } finally {
984 RxJavaPlugins.reset();
985 }
986 }
987
988 @Test
989 public void emptyIdentity() {
990 assertEquals(Flowable.empty(), Flowable.empty());
991 }
992
993 @Test
994 public void emptyIsEmpty() {
995 Flowable.<Integer>empty().subscribe(w);
996
997 verify(w).onComplete();
998 verify(w, never()).onNext(any(Integer.class));
999 verify(w, never()).onError(any(Throwable.class));
1000 }
1001
1002 @Test
1003 public void extend() {
1004 final TestSubscriber<Object> subscriber = new TestSubscriber<>();
1005 final Object value = new Object();
1006 Object returned = Flowable.just(value).to(new FlowableConverter<Object, Object>() {
1007 @Override
1008 public Object apply(Flowable<Object> onSubscribe) {
1009 onSubscribe.subscribe(subscriber);
1010 subscriber.assertNoErrors();
1011 subscriber.assertComplete();
1012 subscriber.assertValue(value);
1013 return subscriber.values().get(0);
1014 }
1015 });
1016 assertSame(returned, value);
1017 }
1018
1019 @Test
1020 public void asExtend() {
1021 final TestSubscriber<Object> subscriber = new TestSubscriber<>();
1022 final Object value = new Object();
1023 Object returned = Flowable.just(value).to(new FlowableConverter<Object, Object>() {
1024 @Override
1025 public Object apply(Flowable<Object> onSubscribe) {
1026 onSubscribe.subscribe(subscriber);
1027 subscriber.assertNoErrors();
1028 subscriber.assertComplete();
1029 subscriber.assertValue(value);
1030 return subscriber.values().get(0);
1031 }
1032 });
1033 assertSame(returned, value);
1034 }
1035
1036 @Test
1037 public void as() {
1038 Flowable.just(1).to(new FlowableConverter<Integer, Observable<Integer>>() {
1039 @Override
1040 public Observable<Integer> apply(Flowable<Integer> v) {
1041 return v.toObservable();
1042 }
1043 })
1044 .test()
1045 .assertResult(1);
1046 }
1047
1048 @Test
1049 public void toObservableEmpty() {
1050 Flowable.empty().toObservable().test().assertResult();
1051 }
1052
1053 @Test
1054 public void toObservableJust() {
1055 Flowable.just(1).toObservable().test().assertResult(1);
1056 }
1057
1058 @Test
1059 public void toObservableRange() {
1060 Flowable.range(1, 5).toObservable().test().assertResult(1, 2, 3, 4, 5);
1061 }
1062
1063 @Test
1064 public void toObservableError() {
1065 Flowable.error(new TestException()).toObservable().test().assertFailure(TestException.class);
1066 }
1067
1068 @Test
1069 public void zipIterableObject() {
1070 final List<Flowable<Integer>> flowables = Arrays.asList(Flowable.just(1, 2, 3), Flowable.just(1, 2, 3));
1071 Flowable.zip(flowables, new Function<Object[], Object>() {
1072 @Override
1073 public Object apply(Object[] o) throws Exception {
1074 int sum = 0;
1075 for (Object i : o) {
1076 sum += (Integer) i;
1077 }
1078 return sum;
1079 }
1080 }).test().assertResult(2, 4, 6);
1081 }
1082
1083 @Test
1084 public void combineLatestObject() {
1085 final List<Flowable<Integer>> flowables = Arrays.asList(Flowable.just(1, 2, 3), Flowable.just(1, 2, 3));
1086 Flowable.combineLatest(flowables, new Function<Object[], Object>() {
1087 @Override
1088 public Object apply(final Object[] o) throws Exception {
1089 int sum = 1;
1090 for (Object i : o) {
1091 sum *= (Integer) i;
1092 }
1093 return sum;
1094 }
1095 }).test().assertResult(3, 6, 9);
1096 }
1097 }