Debian Codegraph @

src/test/java/io/reactivex/rxjava3/testsupport/TestSubscriberExTest.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.testsupport;
15
16 import static org.junit.Assert.*;
17 import static org.mockito.Mockito.*;
18
19 import java.io.IOException;
20 import java.util.*;
21 import java.util.concurrent.TimeUnit;
22 import java.util.concurrent.atomic.AtomicBoolean;
23
24 import org.junit.Test;
25 import org.mockito.InOrder;
26 import org.reactivestreams.*;
27
28 import io.reactivex.rxjava3.core.*;
29 import io.reactivex.rxjava3.core.Scheduler.Worker;
30 import io.reactivex.rxjava3.exceptions.*;
31 import io.reactivex.rxjava3.functions.*;
32 import io.reactivex.rxjava3.internal.functions.Functions;
33 import io.reactivex.rxjava3.internal.subscriptions.*;
34 import io.reactivex.rxjava3.operators.QueueFuseable;
35 import io.reactivex.rxjava3.processors.*;
36 import io.reactivex.rxjava3.schedulers.Schedulers;
37
38 public class TestSubscriberExTest extends RxJavaTest {
39
40 @Test
41 public void assertTestSubscriberEx() {
42 Flowable<Integer> oi = Flowable.fromIterable(Arrays.asList(1, 2));
43 TestSubscriberEx<Integer> ts = new TestSubscriberEx<>();
44 oi.subscribe(ts);
45
46 ts.assertValues(1, 2);
47 ts.assertValueCount(2);
48 ts.assertTerminated();
49 }
50
51 @Test
52 public void assertNotMatchCount() {
53 assertThrows(AssertionError.class, () -> {
54 Flowable<Integer> oi = Flowable.fromIterable(Arrays.asList(1, 2));
55 TestSubscriberEx<Integer> ts = new TestSubscriberEx<>();
56 oi.subscribe(ts);
57
58 ts.assertValues(1);
59 ts.assertValueCount(2);
60 ts.assertTerminated();
61 });
62 }
63
64 @Test
65 public void assertNotMatchValue() {
66 assertThrows(AssertionError.class, () -> {
67 Flowable<Integer> oi = Flowable.fromIterable(Arrays.asList(1, 2));
68 TestSubscriberEx<Integer> ts = new TestSubscriberEx<>();
69 oi.subscribe(ts);
70
71 ts.assertValues(1, 3);
72 ts.assertValueCount(2);
73 ts.assertTerminated();
74 });
75 }
76
77 @Test
78 public void assertNeverAtNotMatchingValue() {
79 Flowable<Integer> oi = Flowable.fromIterable(Arrays.asList(1, 2));
80 TestSubscriberEx<Integer> ts = new TestSubscriberEx<>();
81 oi.subscribe(ts);
82
83 ts.assertNever(3);
84 ts.assertValueCount(2);
85 ts.assertTerminated();
86 }
87
88 @Test
89 public void assertNeverAtMatchingValue() {
90 assertThrows(AssertionError.class, () -> {
91 Flowable<Integer> oi = Flowable.fromIterable(Arrays.asList(1, 2));
92 TestSubscriberEx<Integer> ts = new TestSubscriberEx<>();
93 oi.subscribe(ts);
94
95 ts.assertValues(1, 2);
96
97 ts.assertNever(2);
98 ts.assertValueCount(2);
99 ts.assertTerminated();
100 });
101 }
102
103 @Test
104 public void assertNeverAtMatchingPredicate() {
105 assertThrows(AssertionError.class, () -> {
106 TestSubscriberEx<Integer> ts = new TestSubscriberEx<>();
107
108 Flowable.just(1, 2).subscribe(ts);
109
110 ts.assertValues(1, 2);
111
112 ts.assertNever(new Predicate<Integer>() {
113 @Override
114 public boolean test(final Integer o) throws Exception {
115 return o == 1;
116 }
117 });
118 });
119 }
120
121 @Test
122 public void assertNeverAtNotMatchingPredicate() {
123 TestSubscriberEx<Integer> ts = new TestSubscriberEx<>();
124
125 Flowable.just(2, 3).subscribe(ts);
126
127 ts.assertNever(new Predicate<Integer>() {
128 @Override
129 public boolean test(final Integer o) throws Exception {
130 return o == 1;
131 }
132 });
133 }
134
135 @Test
136 public void assertTerminalEventNotReceived() {
137 assertThrows(AssertionError.class, () -> {
138 PublishProcessor<Integer> p = PublishProcessor.create();
139 TestSubscriberEx<Integer> ts = new TestSubscriberEx<>();
140 p.subscribe(ts);
141
142 p.onNext(1);
143 p.onNext(2);
144
145 ts.assertValues(1, 2);
146 ts.assertValueCount(2);
147 ts.assertTerminated();
148 });
149 }
150
151 @Test
152 public void wrappingMock() {
153 Flowable<Integer> oi = Flowable.fromIterable(Arrays.asList(1, 2));
154 Subscriber<Integer> mockSubscriber = TestHelper.mockSubscriber();
155
156 oi.subscribe(new TestSubscriberEx<>(mockSubscriber));
157
158 InOrder inOrder = inOrder(mockSubscriber);
159 inOrder.verify(mockSubscriber, times(1)).onNext(1);
160 inOrder.verify(mockSubscriber, times(1)).onNext(2);
161 inOrder.verify(mockSubscriber, times(1)).onComplete();
162 inOrder.verifyNoMoreInteractions();
163 }
164
165 @Test
166 public void wrappingMockWhenUnsubscribeInvolved() {
167 Flowable<Integer> oi = Flowable.fromIterable(Arrays.asList(1, 2, 3, 4, 5, 6, 7, 8, 9)).take(2);
168 Subscriber<Integer> mockSubscriber = TestHelper.mockSubscriber();
169 oi.subscribe(new TestSubscriberEx<>(mockSubscriber));
170
171 InOrder inOrder = inOrder(mockSubscriber);
172 inOrder.verify(mockSubscriber, times(1)).onNext(1);
173 inOrder.verify(mockSubscriber, times(1)).onNext(2);
174 inOrder.verify(mockSubscriber, times(1)).onComplete();
175 inOrder.verifyNoMoreInteractions();
176 }
177
178 @Test
179 public void assertError() {
180 RuntimeException e = new RuntimeException("Oops");
181 TestSubscriberEx<Object> subscriber = new TestSubscriberEx<>();
182 Flowable.error(e).subscribe(subscriber);
183 subscriber.assertError(e);
184 }
185
186 @Test
187 public void awaitTerminalEventWithDurationAndUnsubscribeOnTimeout() {
188 TestSubscriberEx<Object> ts = new TestSubscriberEx<>();
189 final AtomicBoolean unsub = new AtomicBoolean(false);
190 Flowable.just(1)
191 //
192 .doOnCancel(new Action() {
193 @Override
194 public void run() {
195 unsub.set(true);
196 }
197 })
198 //
199 .delay(1000, TimeUnit.MILLISECONDS).subscribe(ts);
200 ts.awaitDone(100, TimeUnit.MILLISECONDS);
201 ts.dispose();
202 assertTrue(unsub.get());
203 }
204
205 @Test(expected = NullPointerException.class)
206 public void nullDelegate1() {
207 TestSubscriberEx<Integer> ts = new TestSubscriberEx<>(null);
208 ts.onComplete();
209 }
210
211 @Test(expected = NullPointerException.class)
212 public void nullDelegate2() {
213 TestSubscriberEx<Integer> ts = new TestSubscriberEx<>(null);
214 ts.onComplete();
215 }
216
217 @Test(expected = NullPointerException.class)
218 public void nullDelegate3() {
219 TestSubscriberEx<Integer> ts = new TestSubscriberEx<>(null, 0L);
220 ts.onComplete();
221 }
222
223 @Test
224 public void delegate1() {
225 TestSubscriberEx<Integer> ts0 = new TestSubscriberEx<>();
226 ts0.onSubscribe(EmptySubscription.INSTANCE);
227
228 TestSubscriberEx<Integer> ts = new TestSubscriberEx<>(ts0);
229 ts.onComplete();
230
231 ts0.assertTerminated();
232 }
233
234 @Test
235 public void delegate2() {
236 TestSubscriberEx<Integer> ts1 = new TestSubscriberEx<>();
237 TestSubscriberEx<Integer> ts2 = new TestSubscriberEx<>(ts1);
238 ts2.onComplete();
239
240 ts1.assertComplete();
241 }
242
243 @Test
244 public void delegate3() {
245 TestSubscriberEx<Integer> ts1 = new TestSubscriberEx<>();
246 TestSubscriberEx<Integer> ts2 = new TestSubscriberEx<>(ts1, 0L);
247 ts2.onComplete();
248 ts1.assertComplete();
249 }
250
251 @Test
252 public void unsubscribed() {
253 TestSubscriberEx<Integer> ts = new TestSubscriberEx<>();
254 assertFalse(ts.isCancelled());
255 }
256
257 @Test
258 public void noErrors() {
259 TestSubscriberEx<Integer> ts = new TestSubscriberEx<>();
260 ts.onError(new TestException());
261 try {
262 ts.assertNoErrors();
263 } catch (AssertionError ex) {
264 // expected
265 return;
266 }
267 fail("Error present but no assertion error!");
268 }
269
270 @Test
271 public void notCompleted() {
272 TestSubscriberEx<Integer> ts = new TestSubscriberEx<>();
273 try {
274 ts.assertComplete();
275 } catch (AssertionError ex) {
276 // expected
277 return;
278 }
279 fail("Not completed and no assertion error!");
280 }
281
282 @Test
283 public void multipleCompletions() {
284 TestSubscriberEx<Integer> ts = new TestSubscriberEx<>();
285 ts.onComplete();
286 ts.onComplete();
287 try {
288 ts.assertComplete();
289 } catch (AssertionError ex) {
290 // expected
291 return;
292 }
293 fail("Multiple completions and no assertion error!");
294 }
295
296 @Test
297 public void completed() {
298 TestSubscriberEx<Integer> ts = new TestSubscriberEx<>();
299 ts.onComplete();
300 try {
301 ts.assertNotComplete();
302 } catch (AssertionError ex) {
303 // expected
304 return;
305 }
306 fail("Completed and no assertion error!");
307 }
308
309 @Test
310 public void multipleCompletions2() {
311 TestSubscriberEx<Integer> ts = new TestSubscriberEx<>();
312 ts.onComplete();
313 ts.onComplete();
314 try {
315 ts.assertNotComplete();
316 } catch (AssertionError ex) {
317 // expected
318 return;
319 }
320 fail("Multiple completions and no assertion error!");
321 }
322
323 @Test
324 public void multipleErrors() {
325 TestSubscriberEx<Integer> ts = new TestSubscriberEx<>();
326 ts.onSubscribe(EmptySubscription.INSTANCE);
327 ts.onError(new TestException());
328 ts.onError(new TestException());
329 try {
330 ts.assertNoErrors();
331 } catch (AssertionError ex) {
332 Throwable e = ex.getCause();
333 if (!(e instanceof CompositeException)) {
334 fail("Multiple Error present but the reported error doesn't have a composite cause!");
335 }
336 CompositeException ce = (CompositeException)e;
337 if (ce.size() != 2) {
338 ce.printStackTrace();
339 }
340 assertEquals(2, ce.size());
341 // expected
342 return;
343 }
344 fail("Multiple Error present but no assertion error!");
345 }
346
347 @Test
348 public void multipleErrors2() {
349 TestSubscriberEx<Integer> ts = new TestSubscriberEx<>();
350 ts.onSubscribe(EmptySubscription.INSTANCE);
351 ts.onError(new TestException());
352 ts.onError(new TestException());
353 try {
354 ts.assertError(TestException.class);
355 } catch (AssertionError ex) {
356 Throwable e = ex.getCause();
357 if (!(e instanceof CompositeException)) {
358 fail("Multiple Error present but the reported error doesn't have a composite cause!");
359 }
360 CompositeException ce = (CompositeException)e;
361 assertEquals(2, ce.size());
362 // expected
363 return;
364 }
365 fail("Multiple Error present but no assertion error!");
366 }
367
368 @Test
369 public void multipleErrors3() {
370 TestSubscriberEx<Integer> ts = new TestSubscriberEx<>();
371 ts.onSubscribe(EmptySubscription.INSTANCE);
372 ts.onError(new TestException());
373 ts.onError(new TestException());
374 try {
375 ts.assertError(new TestException());
376 } catch (AssertionError ex) {
377 Throwable e = ex.getCause();
378 if (!(e instanceof CompositeException)) {
379 fail("Multiple Error present but the reported error doesn't have a composite cause!");
380 }
381 CompositeException ce = (CompositeException)e;
382 assertEquals(2, ce.size());
383 // expected
384 return;
385 }
386 fail("Multiple Error present but no assertion error!");
387 }
388
389 @Test
390 public void multipleErrors4() {
391 TestSubscriberEx<Integer> ts = new TestSubscriberEx<>();
392 ts.onSubscribe(EmptySubscription.INSTANCE);
393 ts.onError(new TestException());
394 ts.onError(new TestException());
395 try {
396 ts.assertError(Functions.<Throwable>alwaysTrue());
397 } catch (AssertionError ex) {
398 Throwable e = ex.getCause();
399 if (!(e instanceof CompositeException)) {
400 fail("Multiple Error present but the reported error doesn't have a composite cause!");
401 }
402 CompositeException ce = (CompositeException)e;
403 assertEquals(2, ce.size());
404 // expected
405 return;
406 }
407 fail("Multiple Error present but no assertion error!");
408 }
409
410 @Test
411 public void differentError() {
412 TestSubscriberEx<Integer> ts = new TestSubscriberEx<>();
413 ts.onError(new TestException());
414 try {
415 ts.assertError(new TestException());
416 } catch (AssertionError ex) {
417 // expected
418 return;
419 }
420 fail("Different Error present but no assertion error!");
421 }
422
423 @Test
424 public void differentError2() {
425 TestSubscriberEx<Integer> ts = new TestSubscriberEx<>();
426 ts.onError(new RuntimeException());
427 try {
428 ts.assertError(new TestException());
429 } catch (AssertionError ex) {
430 // expected
431 return;
432 }
433 fail("Different Error present but no assertion error!");
434 }
435
436 @Test
437 public void differentError3() {
438 TestSubscriberEx<Integer> ts = new TestSubscriberEx<>();
439 ts.onError(new RuntimeException());
440 try {
441 ts.assertError(TestException.class);
442 }
443 catch (AssertionError ex) {
444 // expected
445 return;
446 }
447 fail("Different Error present but no assertion error!");
448 }
449
450 @Test
451 public void differentError4() {
452 TestSubscriberEx<Integer> ts = new TestSubscriberEx<>();
453 ts.onError(new RuntimeException());
454 try {
455 ts.assertError(Functions.<Throwable>alwaysFalse());
456 } catch (AssertionError ex) {
457 // expected
458 return;
459 }
460 fail("Different Error present but no assertion error!");
461 }
462
463 @Test
464 public void errorInPredicate() {
465 TestSubscriberEx<Integer> ts = new TestSubscriberEx<>();
466 ts.onError(new RuntimeException());
467 try {
468 ts.assertError(new Predicate<Throwable>() {
469 @Override
470 public boolean test(Throwable throwable) throws Exception {
471 throw new TestException();
472 }
473 });
474 } catch (TestException ex) {
475 // expected
476 return;
477 }
478 fail("Error in predicate but not thrown!");
479 }
480
481 @Test
482 public void noError() {
483 TestSubscriberEx<Integer> ts = new TestSubscriberEx<>();
484 try {
485 ts.assertError(TestException.class);
486 } catch (AssertionError ex) {
487 // expected
488 return;
489 }
490 fail("No present but no assertion error!");
491 }
492
493 @Test
494 public void noError2() {
495 TestSubscriberEx<Integer> ts = new TestSubscriberEx<>();
496 try {
497 ts.assertError(new TestException());
498 } catch (AssertionError ex) {
499 // expected
500 return;
501 }
502 fail("No present but no assertion error!");
503 }
504
505 @Test
506 public void noError3() {
507 TestSubscriberEx<Integer> ts = new TestSubscriberEx<>();
508 try {
509 ts.assertError(Functions.<Throwable>alwaysTrue());
510 } catch (AssertionError ex) {
511 // expected
512 return;
513 }
514 fail("No present but no assertion error!");
515 }
516
517 @Test
518 public void interruptTerminalEventAwait() {
519 TestSubscriberEx<Integer> ts = new TestSubscriberEx<>();
520
521 final Thread t0 = Thread.currentThread();
522 Worker w = Schedulers.computation().createWorker();
523 try {
524 w.schedule(new Runnable() {
525 @Override
526 public void run() {
527 t0.interrupt();
528 }
529 }, 200, TimeUnit.MILLISECONDS);
530
531 try {
532 if (ts.await(5, TimeUnit.SECONDS)) {
533 fail("Did not interrupt wait!");
534 }
535 } catch (InterruptedException expected) {
536 // expected
537 }
538 } finally {
539 w.dispose();
540 }
541 }
542
543 @Test
544 public void interruptTerminalEventAwaitTimed() {
545 TestSubscriberEx<Integer> ts = new TestSubscriberEx<>();
546
547 final Thread t0 = Thread.currentThread();
548 Worker w = Schedulers.computation().createWorker();
549 try {
550 w.schedule(new Runnable() {
551 @Override
552 public void run() {
553 t0.interrupt();
554 }
555 }, 200, TimeUnit.MILLISECONDS);
556
557 try {
558 if (ts.await(5, TimeUnit.SECONDS)) {
559 fail("Did not interrupt wait!");
560 }
561 } catch (InterruptedException expected) {
562 // expected
563 }
564 } finally {
565 Thread.interrupted(); // clear interrupted flag
566 w.dispose();
567 }
568 }
569
570 @Test
571 public void interruptTerminalEventAwaitAndUnsubscribe() {
572 TestSubscriberEx<Integer> ts = new TestSubscriberEx<>();
573
574 final Thread t0 = Thread.currentThread();
575 Worker w = Schedulers.computation().createWorker();
576 try {
577 w.schedule(new Runnable() {
578 @Override
579 public void run() {
580 t0.interrupt();
581 }
582 }, 200, TimeUnit.MILLISECONDS);
583
584 try {
585 ts.awaitDone(5, TimeUnit.SECONDS);
586 } catch (RuntimeException allowed) {
587 assertTrue(allowed.toString(), allowed.getCause() instanceof InterruptedException);
588 }
589
590 ts.dispose();
591 if (!ts.isCancelled()) {
592 fail("Did not unsubscribe!");
593 }
594 } finally {
595 w.dispose();
596 }
597 }
598
599 @Test
600 public void noTerminalEventBut1Completed() {
601 TestSubscriberEx<Integer> ts = new TestSubscriberEx<>();
602
603 ts.onComplete();
604
605 try {
606 ts.assertNotTerminated();
607 throw new RuntimeException("Failed to report there were terminal event(s)!");
608 } catch (AssertionError ex) {
609 // expected
610 }
611 }
612
613 @Test
614 public void noTerminalEventBut1Error() {
615 TestSubscriberEx<Integer> ts = new TestSubscriberEx<>();
616
617 ts.onError(new TestException());
618
619 try {
620 ts.assertNotTerminated();
621 throw new RuntimeException("Failed to report there were terminal event(s)!");
622 } catch (AssertionError ex) {
623 // expected
624 }
625 }
626
627 @Test
628 public void noTerminalEventBut1Error1Completed() {
629 TestSubscriberEx<Integer> ts = new TestSubscriberEx<>();
630
631 ts.onComplete();
632 ts.onError(new TestException());
633
634 try {
635 ts.assertNotTerminated();
636 throw new RuntimeException("Failed to report there were terminal event(s)!");
637 } catch (AssertionError ex) {
638 // expected
639 }
640 }
641
642 @Test
643 public void noTerminalEventBut2Errors() {
644 TestSubscriberEx<Integer> ts = new TestSubscriberEx<>();
645 ts.onSubscribe(EmptySubscription.INSTANCE);
646
647 ts.onError(new TestException());
648 ts.onError(new TestException());
649
650 try {
651 ts.assertNotTerminated();
652 throw new RuntimeException("Failed to report there were terminal event(s)!");
653 } catch (AssertionError ex) {
654 // expected
655 Throwable e = ex.getCause();
656 if (!(e instanceof CompositeException)) {
657 fail("Multiple Error present but the reported error doesn't have a composite cause!");
658 }
659 CompositeException ce = (CompositeException)e;
660 assertEquals(2, ce.size());
661 }
662 }
663
664 @Test
665 public void noValues() {
666 TestSubscriberEx<Integer> ts = new TestSubscriberEx<>();
667 ts.onNext(1);
668
669 try {
670 ts.assertNoValues();
671 throw new RuntimeException("Failed to report there were values!");
672 } catch (AssertionError ex) {
673 // expected
674 }
675 }
676
677 @Test
678 public void valueCount() {
679 TestSubscriberEx<Integer> ts = new TestSubscriberEx<>();
680 ts.onNext(1);
681 ts.onNext(2);
682
683 try {
684 ts.assertValueCount(3);
685 throw new RuntimeException("Failed to report there were values!");
686 } catch (AssertionError ex) {
687 // expected
688 }
689 }
690
691 @Test
692 public void onCompletedCrashCountsDownLatch() {
693 TestSubscriberEx<Integer> ts0 = new TestSubscriberEx<Integer>() {
694 @Override
695 public void onComplete() {
696 throw new TestException();
697 }
698 };
699 TestSubscriberEx<Integer> ts = new TestSubscriberEx<>(ts0);
700
701 try {
702 ts.onComplete();
703 } catch (TestException ex) {
704 // expected
705 }
706
707 ts.awaitDone(5, TimeUnit.SECONDS);
708 }
709
710 @Test
711 public void onErrorCrashCountsDownLatch() {
712 TestSubscriberEx<Integer> ts0 = new TestSubscriberEx<Integer>() {
713 @Override
714 public void onError(Throwable e) {
715 throw new TestException();
716 }
717 };
718 TestSubscriberEx<Integer> ts = new TestSubscriberEx<>(ts0);
719
720 try {
721 ts.onError(new RuntimeException());
722 } catch (TestException ex) {
723 // expected
724 }
725
726 ts.awaitDone(5, TimeUnit.SECONDS);
727 }
728
729 @Test
730 public void createDelegate() {
731 TestSubscriberEx<Integer> ts1 = new TestSubscriberEx<>();
732
733 TestSubscriberEx<Integer> ts = new TestSubscriberEx<>(ts1);
734
735 ts.assertNotSubscribed();
736
737 assertFalse(ts.hasSubscription());
738
739 ts.onSubscribe(new BooleanSubscription());
740
741 try {
742 ts.assertNotSubscribed();
743 throw new RuntimeException("Should have thrown");
744 } catch (AssertionError ex) {
745 // expected
746 }
747
748 assertTrue(ts.hasSubscription());
749
750 assertFalse(ts.isDisposed());
751
752 ts.onNext(1);
753 ts.onError(new TestException());
754 ts.onComplete();
755
756 ts1.assertValue(1).assertError(TestException.class).assertComplete();
757
758 ts.dispose();
759
760 assertTrue(ts.isDisposed());
761
762 assertSame(Thread.currentThread(), ts.lastThread());
763
764 try {
765 ts.assertNoValues();
766 throw new RuntimeException("Should have thrown");
767 } catch (AssertionError exc) {
768 // expected
769 }
770
771 try {
772 ts.assertValueCount(0);
773 throw new RuntimeException("Should have thrown");
774 } catch (AssertionError exc) {
775 // expected
776 }
777
778 ts.assertValueSequence(Collections.singletonList(1));
779
780 try {
781 ts.assertValueSequence(Collections.singletonList(2));
782 throw new RuntimeException("Should have thrown");
783 } catch (AssertionError exc) {
784 // expected
785 }
786 }
787
788 @Test
789 public void assertError2() {
790 TestSubscriberEx<Integer> ts = new TestSubscriberEx<>();
791
792 try {
793 ts.assertError(TestException.class);
794 throw new RuntimeException("Should have thrown");
795 } catch (AssertionError ex) {
796 // expected
797 }
798
799 try {
800 ts.assertError(new TestException());
801 throw new RuntimeException("Should have thrown");
802 } catch (AssertionError ex) {
803 // expected
804 }
805
806 try {
807 ts.assertErrorMessage("");
808 throw new RuntimeException("Should have thrown");
809 } catch (AssertionError exc) {
810 // expected
811 }
812
813 try {
814 ts.assertError(Functions.<Throwable>alwaysTrue());
815 throw new RuntimeException("Should have thrown");
816 } catch (AssertionError ex) {
817 // expected
818 }
819
820 try {
821 ts.assertSubscribed();
822 throw new RuntimeException("Should have thrown");
823 } catch (AssertionError exc) {
824 // expected
825 }
826
827 try {
828 ts.assertTerminated();
829 throw new RuntimeException("Should have thrown");
830 } catch (AssertionError exc) {
831 // expected
832 }
833
834 ts.onSubscribe(new BooleanSubscription());
835
836 ts.assertSubscribed();
837
838 ts.assertNoErrors();
839
840 TestException ex = new TestException("Forced failure");
841
842 ts.onError(ex);
843
844 ts.assertError(ex);
845
846 ts.assertError(TestException.class);
847
848 ts.assertErrorMessage("Forced failure");
849
850 ts.assertError(Functions.<Throwable>alwaysTrue());
851
852 ts.assertError(new Predicate<Throwable>() {
853 @Override
854 public boolean test(Throwable t) {
855 return t.getMessage() != null && t.getMessage().contains("Forced");
856 }
857 });
858
859 try {
860 ts.assertErrorMessage("");
861 throw new RuntimeException("Should have thrown");
862 } catch (AssertionError exc) {
863 // expected
864 }
865
866 try {
867 ts.assertError(new RuntimeException());
868 throw new RuntimeException("Should have thrown");
869 } catch (AssertionError exc) {
870 // expected
871 }
872
873 try {
874 ts.assertError(IOException.class);
875 throw new RuntimeException("Should have thrown");
876 } catch (AssertionError exc) {
877 // expected
878 }
879
880 try {
881 ts.assertNoErrors();
882 throw new RuntimeException("Should have thrown");
883 } catch (AssertionError exc) {
884 // expected
885 }
886
887 try {
888 ts.assertError(Functions.<Throwable>alwaysFalse());
889 throw new RuntimeException("Should have thrown");
890 } catch (AssertionError exc) {
891 // expected
892 }
893
894 ts.assertTerminated();
895
896 ts.assertValueCount(0);
897
898 ts.assertNoValues();
899 }
900
901 @Test
902 public void emptyObserverEnum() {
903 assertEquals(1, TestSubscriberEx.EmptySubscriber.values().length);
904 assertNotNull(TestSubscriberEx.EmptySubscriber.valueOf("INSTANCE"));
905 }
906
907 @Test
908 public void valueAndClass() {
909 assertEquals("null", TestSubscriberEx.valueAndClass(null));
910 assertEquals("1 (class: Integer)", TestSubscriberEx.valueAndClass(1));
911 }
912
913 @Test
914 public void assertFailure() {
915 TestSubscriberEx<Integer> ts = new TestSubscriberEx<>();
916
917 ts.onSubscribe(new BooleanSubscription());
918
919 ts.onError(new TestException("Forced failure"));
920
921 ts.assertFailure(TestException.class);
922
923 ts.assertFailure(Functions.<Throwable>alwaysTrue());
924
925 ts.assertFailureAndMessage(TestException.class, "Forced failure");
926
927 ts.onNext(1);
928
929 ts.assertFailure(TestException.class, 1);
930
931 ts.assertFailure(Functions.<Throwable>alwaysTrue(), 1);
932
933 ts.assertFailureAndMessage(TestException.class, "Forced failure", 1);
934 }
935
936 @Test
937 public void assertFuseable() {
938 TestSubscriberEx<Integer> ts = new TestSubscriberEx<>();
939
940 ts.onSubscribe(new BooleanSubscription());
941
942 ts.assertNotFuseable();
943
944 try {
945 ts.assertFuseable();
946 throw new RuntimeException("Should have thrown");
947 } catch (AssertionError ex) {
948 // expected
949 }
950
951 try {
952 ts.assertFusionMode(QueueFuseable.SYNC);
953 throw new RuntimeException("Should have thrown");
954 } catch (AssertionError ex) {
955 // expected
956 }
957 ts = new TestSubscriberEx<>();
958 ts.setInitialFusionMode(QueueFuseable.ANY);
959
960 ts.onSubscribe(new ScalarSubscription<>(ts, 1));
961
962 ts.assertFuseable();
963
964 ts.assertFusionMode(QueueFuseable.SYNC);
965
966 try {
967 ts.assertFusionMode(QueueFuseable.NONE);
968 throw new RuntimeException("Should have thrown");
969 } catch (AssertionError ex) {
970 // expected
971 }
972
973 try {
974 ts.assertNotFuseable();
975 throw new RuntimeException("Should have thrown");
976 } catch (AssertionError ex) {
977 // expected
978 }
979
980 }
981
982 @Test
983 public void assertTerminated() {
984 TestSubscriberEx<Integer> ts = new TestSubscriberEx<>();
985
986 ts.assertNotTerminated();
987
988 ts.onError(null);
989
990 try {
991 ts.assertNotTerminated();
992 throw new RuntimeException("Should have thrown!");
993 } catch (AssertionError ex) {
994 // expected
995 }
996 }
997
998 @Test
999 public void assertResult() {
1000 TestSubscriberEx<Integer> ts = new TestSubscriberEx<>();
1001
1002 ts.onSubscribe(new BooleanSubscription());
1003
1004 ts.onComplete();
1005
1006 ts.assertResult();
1007
1008 try {
1009 ts.assertResult(1);
1010 throw new RuntimeException("Should have thrown");
1011 } catch (AssertionError ex) {
1012 // expected
1013 }
1014
1015 ts.onNext(1);
1016
1017 ts.assertResult(1);
1018
1019 try {
1020 ts.assertResult(2);
1021 throw new RuntimeException("Should have thrown");
1022 } catch (AssertionError ex) {
1023 // expected
1024 }
1025
1026 try {
1027 ts.assertResult();
1028 throw new RuntimeException("Should have thrown");
1029 } catch (AssertionError ex) {
1030 // expected
1031 }
1032
1033 }
1034
1035 @Test
1036 public void await() throws Exception {
1037 TestSubscriberEx<Integer> ts = new TestSubscriberEx<>();
1038
1039 ts.onSubscribe(new BooleanSubscription());
1040
1041 assertFalse(ts.await(100, TimeUnit.MILLISECONDS));
1042
1043 ts.awaitDone(100, TimeUnit.MILLISECONDS);
1044
1045 assertTrue(ts.isDisposed());
1046
1047 assertFalse(ts.await(100, TimeUnit.MILLISECONDS));
1048
1049 ts.assertNotComplete().assertNoErrors();
1050
1051 ts.onComplete();
1052
1053 assertTrue(ts.await(100, TimeUnit.MILLISECONDS));
1054
1055 ts.await();
1056
1057 ts.awaitDone(5, TimeUnit.SECONDS);
1058
1059 ts.assertComplete().assertNoErrors();
1060
1061 assertTrue(ts.await(5, TimeUnit.SECONDS));
1062
1063 final TestSubscriberEx<Integer> ts1 = new TestSubscriberEx<>();
1064
1065 ts1.onSubscribe(new BooleanSubscription());
1066
1067 Schedulers.single().scheduleDirect(new Runnable() {
1068 @Override
1069 public void run() {
1070 ts1.onComplete();
1071 }
1072 }, 200, TimeUnit.MILLISECONDS);
1073
1074 ts1.await();
1075 }
1076
1077 @Test
1078 public void errors() {
1079 TestSubscriberEx<Integer> ts = new TestSubscriberEx<>();
1080
1081 ts.onSubscribe(new BooleanSubscription());
1082
1083 assertEquals(0, ts.errors().size());
1084
1085 ts.onError(new TestException());
1086
1087 assertEquals(1, ts.errors().size());
1088
1089 TestHelper.assertError(ts.errors(), 0, TestException.class);
1090 }
1091
1092 @Test
1093 public void onNext() {
1094 TestSubscriberEx<Integer> ts = new TestSubscriberEx<>();
1095
1096 ts.onSubscribe(new BooleanSubscription());
1097
1098 ts.assertNoValues();
1099
1100 assertEquals(Collections.emptyList(), ts.values());
1101
1102 ts.onNext(1);
1103
1104 assertEquals(Collections.singletonList(1), ts.values());
1105
1106 ts.cancel();
1107
1108 assertTrue(ts.isCancelled());
1109 assertTrue(ts.isDisposed());
1110
1111 ts.assertValue(1);
1112
1113 ts.onComplete();
1114 }
1115
1116 @Test
1117 public void fusionModeToString() {
1118 assertEquals("NONE", TestSubscriberEx.fusionModeToString(QueueFuseable.NONE));
1119 assertEquals("SYNC", TestSubscriberEx.fusionModeToString(QueueFuseable.SYNC));
1120 assertEquals("ASYNC", TestSubscriberEx.fusionModeToString(QueueFuseable.ASYNC));
1121 assertEquals("Unknown(100)", TestSubscriberEx.fusionModeToString(100));
1122 }
1123
1124 @Test
1125 public void multipleTerminals() {
1126 TestSubscriberEx<Integer> ts = new TestSubscriberEx<>();
1127
1128 ts.onSubscribe(new BooleanSubscription());
1129
1130 ts.assertNotComplete();
1131
1132 ts.onComplete();
1133
1134 try {
1135 ts.assertNotComplete();
1136 throw new RuntimeException("Should have thrown");
1137 } catch (Throwable ex) {
1138 // expected
1139 }
1140
1141 ts.assertTerminated();
1142
1143 ts.onComplete();
1144
1145 try {
1146 ts.assertComplete();
1147 throw new RuntimeException("Should have thrown");
1148 } catch (Throwable ex) {
1149 // expected
1150 }
1151
1152 try {
1153 ts.assertTerminated();
1154 throw new RuntimeException("Should have thrown");
1155 } catch (Throwable ex) {
1156 // expected
1157 }
1158
1159 try {
1160 ts.assertNotComplete();
1161 throw new RuntimeException("Should have thrown");
1162 } catch (Throwable ex) {
1163 // expected
1164 }
1165 }
1166
1167 @Test
1168 public void assertValue() {
1169 TestSubscriberEx<Integer> ts = new TestSubscriberEx<>();
1170
1171 ts.onSubscribe(new BooleanSubscription());
1172
1173 try {
1174 ts.assertValue(1);
1175 throw new RuntimeException("Should have thrown");
1176 } catch (Throwable ex) {
1177 // expected
1178 }
1179
1180 ts.onNext(1);
1181
1182 ts.assertValue(1);
1183
1184 try {
1185 ts.assertValue(2);
1186 throw new RuntimeException("Should have thrown");
1187 } catch (Throwable ex) {
1188 // expected
1189 }
1190
1191 ts.onNext(2);
1192
1193 try {
1194 ts.assertValue(1);
1195 throw new RuntimeException("Should have thrown");
1196 } catch (Throwable ex) {
1197 // expected
1198 }
1199 }
1200
1201 @Test
1202 public void onNextMisbehave() {
1203 TestSubscriberEx<Integer> ts = new TestSubscriberEx<>();
1204
1205 ts.onNext(1);
1206
1207 ts.assertError(IllegalStateException.class);
1208
1209 ts = new TestSubscriberEx<>();
1210
1211 ts.onSubscribe(new BooleanSubscription());
1212
1213 ts.onNext(null);
1214
1215 ts.assertFailure(NullPointerException.class, (Integer)null);
1216 }
1217
1218 @Test
1219 public void awaitTerminalEventInterrupt() {
1220 final TestSubscriberEx<Integer> ts = new TestSubscriberEx<>();
1221
1222 ts.onSubscribe(new BooleanSubscription());
1223
1224 Thread.currentThread().interrupt();
1225
1226 try {
1227 ts.awaitDone(5, TimeUnit.SECONDS);
1228 } catch (RuntimeException allowed) {
1229 assertTrue(allowed.toString(), allowed.getCause() instanceof InterruptedException);
1230 }
1231
1232 // FIXME ? catch consumes this flag
1233 // assertTrue(Thread.interrupted());
1234
1235 Thread.currentThread().interrupt();
1236
1237 try {
1238 ts.awaitDone(5, TimeUnit.SECONDS);
1239 } catch (RuntimeException allowed) {
1240 assertTrue(allowed.toString(), allowed.getCause() instanceof InterruptedException);
1241 }
1242
1243 // FIXME ? catch consumes this flag
1244 // assertTrue(Thread.interrupted());
1245 }
1246
1247 @Test
1248 public void assertTerminated2() {
1249 TestSubscriberEx<Integer> ts = new TestSubscriberEx<>();
1250
1251 ts.onSubscribe(new BooleanSubscription());
1252
1253 ts.assertNotTerminated();
1254
1255 ts.onError(new TestException());
1256
1257 ts.assertTerminated();
1258
1259 ts.onError(new IOException());
1260
1261 try {
1262 ts.assertTerminated();
1263 throw new RuntimeException("Should have thrown");
1264 } catch (AssertionError ex) {
1265 // expected
1266 }
1267
1268 try {
1269 ts.assertError(TestException.class);
1270 throw new RuntimeException("Should have thrown");
1271 } catch (AssertionError ex) {
1272 // expected
1273 }
1274
1275 ts = new TestSubscriberEx<>();
1276
1277 ts.onSubscribe(new BooleanSubscription());
1278
1279 ts.onError(new TestException());
1280 ts.onComplete();
1281
1282 try {
1283 ts.assertTerminated();
1284 throw new RuntimeException("Should have thrown");
1285 } catch (AssertionError ex) {
1286 // expected
1287 }
1288 }
1289
1290 @Test
1291 public void onSubscribe() {
1292 TestSubscriberEx<Integer> ts = new TestSubscriberEx<>();
1293
1294 ts.onSubscribe(null);
1295
1296 ts.assertError(NullPointerException.class);
1297
1298 ts = new TestSubscriberEx<>();
1299
1300 ts.onSubscribe(new BooleanSubscription());
1301
1302 BooleanSubscription bs1 = new BooleanSubscription();
1303
1304 ts.onSubscribe(bs1);
1305
1306 assertTrue(bs1.isCancelled());
1307
1308 ts.assertError(IllegalStateException.class);
1309
1310 ts = new TestSubscriberEx<>();
1311 ts.dispose();
1312
1313 bs1 = new BooleanSubscription();
1314
1315 ts.onSubscribe(bs1);
1316
1317 assertTrue(bs1.isCancelled());
1318
1319 }
1320
1321 @Test
1322 public void assertValueSequence() {
1323 TestSubscriberEx<Integer> ts = new TestSubscriberEx<>();
1324
1325 ts.onSubscribe(new BooleanSubscription());
1326
1327 ts.onNext(1);
1328 ts.onNext(2);
1329
1330 try {
1331 ts.assertValueSequence(Collections.<Integer>emptyList());
1332 throw new RuntimeException("Should have thrown");
1333 } catch (AssertionError ex) {
1334 // expected
1335 }
1336
1337 try {
1338 ts.assertValueSequence(Collections.singletonList(1));
1339 throw new RuntimeException("Should have thrown");
1340 } catch (AssertionError ex) {
1341 // expected
1342 }
1343
1344 ts.assertValueSequence(Arrays.asList(1, 2));
1345
1346 try {
1347 ts.assertValueSequence(Arrays.asList(1, 2, 3));
1348 throw new RuntimeException("Should have thrown");
1349 } catch (AssertionError ex) {
1350 // expected
1351 }
1352 }
1353
1354 @Test
1355 public void assertEmpty() {
1356 TestSubscriberEx<Integer> ts = new TestSubscriberEx<>();
1357
1358 try {
1359 ts.assertEmpty();
1360 throw new RuntimeException("Should have thrown!");
1361 } catch (AssertionError ex) {
1362 // expected
1363 }
1364
1365 ts.onSubscribe(new BooleanSubscription());
1366
1367 ts.assertEmpty();
1368
1369 ts.onNext(1);
1370
1371 try {
1372 ts.assertEmpty();
1373 throw new RuntimeException("Should have thrown!");
1374 } catch (AssertionError ex) {
1375 // expected
1376 }
1377 }
1378
1379 @Test
1380 public void awaitDoneTimed() {
1381 TestSubscriberEx<Integer> ts = new TestSubscriberEx<>();
1382
1383 Thread.currentThread().interrupt();
1384
1385 try {
1386 ts.awaitDone(5, TimeUnit.SECONDS);
1387 } catch (RuntimeException ex) {
1388 assertTrue(ex.toString(), ex.getCause() instanceof InterruptedException);
1389 }
1390 }
1391
1392 @Test
1393 public void assertNotSubscribed() {
1394 TestSubscriberEx<Integer> ts = new TestSubscriberEx<>();
1395
1396 ts.assertNotSubscribed();
1397
1398 ts.errors().add(new TestException());
1399
1400 try {
1401 ts.assertNotSubscribed();
1402 throw new RuntimeException("Should have thrown!");
1403 } catch (AssertionError ex) {
1404 // expected
1405 }
1406 }
1407
1408 @Test
1409 public void assertErrorMultiple() {
1410 TestSubscriberEx<Integer> ts = new TestSubscriberEx<>();
1411
1412 TestException e = new TestException();
1413 ts.errors().add(e);
1414 ts.errors().add(new TestException());
1415
1416 try {
1417 ts.assertError(TestException.class);
1418 throw new RuntimeException("Should have thrown!");
1419 } catch (AssertionError ex) {
1420 // expected
1421 }
1422 try {
1423 ts.assertError(e);
1424 throw new RuntimeException("Should have thrown!");
1425 } catch (AssertionError ex) {
1426 // expected
1427 }
1428 try {
1429 ts.assertErrorMessage("");
1430 throw new RuntimeException("Should have thrown!");
1431 } catch (AssertionError ex) {
1432 // expected
1433 }
1434 }
1435
1436 @Test
1437 public void assertComplete() {
1438 TestSubscriberEx<Integer> ts = new TestSubscriberEx<>();
1439
1440 ts.onSubscribe(new BooleanSubscription());
1441
1442 try {
1443 ts.assertComplete();
1444 throw new RuntimeException("Should have thrown!");
1445 } catch (AssertionError ex) {
1446 // expected
1447 }
1448
1449 ts.onComplete();
1450
1451 ts.assertComplete();
1452
1453 ts.onComplete();
1454
1455 try {
1456 ts.assertComplete();
1457 throw new RuntimeException("Should have thrown!");
1458 } catch (AssertionError ex) {
1459 // expected
1460 }
1461 }
1462
1463 @Test
1464 public void completeWithoutOnSubscribe() {
1465 TestSubscriberEx<Integer> ts = new TestSubscriberEx<>();
1466
1467 ts.onComplete();
1468
1469 ts.assertError(IllegalStateException.class);
1470 }
1471
1472 @Test
1473 public void completeDelegateThrows() {
1474 TestSubscriberEx<Integer> ts = new TestSubscriberEx<>(new FlowableSubscriber<Integer>() {
1475
1476 @Override
1477 public void onSubscribe(Subscription s) {
1478
1479 }
1480
1481 @Override
1482 public void onNext(Integer value) {
1483
1484 }
1485
1486 @Override
1487 public void onError(Throwable e) {
1488 throw new TestException();
1489 }
1490
1491 @Override
1492 public void onComplete() {
1493 throw new TestException();
1494 }
1495
1496 });
1497
1498 ts.onSubscribe(new BooleanSubscription());
1499
1500 try {
1501 ts.onComplete();
1502 throw new RuntimeException("Should have thrown!");
1503 } catch (TestException ex) {
1504 ts.assertTerminated();
1505 }
1506 }
1507
1508 @Test
1509 public void errorDelegateThrows() {
1510 TestSubscriberEx<Integer> ts = new TestSubscriberEx<>(new FlowableSubscriber<Integer>() {
1511
1512 @Override
1513 public void onSubscribe(Subscription s) {
1514
1515 }
1516
1517 @Override
1518 public void onNext(Integer value) {
1519
1520 }
1521
1522 @Override
1523 public void onError(Throwable e) {
1524 throw new TestException();
1525 }
1526
1527 @Override
1528 public void onComplete() {
1529 throw new TestException();
1530 }
1531
1532 });
1533
1534 ts.onSubscribe(new BooleanSubscription());
1535
1536 try {
1537 ts.onError(new IOException());
1538 throw new RuntimeException("Should have thrown!");
1539 } catch (TestException ex) {
1540 ts.assertTerminated();
1541 }
1542 }
1543
1544 @Test
1545 public void syncQueueThrows() {
1546 TestSubscriberEx<Object> ts = new TestSubscriberEx<>();
1547 ts.setInitialFusionMode(QueueFuseable.SYNC);
1548
1549 Flowable.range(1, 5)
1550 .map(new Function<Integer, Object>() {
1551 @Override
1552 public Object apply(Integer v) throws Exception { throw new TestException(); }
1553 })
1554 .subscribe(ts);
1555
1556 ts.assertSubscribed()
1557 .assertFuseable()
1558 .assertFusionMode(QueueFuseable.SYNC)
1559 .assertFailure(TestException.class);
1560 }
1561
1562 @Test
1563 public void asyncQueueThrows() {
1564 TestSubscriberEx<Object> ts = new TestSubscriberEx<>();
1565 ts.setInitialFusionMode(QueueFuseable.ANY);
1566
1567 UnicastProcessor<Integer> up = UnicastProcessor.create();
1568
1569 up
1570 .map(new Function<Integer, Object>() {
1571 @Override
1572 public Object apply(Integer v) throws Exception { throw new TestException(); }
1573 })
1574 .subscribe(ts);
1575
1576 up.onNext(1);
1577
1578 ts.assertSubscribed()
1579 .assertFuseable()
1580 .assertFusionMode(QueueFuseable.ASYNC)
1581 .assertFailure(TestException.class);
1582 }
1583
1584 @Test
1585 public void assertValuePredicateEmpty() {
1586 assertThrows("No values", AssertionError.class, () -> {
1587 TestSubscriberEx<Object> ts = new TestSubscriberEx<>();
1588
1589 Flowable.empty().subscribe(ts);
1590
1591 ts.assertValue(new Predicate<Object>() {
1592 @Override public boolean test(final Object o) throws Exception {
1593 return false;
1594 }
1595 });
1596 });
1597 }
1598
1599 @Test
1600 public void assertValuePredicateMatch() {
1601 TestSubscriberEx<Integer> ts = new TestSubscriberEx<>();
1602
1603 Flowable.just(1).subscribe(ts);
1604
1605 ts.assertValue(new Predicate<Integer>() {
1606 @Override public boolean test(final Integer o) throws Exception {
1607 return o == 1;
1608 }
1609 });
1610 }
1611
1612 @Test
1613 public void assertValuePredicateNoMatch() {
1614 assertThrows("Value not present", AssertionError.class, () -> {
1615 TestSubscriberEx<Integer> ts = new TestSubscriberEx<>();
1616
1617 Flowable.just(1).subscribe(ts);
1618
1619 ts.assertValue(new Predicate<Integer>() {
1620 @Override public boolean test(final Integer o) throws Exception {
1621 return o != 1;
1622 }
1623 });
1624 });
1625 }
1626
1627 @Test
1628 public void assertValuePredicateMatchButMore() {
1629 assertThrows("Value present but other values as well", AssertionError.class, () -> {
1630 TestSubscriberEx<Integer> ts = new TestSubscriberEx<>();
1631
1632 Flowable.just(1, 2).subscribe(ts);
1633
1634 ts.assertValue(new Predicate<Integer>() {
1635 @Override public boolean test(final Integer o) throws Exception {
1636 return o == 1;
1637 }
1638 });
1639 });
1640 }
1641
1642 @Test
1643 public void assertValueAtPredicateEmpty() {
1644 assertThrows("No values", AssertionError.class, () -> {
1645 TestSubscriberEx<Object> ts = new TestSubscriberEx<>();
1646
1647 Flowable.empty().subscribe(ts);
1648
1649 ts.assertValueAt(0, new Predicate<Object>() {
1650 @Override public boolean test(final Object o) throws Exception {
1651 return false;
1652 }
1653 });
1654 });
1655 }
1656
1657 @Test
1658 public void assertValueAtPredicateMatch() {
1659 TestSubscriberEx<Integer> ts = new TestSubscriberEx<>();
1660
1661 Flowable.just(1, 2).subscribe(ts);
1662
1663 ts.assertValueAt(1, new Predicate<Integer>() {
1664 @Override public boolean test(final Integer o) throws Exception {
1665 return o == 2;
1666 }
1667 });
1668 }
1669
1670 @Test
1671 public void assertValueAtPredicateNoMatch() {
1672 assertThrows("Value not present", AssertionError.class, () -> {
1673 TestSubscriberEx<Integer> ts = new TestSubscriberEx<>();
1674
1675 Flowable.just(1, 2, 3).subscribe(ts);
1676
1677 ts.assertValueAt(2, new Predicate<Integer>() {
1678 @Override public boolean test(final Integer o) throws Exception {
1679 return o != 3;
1680 }
1681 });
1682 });
1683 }
1684
1685 @Test
1686 public void assertValueAtInvalidIndex() {
1687 assertThrows("Invalid index: 2 (latch = 0, values = 2, errors = 0, completions = 1)", AssertionError.class, () -> {
1688 TestSubscriberEx<Integer> ts = new TestSubscriberEx<>();
1689
1690 Flowable.just(1, 2).subscribe(ts);
1691
1692 ts.assertValueAt(2, new Predicate<Integer>() {
1693 @Override public boolean test(final Integer o) throws Exception {
1694 return o == 1;
1695 }
1696 });
1697 });
1698 }
1699
1700 @Test
1701 public void requestMore() {
1702 Flowable.range(1, 5)
1703 .test(0)
1704 .requestMore(1)
1705 .assertValue(1)
1706 .requestMore(2)
1707 .assertValues(1, 2, 3)
1708 .requestMore(3)
1709 .assertResult(1, 2, 3, 4, 5);
1710 }
1711
1712 @Test
1713 public void withTag() {
1714 try {
1715 for (int i = 1; i < 3; i++) {
1716 Flowable.just(i)
1717 .test()
1718 .withTag("testing with item=" + i)
1719 .assertResult(1)
1720 ;
1721 }
1722 throw new RuntimeException("Should have thrown!");
1723 } catch (AssertionError ex) {
1724 assertTrue(ex.toString(), ex.toString().contains("testing with item=2"));
1725 }
1726 }
1727
1728 @Test
1729 public void timeoutIndicated() throws InterruptedException {
1730 Thread.interrupted(); // clear flag
1731
1732 TestSubscriberEx<Object> ts = Flowable.never()
1733 .to(TestHelper.<Object>testConsumer());
1734 assertFalse(ts.await(1, TimeUnit.MILLISECONDS));
1735
1736 try {
1737 ts.assertResult(1);
1738 throw new RuntimeException("Should have thrown!");
1739 } catch (AssertionError ex) {
1740 assertTrue(ex.toString(), ex.toString().contains("timeout!"));
1741 }
1742 }
1743
1744 @Test
1745 public void timeoutIndicated2() throws InterruptedException {
1746 try {
1747 Flowable.never()
1748 .test()
1749 .awaitDone(1, TimeUnit.MILLISECONDS)
1750 .assertResult(1);
1751
1752 throw new RuntimeException("Should have thrown!");
1753 } catch (AssertionError ex) {
1754 assertTrue(ex.toString(), ex.toString().contains("timeout!"));
1755 }
1756 }
1757
1758 @Test
1759 public void timeoutIndicated3() throws InterruptedException {
1760 TestSubscriberEx<Object> ts = Flowable.never()
1761 .to(TestHelper.<Object>testConsumer());
1762 assertFalse(ts.await(1, TimeUnit.MILLISECONDS));
1763
1764 try {
1765 ts.assertResult(1);
1766 throw new RuntimeException("Should have thrown!");
1767 } catch (AssertionError ex) {
1768 assertTrue(ex.toString(), ex.toString().contains("timeout!"));
1769 }
1770 }
1771
1772 @Test
1773 public void disposeIndicated() {
1774 TestSubscriberEx<Object> ts = new TestSubscriberEx<>();
1775 ts.cancel();
1776
1777 try {
1778 ts.assertResult(1);
1779 throw new RuntimeException("Should have thrown!");
1780 } catch (Throwable ex) {
1781 assertTrue(ex.toString(), ex.toString().contains("disposed!"));
1782 }
1783 }
1784
1785 @Test
1786 public void awaitCount() {
1787 Flowable.range(1, 10).delay(100, TimeUnit.MILLISECONDS)
1788 .test(5)
1789 .awaitCount(5)
1790 .assertValues(1, 2, 3, 4, 5)
1791 .requestMore(5)
1792 .awaitDone(5, TimeUnit.SECONDS)
1793 .assertResult(1, 2, 3, 4, 5, 6, 7, 8, 9, 10);
1794 }
1795
1796 @Test
1797 public void awaitCountLess() {
1798 Flowable.range(1, 4)
1799 .test()
1800 .awaitCount(5)
1801 .assertResult(1, 2, 3, 4);
1802 }
1803
1804 @Test
1805 public void assertValueAtPredicateThrows() {
1806 try {
1807 Flowable.just(1)
1808 .test()
1809 .assertValueAt(0, new Predicate<Integer>() {
1810 @Override
1811 public boolean test(Integer t) throws Exception {
1812 throw new IllegalArgumentException();
1813 }
1814 });
1815 throw new RuntimeException("Should have thrown!");
1816 } catch (IllegalArgumentException ex) {
1817 // expected
1818 }
1819 }
1820
1821 @Test
1822 public void assertValuesOnly() {
1823 TestSubscriberEx<Integer> ts = new TestSubscriberEx<>();
1824 ts.onSubscribe(new BooleanSubscription());
1825 ts.assertValuesOnly();
1826
1827 ts.onNext(5);
1828 ts.assertValuesOnly(5);
1829
1830 ts.onNext(-1);
1831 ts.assertValuesOnly(5, -1);
1832 }
1833
1834 @Test
1835 public void assertValuesOnlyThrowsOnUnexpectedValue() {
1836 TestSubscriberEx<Integer> ts = new TestSubscriberEx<>();
1837 ts.onSubscribe(new BooleanSubscription());
1838 ts.assertValuesOnly();
1839
1840 ts.onNext(5);
1841 ts.assertValuesOnly(5);
1842
1843 ts.onNext(-1);
1844
1845 try {
1846 ts.assertValuesOnly(5);
1847 throw new RuntimeException();
1848 } catch (AssertionError ex) {
1849 // expected
1850 }
1851 }
1852
1853 @Test
1854 public void assertValuesOnlyThrowsWhenCompleted() {
1855 TestSubscriberEx<Integer> ts = new TestSubscriberEx<>();
1856 ts.onSubscribe(new BooleanSubscription());
1857
1858 ts.onComplete();
1859
1860 try {
1861 ts.assertValuesOnly();
1862 throw new RuntimeException();
1863 } catch (AssertionError ex) {
1864 // expected
1865 }
1866 }
1867
1868 @Test
1869 public void assertValuesOnlyThrowsWhenErrored() {
1870 TestSubscriberEx<Integer> ts = new TestSubscriberEx<>();
1871 ts.onSubscribe(new BooleanSubscription());
1872
1873 ts.onError(new TestException());
1874
1875 try {
1876 ts.assertValuesOnly();
1877 throw new RuntimeException();
1878 } catch (AssertionError ex) {
1879 // expected
1880 }
1881 }
1882 }