Debian Codegraph @

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