Debian Codegraph @

src/test/java/io/reactivex/rxjava3/subscribers/SafeSubscriberTest.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.ArgumentMatchers.any;
18 import static org.mockito.Mockito.*;
19
20 import java.util.List;
21 import java.util.concurrent.atomic.AtomicReference;
22
23 import org.junit.Test;
24 import org.mockito.Mockito;
25 import org.reactivestreams.*;
26
27 import io.reactivex.rxjava3.core.*;
28 import io.reactivex.rxjava3.exceptions.*;
29 import io.reactivex.rxjava3.internal.subscriptions.BooleanSubscription;
30 import io.reactivex.rxjava3.plugins.RxJavaPlugins;
31 import io.reactivex.rxjava3.testsupport.*;
32
33 public class SafeSubscriberTest extends RxJavaTest {
34
35 /**
36 * Ensure onNext can not be called after onError.
37 */
38 @Test
39 public void onNextAfterOnError() {
40 TestObservable t = new TestObservable();
41 Flowable<String> st = Flowable.unsafeCreate(t);
42
43 Subscriber<String> w = TestHelper.mockSubscriber();
44 st.subscribe(new SafeSubscriber<>(new TestSubscriber<>(w)));
45
46 t.sendOnNext("one");
47 t.sendOnError(new RuntimeException("bad"));
48 t.sendOnNext("two");
49
50 verify(w, times(1)).onNext("one");
51 verify(w, times(1)).onError(any(Throwable.class));
52 verify(w, Mockito.never()).onNext("two");
53 }
54
55 /**
56 * Ensure onComplete can not be called after onError.
57 */
58 @Test
59 public void onCompletedAfterOnError() {
60 TestObservable t = new TestObservable();
61 Flowable<String> st = Flowable.unsafeCreate(t);
62
63 Subscriber<String> w = TestHelper.mockSubscriber();
64
65 st.subscribe(new SafeSubscriber<>(new TestSubscriber<>(w)));
66
67 t.sendOnNext("one");
68 t.sendOnError(new RuntimeException("bad"));
69 t.sendOnCompleted();
70
71 verify(w, times(1)).onNext("one");
72 verify(w, times(1)).onError(any(Throwable.class));
73 verify(w, Mockito.never()).onComplete();
74 }
75
76 /**
77 * Ensure onNext can not be called after onComplete.
78 */
79 @Test
80 public void onNextAfterOnCompleted() {
81 TestObservable t = new TestObservable();
82 Flowable<String> st = Flowable.unsafeCreate(t);
83
84 Subscriber<String> w = TestHelper.mockSubscriber();
85 st.subscribe(new SafeSubscriber<>(new TestSubscriber<>(w)));
86
87 t.sendOnNext("one");
88 t.sendOnCompleted();
89 t.sendOnNext("two");
90
91 verify(w, times(1)).onNext("one");
92 verify(w, Mockito.never()).onNext("two");
93 verify(w, times(1)).onComplete();
94 verify(w, Mockito.never()).onError(any(Throwable.class));
95 }
96
97 /**
98 * Ensure onError can not be called after onComplete.
99 */
100 @Test
101 @SuppressUndeliverable
102 public void onErrorAfterOnCompleted() {
103 TestObservable t = new TestObservable();
104 Flowable<String> st = Flowable.unsafeCreate(t);
105
106 Subscriber<String> w = TestHelper.mockSubscriber();
107 st.subscribe(new SafeSubscriber<>(new TestSubscriber<>(w)));
108
109 t.sendOnNext("one");
110 t.sendOnCompleted();
111 t.sendOnError(new RuntimeException("bad"));
112
113 verify(w, times(1)).onNext("one");
114 verify(w, times(1)).onComplete();
115 verify(w, Mockito.never()).onError(any(Throwable.class));
116 }
117
118 /**
119 * An Observable that doesn't do the right thing on UnSubscribe/Error/etc in that it will keep sending events down the pipe regardless of what happens.
120 */
121 private static class TestObservable implements Publisher<String> {
122
123 Subscriber<? super String> subscriber;
124
125 /* used to simulate subscription */
126 public void sendOnCompleted() {
127 subscriber.onComplete();
128 }
129
130 /* used to simulate subscription */
131 public void sendOnNext(String value) {
132 subscriber.onNext(value);
133 }
134
135 /* used to simulate subscription */
136 public void sendOnError(Throwable e) {
137 subscriber.onError(e);
138 }
139
140 @Override
141 public void subscribe(Subscriber<? super String> subscriber) {
142 this.subscriber = subscriber;
143 subscriber.onSubscribe(new Subscription() {
144
145 @Override
146 public void cancel() {
147 // going to do nothing to pretend I'm a bad Observable that keeps allowing events to be sent
148 System.out.println("==> SynchronizeTest unsubscribe that does nothing!");
149 }
150
151 @Override
152 public void request(long n) {
153
154 }
155
156 });
157 }
158
159 }
160
161 @Test
162 public void onNextFailure() {
163 AtomicReference<Throwable> onError = new AtomicReference<>();
164 try {
165 OBSERVER_ONNEXT_FAIL(onError).onNext("one");
166 fail("expects exception to be thrown");
167 } catch (Exception e) {
168 assertNull(onError.get());
169 assertTrue(e instanceof SafeSubscriberTestException);
170 assertEquals("onNextFail", e.getMessage());
171 }
172 }
173
174 @Test
175 public void onNextFailureSafe() {
176 AtomicReference<Throwable> onError = new AtomicReference<>();
177 try {
178 SafeSubscriber<String> safeObserver = new SafeSubscriber<>(OBSERVER_ONNEXT_FAIL(onError));
179 safeObserver.onSubscribe(new BooleanSubscription());
180 safeObserver.onNext("one");
181 assertNotNull(onError.get());
182 assertTrue(onError.get() instanceof SafeSubscriberTestException);
183 assertEquals("onNextFail", onError.get().getMessage());
184 } catch (Exception e) {
185 fail("expects exception to be passed to onError");
186 }
187 }
188
189 @Test
190 public void onCompleteFailure() {
191 AtomicReference<Throwable> onError = new AtomicReference<>();
192 try {
193 OBSERVER_ONCOMPLETED_FAIL(onError).onComplete();
194 fail("expects exception to be thrown");
195 } catch (Exception e) {
196 assertNull(onError.get());
197 assertTrue(e instanceof SafeSubscriberTestException);
198 assertEquals("onCompleteFail", e.getMessage());
199 }
200 }
201
202 @Test
203 public void onErrorFailure() {
204 try {
205 subscriberOnErrorFail().onError(new SafeSubscriberTestException("error!"));
206 fail("expects exception to be thrown");
207 } catch (Exception e) {
208 assertTrue(e instanceof SafeSubscriberTestException);
209 assertEquals("onErrorFail", e.getMessage());
210 }
211 }
212
213 @Test
214 public void onNextOnErrorFailure() {
215 try {
216 OBSERVER_ONNEXT_ONERROR_FAIL().onNext("one");
217 fail("expects exception to be thrown");
218 } catch (Exception e) {
219 e.printStackTrace();
220 assertTrue(e instanceof SafeSubscriberTestException);
221 assertEquals("onNextFail", e.getMessage());
222 }
223 }
224
225 static final Subscription THROWING_DISPOSABLE = new Subscription() {
226
227 @Override
228 public void cancel() {
229 // break contract by throwing exception
230 throw new SafeSubscriberTestException("failure from unsubscribe");
231 }
232
233 @Override
234 public void request(long n) {
235 // ignored
236 }
237 };
238
239 private static Subscriber<String> OBSERVER_ONNEXT_FAIL(final AtomicReference<Throwable> onError) {
240 return new DefaultSubscriber<String>() {
241
242 @Override
243 public void onComplete() {
244
245 }
246
247 @Override
248 public void onError(Throwable e) {
249 onError.set(e);
250 }
251
252 @Override
253 public void onNext(String args) {
254 throw new SafeSubscriberTestException("onNextFail");
255 }
256 };
257
258 }
259
260 private static Subscriber<String> OBSERVER_ONNEXT_ONERROR_FAIL() {
261 return new DefaultSubscriber<String>() {
262
263 @Override
264 public void onComplete() {
265
266 }
267
268 @Override
269 public void onError(Throwable e) {
270 throw new SafeSubscriberTestException("onErrorFail");
271 }
272
273 @Override
274 public void onNext(String args) {
275 throw new SafeSubscriberTestException("onNextFail");
276 }
277
278 };
279 }
280
281 private static Subscriber<String> subscriberOnErrorFail() {
282 return new DefaultSubscriber<String>() {
283
284 @Override
285 public void onComplete() {
286
287 }
288
289 @Override
290 public void onError(Throwable e) {
291 throw new SafeSubscriberTestException("onErrorFail");
292 }
293
294 @Override
295 public void onNext(String args) {
296
297 }
298
299 };
300 }
301
302 private static Subscriber<String> OBSERVER_ONCOMPLETED_FAIL(final AtomicReference<Throwable> onError) {
303 return new DefaultSubscriber<String>() {
304
305 @Override
306 public void onComplete() {
307 throw new SafeSubscriberTestException("onCompleteFail");
308 }
309
310 @Override
311 public void onError(Throwable e) {
312 onError.set(e);
313 }
314
315 @Override
316 public void onNext(String args) {
317
318 }
319
320 };
321 }
322
323 @SuppressWarnings("serial")
324 private static class SafeSubscriberTestException extends RuntimeException {
325 SafeSubscriberTestException(String message) {
326 super(message);
327 }
328 }
329
330 @Test
331 public void actual() {
332 Subscriber<Integer> actual = new DefaultSubscriber<Integer>() {
333 @Override
334 public void onNext(Integer t) {
335 }
336
337 @Override
338 public void onError(Throwable e) {
339 }
340
341 @Override
342 public void onComplete() {
343 }
344 };
345 SafeSubscriber<Integer> s = new SafeSubscriber<>(actual);
346
347 assertSame(actual, s.downstream);
348 }
349
350 @Test
351 public void dispose() {
352 TestSubscriber<Integer> ts = new TestSubscriber<>();
353
354 SafeSubscriber<Integer> so = new SafeSubscriber<>(ts);
355
356 BooleanSubscription bs = new BooleanSubscription();
357
358 so.onSubscribe(bs);
359
360 ts.dispose();
361
362 assertTrue(bs.isCancelled());
363
364 // assertTrue(so.isDisposed());
365 }
366
367 @Test
368 @SuppressUndeliverable
369 public void onNextAfterComplete() {
370 TestSubscriber<Integer> ts = new TestSubscriber<>();
371
372 SafeSubscriber<Integer> so = new SafeSubscriber<>(ts);
373
374 BooleanSubscription bs = new BooleanSubscription();
375
376 so.onSubscribe(bs);
377
378 so.onComplete();
379
380 so.onNext(1);
381
382 so.onError(new TestException());
383
384 so.onComplete();
385
386 ts.assertResult();
387 }
388
389 @Test
390 public void onNextNull() {
391 TestSubscriber<Integer> ts = new TestSubscriber<>();
392
393 SafeSubscriber<Integer> so = new SafeSubscriber<>(ts);
394
395 BooleanSubscription bs = new BooleanSubscription();
396
397 so.onSubscribe(bs);
398
399 so.onNext(null);
400
401 ts.assertFailure(NullPointerException.class);
402 }
403
404 @Test
405 public void onNextWithoutOnSubscribe() {
406 TestSubscriberEx<Integer> ts = new TestSubscriberEx<>();
407
408 SafeSubscriber<Integer> so = new SafeSubscriber<>(ts);
409
410 so.onNext(1);
411
412 ts.assertFailureAndMessage(NullPointerException.class, "Subscription not set!");
413 }
414
415 @Test
416 public void onErrorWithoutOnSubscribe() {
417 TestSubscriberEx<Integer> ts = new TestSubscriberEx<>();
418
419 SafeSubscriber<Integer> so = new SafeSubscriber<>(ts);
420
421 so.onError(new TestException());
422
423 ts.assertFailure(CompositeException.class);
424
425 TestHelper.assertError(ts, 0, TestException.class);
426 TestHelper.assertError(ts, 1, NullPointerException.class, "Subscription not set!");
427 }
428
429 @Test
430 public void onCompleteWithoutOnSubscribe() {
431 TestSubscriberEx<Integer> ts = new TestSubscriberEx<>();
432
433 SafeSubscriber<Integer> so = new SafeSubscriber<>(ts);
434
435 so.onComplete();
436
437 ts.assertFailureAndMessage(NullPointerException.class, "Subscription not set!");
438 }
439
440 @Test
441 public void onNextNormal() {
442 TestSubscriber<Integer> ts = new TestSubscriber<>();
443
444 SafeSubscriber<Integer> so = new SafeSubscriber<>(ts);
445
446 BooleanSubscription bs = new BooleanSubscription();
447
448 so.onSubscribe(bs);
449
450 so.onNext(1);
451 so.onComplete();
452
453 ts.assertResult(1);
454 }
455
456 static final class CrashDummy implements FlowableSubscriber<Object>, Subscription {
457 final boolean crashOnSubscribe;
458
459 int crashOnNext;
460
461 final boolean crashOnError;
462
463 final boolean crashOnComplete;
464
465 final boolean crashDispose;
466
467 final boolean crashRequest;
468
469 Throwable error;
470
471 CrashDummy(boolean crashOnSubscribe, int crashOnNext,
472 boolean crashOnError, boolean crashOnComplete, boolean crashDispose, boolean crashRequest) {
473 this.crashOnSubscribe = crashOnSubscribe;
474 this.crashOnNext = crashOnNext;
475 this.crashOnError = crashOnError;
476 this.crashOnComplete = crashOnComplete;
477 this.crashDispose = crashDispose;
478 this.crashRequest = crashRequest;
479 }
480
481 @Override
482 public void cancel() {
483 if (crashDispose) {
484 throw new TestException("cancel()");
485 }
486 }
487
488 @Override
489 public void request(long n) {
490 if (crashRequest) {
491 throw new TestException("request()");
492 }
493 }
494
495 @Override
496 public void onSubscribe(Subscription s) {
497 if (crashOnSubscribe) {
498 throw new TestException("onSubscribe()");
499 }
500 }
501
502 @Override
503 public void onNext(Object value) {
504 if (--crashOnNext == 0) {
505 throw new TestException("onNext(" + value + ")");
506 }
507 }
508
509 @Override
510 public void onError(Throwable e) {
511 if (crashOnError) {
512 throw new TestException("onError(" + e + ")");
513 }
514 error = e;
515 }
516
517 @Override
518 public void onComplete() {
519 if (crashOnComplete) {
520 throw new TestException("onComplete()");
521 }
522 }
523
524 public SafeSubscriber<Object> toSafe() {
525 return new SafeSubscriber<>(this);
526 }
527
528 public CrashDummy assertError(Class<? extends Throwable> clazz) {
529 if (!clazz.isInstance(error)) {
530 throw new AssertionError("Different error: " + error);
531 }
532 return this;
533 }
534
535 public CrashDummy assertInnerError(int index, Class<? extends Throwable> clazz) {
536 List<Throwable> cel = TestHelper.compositeList(error);
537 TestHelper.assertError(cel, index, clazz);
538 return this;
539 }
540
541 public CrashDummy assertInnerError(int index, Class<? extends Throwable> clazz, String message) {
542 List<Throwable> cel = TestHelper.compositeList(error);
543 TestHelper.assertError(cel, index, clazz, message);
544 return this;
545 }
546
547 }
548
549 @Test
550 public void onNextOnErrorCrash() {
551 List<Throwable> list = TestHelper.trackPluginErrors();
552
553 try {
554 CrashDummy cd = new CrashDummy(false, 1, true, false, false, false);
555 SafeSubscriber<Object> so = cd.toSafe();
556 so.onSubscribe(cd);
557
558 so.onNext(1);
559
560 TestHelper.assertError(list, 0, CompositeException.class);
561 List<Throwable> ce = TestHelper.compositeList(list.get(0));
562 TestHelper.assertError(ce, 0, TestException.class, "onNext(1)");
563 TestHelper.assertError(ce, 1, TestException.class, "onError(io.reactivex.rxjava3.exceptions.TestException: onNext(1))");
564 } finally {
565 RxJavaPlugins.reset();
566 }
567 }
568
569 @Test
570 public void onNextDisposeCrash() {
571 CrashDummy cd = new CrashDummy(false, 1, false, false, true, false);
572 SafeSubscriber<Object> so = cd.toSafe();
573 so.onSubscribe(cd);
574
575 so.onNext(1);
576
577 cd.assertError(CompositeException.class);
578 cd.assertInnerError(0, TestException.class, "onNext(1)");
579 cd.assertInnerError(1, TestException.class, "cancel()");
580 }
581
582 @Test
583 public void onSubscribeTwice() {
584 List<Throwable> list = TestHelper.trackPluginErrors();
585
586 try {
587 CrashDummy cd = new CrashDummy(false, 1, false, false, false, false);
588 SafeSubscriber<Object> so = cd.toSafe();
589 so.onSubscribe(cd);
590 so.onSubscribe(cd);
591
592 TestHelper.assertError(list, 0, IllegalStateException.class);
593 } finally {
594 RxJavaPlugins.reset();
595 }
596 }
597
598 @Test
599 public void onSubscribeCrashes() {
600 List<Throwable> list = TestHelper.trackPluginErrors();
601
602 try {
603 CrashDummy cd = new CrashDummy(true, 1, false, false, false, false);
604 SafeSubscriber<Object> so = cd.toSafe();
605 so.onSubscribe(cd);
606
607 TestHelper.assertUndeliverable(list, 0, TestException.class, "onSubscribe()");
608 } finally {
609 RxJavaPlugins.reset();
610 }
611 }
612
613 @Test
614 public void onSubscribeAndDisposeCrashes() {
615 List<Throwable> list = TestHelper.trackPluginErrors();
616
617 try {
618 CrashDummy cd = new CrashDummy(true, 1, false, false, true, false);
619 SafeSubscriber<Object> so = cd.toSafe();
620 so.onSubscribe(cd);
621
622 TestHelper.assertError(list, 0, CompositeException.class);
623 List<Throwable> ce = TestHelper.compositeList(list.get(0));
624 TestHelper.assertError(ce, 0, TestException.class, "onSubscribe()");
625 TestHelper.assertError(ce, 1, TestException.class, "cancel()");
626 } finally {
627 RxJavaPlugins.reset();
628 }
629 }
630
631 @Test
632 public void onNextOnSubscribeCrash() {
633 List<Throwable> list = TestHelper.trackPluginErrors();
634
635 try {
636 CrashDummy cd = new CrashDummy(true, 1, false, false, false, false);
637 SafeSubscriber<Object> so = cd.toSafe();
638
639 so.onNext(1);
640
641 TestHelper.assertError(list, 0, CompositeException.class);
642 List<Throwable> ce = TestHelper.compositeList(list.get(0));
643 TestHelper.assertError(ce, 0, NullPointerException.class, "Subscription not set!");
644 TestHelper.assertError(ce, 1, TestException.class, "onSubscribe()");
645 } finally {
646 RxJavaPlugins.reset();
647 }
648 }
649
650 @Test
651 public void onNextNullDisposeCrashes() {
652 CrashDummy cd = new CrashDummy(false, 1, false, false, true, false);
653 SafeSubscriber<Object> so = cd.toSafe();
654 so.onSubscribe(cd);
655
656 so.onNext(null);
657
658 cd.assertInnerError(0, NullPointerException.class);
659 cd.assertInnerError(1, TestException.class, "cancel()");
660 }
661
662 @Test
663 public void noSubscribeOnErrorCrashes() {
664 List<Throwable> list = TestHelper.trackPluginErrors();
665
666 try {
667 CrashDummy cd = new CrashDummy(false, 1, true, false, false, false);
668 SafeSubscriber<Object> so = cd.toSafe();
669
670 so.onNext(1);
671
672 TestHelper.assertError(list, 0, CompositeException.class);
673 List<Throwable> ce = TestHelper.compositeList(list.get(0));
674 TestHelper.assertError(ce, 0, NullPointerException.class, "Subscription not set!");
675 TestHelper.assertError(ce, 1, TestException.class, "onError(java.lang.NullPointerException: Subscription not set!)");
676 } finally {
677 RxJavaPlugins.reset();
678 }
679 }
680
681 @Test
682 public void onErrorNull() {
683 CrashDummy cd = new CrashDummy(false, 1, false, false, false, false);
684 SafeSubscriber<Object> so = cd.toSafe();
685 so.onSubscribe(cd);
686
687 so.onError(null);
688
689 cd.assertError(NullPointerException.class);
690 }
691
692 @Test
693 public void onErrorNoSubscribeCrash() {
694 List<Throwable> list = TestHelper.trackPluginErrors();
695
696 try {
697 CrashDummy cd = new CrashDummy(true, 1, false, false, false, false);
698 SafeSubscriber<Object> so = cd.toSafe();
699
700 so.onError(new TestException());
701
702 TestHelper.assertError(list, 0, CompositeException.class);
703 List<Throwable> ce = TestHelper.compositeList(list.get(0));
704 TestHelper.assertError(ce, 0, TestException.class);
705 TestHelper.assertError(ce, 1, NullPointerException.class, "Subscription not set!");
706 } finally {
707 RxJavaPlugins.reset();
708 }
709 }
710
711 @Test
712 public void onErrorNoSubscribeOnErrorCrash() {
713 List<Throwable> list = TestHelper.trackPluginErrors();
714
715 try {
716 CrashDummy cd = new CrashDummy(false, 1, true, false, false, false);
717 SafeSubscriber<Object> so = cd.toSafe();
718
719 so.onError(new TestException());
720
721 TestHelper.assertError(list, 0, CompositeException.class);
722 List<Throwable> ce = TestHelper.compositeList(list.get(0));
723 TestHelper.assertError(ce, 0, TestException.class);
724 TestHelper.assertError(ce, 1, NullPointerException.class, "Subscription not set!");
725 TestHelper.assertError(ce, 2, TestException.class);
726 } finally {
727 RxJavaPlugins.reset();
728 }
729 }
730
731 @Test
732 public void onCompleteteCrash() {
733 List<Throwable> list = TestHelper.trackPluginErrors();
734
735 try {
736 CrashDummy cd = new CrashDummy(false, 1, false, true, false, false);
737 SafeSubscriber<Object> so = cd.toSafe();
738
739 so.onSubscribe(cd);
740
741 so.onComplete();
742
743 TestHelper.assertUndeliverable(list, 0, TestException.class, "onComplete()");
744 } finally {
745 RxJavaPlugins.reset();
746 }
747 }
748
749 @Test
750 public void onCompleteteNoSubscribeCrash() {
751 List<Throwable> list = TestHelper.trackPluginErrors();
752
753 try {
754 CrashDummy cd = new CrashDummy(true, 1, false, true, false, false);
755 SafeSubscriber<Object> so = cd.toSafe();
756
757 so.onComplete();
758
759 TestHelper.assertError(list, 0, CompositeException.class);
760 List<Throwable> ce = TestHelper.compositeList(list.get(0));
761 TestHelper.assertError(ce, 0, NullPointerException.class, "Subscription not set!");
762 TestHelper.assertError(ce, 1, TestException.class, "onSubscribe()");
763 } finally {
764 RxJavaPlugins.reset();
765 }
766 }
767
768 @Test
769 public void onCompleteteNoSubscribeOnErrorCrash() {
770 List<Throwable> list = TestHelper.trackPluginErrors();
771
772 try {
773 CrashDummy cd = new CrashDummy(false, 1, true, true, false, false);
774 SafeSubscriber<Object> so = cd.toSafe();
775
776 so.onComplete();
777
778 TestHelper.assertError(list, 0, CompositeException.class);
779 List<Throwable> ce = TestHelper.compositeList(list.get(0));
780 TestHelper.assertError(ce, 0, NullPointerException.class, "Subscription not set!");
781 TestHelper.assertError(ce, 1, TestException.class);
782 } finally {
783 RxJavaPlugins.reset();
784 }
785 }
786
787 @Test
788 public void requestCrash() {
789 List<Throwable> list = TestHelper.trackPluginErrors();
790
791 try {
792 CrashDummy cd = new CrashDummy(false, 1, false, false, false, true);
793 SafeSubscriber<Object> so = cd.toSafe();
794 so.onSubscribe(cd);
795
796 so.request(1);
797
798 TestHelper.assertUndeliverable(list, 0, TestException.class);
799 } finally {
800 RxJavaPlugins.reset();
801 }
802 }
803
804 @Test
805 public void cancelCrash() {
806 List<Throwable> list = TestHelper.trackPluginErrors();
807
808 try {
809 CrashDummy cd = new CrashDummy(false, 1, false, false, true, false);
810 SafeSubscriber<Object> so = cd.toSafe();
811 so.onSubscribe(cd);
812
813 so.cancel();
814
815 TestHelper.assertUndeliverable(list, 0, TestException.class);
816 } finally {
817 RxJavaPlugins.reset();
818 }
819 }
820
821 @Test
822 public void requestCancelCrash() {
823 List<Throwable> list = TestHelper.trackPluginErrors();
824
825 try {
826 CrashDummy cd = new CrashDummy(false, 1, false, false, true, true);
827 SafeSubscriber<Object> so = cd.toSafe();
828 so.onSubscribe(cd);
829
830 so.request(1);
831
832 TestHelper.assertError(list, 0, CompositeException.class);
833 List<Throwable> ce = TestHelper.compositeList(list.get(0));
834 TestHelper.assertError(ce, 0, TestException.class, "request()");
835 TestHelper.assertError(ce, 1, TestException.class, "cancel()");
836 } finally {
837 RxJavaPlugins.reset();
838 }
839 }
840
841 }