Debian Codegraph @

src/test/java/io/reactivex/rxjava3/observers/SafeObserverTest.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.observers;
15
16 import static org.junit.Assert.*;
17
18 import java.util.List;
19 import java.util.concurrent.atomic.AtomicReference;
20
21 import org.junit.Test;
22
23 import io.reactivex.rxjava3.core.*;
24 import io.reactivex.rxjava3.disposables.*;
25 import io.reactivex.rxjava3.exceptions.*;
26 import io.reactivex.rxjava3.plugins.RxJavaPlugins;
27 import io.reactivex.rxjava3.testsupport.*;
28
29 public class SafeObserverTest extends RxJavaTest {
30
31 @Test
32 public void onNextFailure() {
33 AtomicReference<Throwable> onError = new AtomicReference<>();
34 try {
35 OBSERVER_ONNEXT_FAIL(onError).onNext("one");
36 fail("expects exception to be thrown");
37 } catch (Exception e) {
38 assertNull(onError.get());
39 assertTrue(e instanceof SafeObserverTestException);
40 assertEquals("onNextFail", e.getMessage());
41 }
42 }
43
44 @Test
45 public void onNextFailureSafe() {
46 AtomicReference<Throwable> onError = new AtomicReference<>();
47 try {
48 SafeObserver<String> safeObserver = new SafeObserver<>(OBSERVER_ONNEXT_FAIL(onError));
49 safeObserver.onSubscribe(Disposable.empty());
50 safeObserver.onNext("one");
51 assertNotNull(onError.get());
52 assertTrue(onError.get() instanceof SafeObserverTestException);
53 assertEquals("onNextFail", onError.get().getMessage());
54 } catch (Exception e) {
55 fail("expects exception to be passed to onError");
56 }
57 }
58
59 @Test
60 public void onCompleteFailure() {
61 AtomicReference<Throwable> onError = new AtomicReference<>();
62 try {
63 OBSERVER_ONCOMPLETED_FAIL(onError).onComplete();
64 fail("expects exception to be thrown");
65 } catch (Exception e) {
66 assertNull(onError.get());
67 assertTrue(e instanceof SafeObserverTestException);
68 assertEquals("onCompleteFail", e.getMessage());
69 }
70 }
71
72 @Test
73 public void onErrorFailure() {
74 try {
75 OBSERVER_ONERROR_FAIL().onError(new SafeObserverTestException("error!"));
76 fail("expects exception to be thrown");
77 } catch (Exception e) {
78 assertTrue(e instanceof SafeObserverTestException);
79 assertEquals("onErrorFail", e.getMessage());
80 }
81 }
82
83 @Test
84 public void onNextOnErrorFailure() {
85 try {
86 OBSERVER_ONNEXT_ONERROR_FAIL().onNext("one");
87 fail("expects exception to be thrown");
88 } catch (Exception e) {
89 e.printStackTrace();
90 assertTrue(e instanceof SafeObserverTestException);
91 assertEquals("onNextFail", e.getMessage());
92 }
93 }
94
95 private static Observer<String> OBSERVER_ONNEXT_FAIL(final AtomicReference<Throwable> onError) {
96 return new DefaultObserver<String>() {
97
98 @Override
99 public void onComplete() {
100
101 }
102
103 @Override
104 public void onError(Throwable e) {
105 onError.set(e);
106 }
107
108 @Override
109 public void onNext(String args) {
110 throw new SafeObserverTestException("onNextFail");
111 }
112 };
113
114 }
115
116 private static Observer<String> OBSERVER_ONNEXT_ONERROR_FAIL() {
117 return new DefaultObserver<String>() {
118
119 @Override
120 public void onComplete() {
121
122 }
123
124 @Override
125 public void onError(Throwable e) {
126 throw new SafeObserverTestException("onErrorFail");
127 }
128
129 @Override
130 public void onNext(String args) {
131 throw new SafeObserverTestException("onNextFail");
132 }
133
134 };
135 }
136
137 private static Observer<String> OBSERVER_ONERROR_FAIL() {
138 return new DefaultObserver<String>() {
139
140 @Override
141 public void onComplete() {
142
143 }
144
145 @Override
146 public void onError(Throwable e) {
147 throw new SafeObserverTestException("onErrorFail");
148 }
149
150 @Override
151 public void onNext(String args) {
152
153 }
154
155 };
156 }
157
158 private static Observer<String> OBSERVER_ONCOMPLETED_FAIL(final AtomicReference<Throwable> onError) {
159 return new DefaultObserver<String>() {
160
161 @Override
162 public void onComplete() {
163 throw new SafeObserverTestException("onCompleteFail");
164 }
165
166 @Override
167 public void onError(Throwable e) {
168 onError.set(e);
169 }
170
171 @Override
172 public void onNext(String args) {
173
174 }
175
176 };
177 }
178
179 @SuppressWarnings("serial")
180 static class SafeObserverTestException extends RuntimeException {
181 SafeObserverTestException(String message) {
182 super(message);
183 }
184 }
185
186 @Test
187 public void actual() {
188 Observer<Integer> actual = new DefaultObserver<Integer>() {
189 @Override
190 public void onNext(Integer t) {
191 }
192
193 @Override
194 public void onError(Throwable e) {
195 }
196
197 @Override
198 public void onComplete() {
199 }
200 };
201 SafeObserver<Integer> observer = new SafeObserver<>(actual);
202
203 assertSame(actual, observer.downstream);
204 }
205
206 @Test
207 public void dispose() {
208 TestObserver<Integer> to = new TestObserver<>();
209
210 SafeObserver<Integer> so = new SafeObserver<>(to);
211
212 Disposable d = Disposable.empty();
213
214 so.onSubscribe(d);
215
216 to.dispose();
217
218 assertTrue(d.isDisposed());
219
220 assertTrue(so.isDisposed());
221 }
222
223 @Test
224 @SuppressUndeliverable
225 public void onNextAfterComplete() {
226 TestObserver<Integer> to = new TestObserver<>();
227
228 SafeObserver<Integer> so = new SafeObserver<>(to);
229
230 Disposable d = Disposable.empty();
231
232 so.onSubscribe(d);
233
234 so.onComplete();
235
236 so.onNext(1);
237
238 so.onError(new TestException());
239
240 so.onComplete();
241
242 to.assertResult();
243 }
244
245 @Test
246 public void onNextNull() {
247 TestObserver<Integer> to = new TestObserver<>();
248
249 SafeObserver<Integer> so = new SafeObserver<>(to);
250
251 Disposable d = Disposable.empty();
252
253 so.onSubscribe(d);
254
255 so.onNext(null);
256
257 to.assertFailure(NullPointerException.class);
258 }
259
260 @Test
261 public void onNextWithoutOnSubscribe() {
262 TestObserverEx<Integer> to = new TestObserverEx<>();
263
264 SafeObserver<Integer> so = new SafeObserver<>(to);
265
266 so.onNext(1);
267
268 to.assertFailureAndMessage(NullPointerException.class, "Subscription not set!");
269 }
270
271 @Test
272 public void onErrorWithoutOnSubscribe() {
273 TestObserverEx<Integer> to = new TestObserverEx<>();
274
275 SafeObserver<Integer> so = new SafeObserver<>(to);
276
277 so.onError(new TestException());
278
279 to.assertFailure(CompositeException.class);
280
281 TestHelper.assertError(to, 0, TestException.class);
282 TestHelper.assertError(to, 1, NullPointerException.class, "Subscription not set!");
283 }
284
285 @Test
286 public void onCompleteWithoutOnSubscribe() {
287 TestObserverEx<Integer> to = new TestObserverEx<>();
288
289 SafeObserver<Integer> so = new SafeObserver<>(to);
290
291 so.onComplete();
292
293 to.assertFailureAndMessage(NullPointerException.class, "Subscription not set!");
294 }
295
296 @Test
297 public void onNextNormal() {
298 TestObserver<Integer> to = new TestObserver<>();
299
300 SafeObserver<Integer> so = new SafeObserver<>(to);
301
302 Disposable d = Disposable.empty();
303
304 so.onSubscribe(d);
305
306 so.onNext(1);
307 so.onComplete();
308
309 to.assertResult(1);
310 }
311
312 static final class CrashDummy implements Observer<Object>, Disposable {
313 boolean crashOnSubscribe;
314 int crashOnNext;
315 boolean crashOnError;
316 boolean crashOnComplete;
317
318 boolean crashDispose;
319
320 Throwable error;
321
322 CrashDummy(boolean crashOnSubscribe, int crashOnNext,
323 boolean crashOnError, boolean crashOnComplete, boolean crashDispose) {
324 this.crashOnSubscribe = crashOnSubscribe;
325 this.crashOnNext = crashOnNext;
326 this.crashOnError = crashOnError;
327 this.crashOnComplete = crashOnComplete;
328 this.crashDispose = crashDispose;
329 }
330
331 @Override
332 public void dispose() {
333 if (crashDispose) {
334 throw new TestException("dispose()");
335 }
336 }
337
338 @Override
339 public boolean isDisposed() {
340 return false;
341 }
342
343 @Override
344 public void onSubscribe(Disposable d) {
345 if (crashOnSubscribe) {
346 throw new TestException("onSubscribe()");
347 }
348 }
349
350 @Override
351 public void onNext(Object value) {
352 if (--crashOnNext == 0) {
353 throw new TestException("onNext(" + value + ")");
354 }
355 }
356
357 @Override
358 public void onError(Throwable e) {
359 if (crashOnError) {
360 throw new TestException("onError(" + e + ")");
361 }
362 error = e;
363 }
364
365 @Override
366 public void onComplete() {
367 if (crashOnComplete) {
368 throw new TestException("onComplete()");
369 }
370 }
371
372 public SafeObserver<Object> toSafe() {
373 return new SafeObserver<>(this);
374 }
375
376 public CrashDummy assertError(Class<? extends Throwable> clazz) {
377 if (!clazz.isInstance(error)) {
378 throw new AssertionError("Different error: " + error);
379 }
380 return this;
381 }
382
383 public CrashDummy assertInnerError(int index, Class<? extends Throwable> clazz) {
384 List<Throwable> cel = TestHelper.compositeList(error);
385 TestHelper.assertError(cel, index, clazz);
386 return this;
387 }
388
389 public CrashDummy assertInnerError(int index, Class<? extends Throwable> clazz, String message) {
390 List<Throwable> cel = TestHelper.compositeList(error);
391 TestHelper.assertError(cel, index, clazz, message);
392 return this;
393 }
394
395 }
396
397 @Test
398 public void onNextOnErrorCrash() {
399 List<Throwable> list = TestHelper.trackPluginErrors();
400
401 try {
402 CrashDummy cd = new CrashDummy(false, 1, true, false, false);
403 SafeObserver<Object> so = cd.toSafe();
404 so.onSubscribe(cd);
405
406 so.onNext(1);
407
408 TestHelper.assertError(list, 0, CompositeException.class);
409 List<Throwable> ce = TestHelper.compositeList(list.get(0));
410 TestHelper.assertError(ce, 0, TestException.class, "onNext(1)");
411 TestHelper.assertError(ce, 1, TestException.class, "onError(io.reactivex.rxjava3.exceptions.TestException: onNext(1))");
412 } finally {
413 RxJavaPlugins.reset();
414 }
415 }
416
417 @Test
418 public void onNextDisposeCrash() {
419 CrashDummy cd = new CrashDummy(false, 1, false, false, true);
420 SafeObserver<Object> so = cd.toSafe();
421 so.onSubscribe(cd);
422
423 so.onNext(1);
424
425 cd.assertError(CompositeException.class);
426 cd.assertInnerError(0, TestException.class, "onNext(1)");
427 cd.assertInnerError(1, TestException.class, "dispose()");
428 }
429
430 @Test
431 public void onSubscribeTwice() {
432 List<Throwable> list = TestHelper.trackPluginErrors();
433
434 try {
435 CrashDummy cd = new CrashDummy(false, 1, false, false, false);
436 SafeObserver<Object> so = cd.toSafe();
437 so.onSubscribe(cd);
438 so.onSubscribe(cd);
439
440 TestHelper.assertError(list, 0, IllegalStateException.class);
441 } finally {
442 RxJavaPlugins.reset();
443 }
444 }
445
446 @Test
447 public void onSubscribeCrashes() {
448 List<Throwable> list = TestHelper.trackPluginErrors();
449
450 try {
451 CrashDummy cd = new CrashDummy(true, 1, false, false, false);
452 SafeObserver<Object> so = cd.toSafe();
453 so.onSubscribe(cd);
454
455 TestHelper.assertUndeliverable(list, 0, TestException.class, "onSubscribe()");
456 } finally {
457 RxJavaPlugins.reset();
458 }
459 }
460
461 @Test
462 public void onSubscribeAndDisposeCrashes() {
463 List<Throwable> list = TestHelper.trackPluginErrors();
464
465 try {
466 CrashDummy cd = new CrashDummy(true, 1, false, false, true);
467 SafeObserver<Object> so = cd.toSafe();
468 so.onSubscribe(cd);
469
470 TestHelper.assertError(list, 0, CompositeException.class);
471 List<Throwable> ce = TestHelper.compositeList(list.get(0));
472 TestHelper.assertError(ce, 0, TestException.class, "onSubscribe()");
473 TestHelper.assertError(ce, 1, TestException.class, "dispose()");
474 } finally {
475 RxJavaPlugins.reset();
476 }
477 }
478
479 @Test
480 public void onNextOnSubscribeCrash() {
481 List<Throwable> list = TestHelper.trackPluginErrors();
482
483 try {
484 CrashDummy cd = new CrashDummy(true, 1, false, false, false);
485 SafeObserver<Object> so = cd.toSafe();
486
487 so.onNext(1);
488
489 TestHelper.assertError(list, 0, CompositeException.class);
490 List<Throwable> ce = TestHelper.compositeList(list.get(0));
491 TestHelper.assertError(ce, 0, NullPointerException.class, "Subscription not set!");
492 TestHelper.assertError(ce, 1, TestException.class, "onSubscribe()");
493 } finally {
494 RxJavaPlugins.reset();
495 }
496 }
497
498 @Test
499 public void onNextNullDisposeCrashes() {
500 CrashDummy cd = new CrashDummy(false, 1, false, false, true);
501 SafeObserver<Object> so = cd.toSafe();
502 so.onSubscribe(cd);
503
504 so.onNext(null);
505
506 cd.assertInnerError(0, NullPointerException.class);
507 cd.assertInnerError(1, TestException.class, "dispose()");
508 }
509
510 @Test
511 public void noSubscribeOnErrorCrashes() {
512 List<Throwable> list = TestHelper.trackPluginErrors();
513
514 try {
515 CrashDummy cd = new CrashDummy(false, 1, true, false, false);
516 SafeObserver<Object> so = cd.toSafe();
517
518 so.onNext(1);
519
520 TestHelper.assertError(list, 0, CompositeException.class);
521 List<Throwable> ce = TestHelper.compositeList(list.get(0));
522 TestHelper.assertError(ce, 0, NullPointerException.class, "Subscription not set!");
523 TestHelper.assertError(ce, 1, TestException.class, "onError(java.lang.NullPointerException: Subscription not set!)");
524 } finally {
525 RxJavaPlugins.reset();
526 }
527 }
528
529 @Test
530 public void onErrorNull() {
531 CrashDummy cd = new CrashDummy(false, 1, false, false, false);
532 SafeObserver<Object> so = cd.toSafe();
533 so.onSubscribe(cd);
534
535 so.onError(null);
536
537 cd.assertError(NullPointerException.class);
538 }
539
540 @Test
541 public void onErrorNoSubscribeCrash() {
542 List<Throwable> list = TestHelper.trackPluginErrors();
543
544 try {
545 CrashDummy cd = new CrashDummy(true, 1, false, false, false);
546 SafeObserver<Object> so = cd.toSafe();
547
548 so.onError(new TestException());
549
550 TestHelper.assertError(list, 0, CompositeException.class);
551 List<Throwable> ce = TestHelper.compositeList(list.get(0));
552 TestHelper.assertError(ce, 0, TestException.class);
553 TestHelper.assertError(ce, 1, NullPointerException.class, "Subscription not set!");
554 } finally {
555 RxJavaPlugins.reset();
556 }
557 }
558
559 @Test
560 public void onErrorNoSubscribeOnErrorCrash() {
561 List<Throwable> list = TestHelper.trackPluginErrors();
562
563 try {
564 CrashDummy cd = new CrashDummy(false, 1, true, false, false);
565 SafeObserver<Object> so = cd.toSafe();
566
567 so.onError(new TestException());
568
569 TestHelper.assertError(list, 0, CompositeException.class);
570 List<Throwable> ce = TestHelper.compositeList(list.get(0));
571 TestHelper.assertError(ce, 0, TestException.class);
572 TestHelper.assertError(ce, 1, NullPointerException.class, "Subscription not set!");
573 TestHelper.assertError(ce, 2, TestException.class);
574 } finally {
575 RxJavaPlugins.reset();
576 }
577 }
578
579 @Test
580 public void onCompleteteCrash() {
581 List<Throwable> list = TestHelper.trackPluginErrors();
582
583 try {
584 CrashDummy cd = new CrashDummy(false, 1, false, true, false);
585 SafeObserver<Object> so = cd.toSafe();
586
587 so.onSubscribe(cd);
588
589 so.onComplete();
590
591 TestHelper.assertUndeliverable(list, 0, TestException.class, "onComplete()");
592 } finally {
593 RxJavaPlugins.reset();
594 }
595 }
596
597 @Test
598 public void onCompleteteNoSubscribeCrash() {
599 List<Throwable> list = TestHelper.trackPluginErrors();
600
601 try {
602 CrashDummy cd = new CrashDummy(true, 1, false, true, false);
603 SafeObserver<Object> so = cd.toSafe();
604
605 so.onComplete();
606
607 TestHelper.assertError(list, 0, CompositeException.class);
608 List<Throwable> ce = TestHelper.compositeList(list.get(0));
609 TestHelper.assertError(ce, 0, NullPointerException.class, "Subscription not set!");
610 TestHelper.assertError(ce, 1, TestException.class, "onSubscribe()");
611 } finally {
612 RxJavaPlugins.reset();
613 }
614 }
615
616 @Test
617 public void onCompleteteNoSubscribeOnErrorCrash() {
618 List<Throwable> list = TestHelper.trackPluginErrors();
619
620 try {
621 CrashDummy cd = new CrashDummy(false, 1, true, true, false);
622 SafeObserver<Object> so = cd.toSafe();
623
624 so.onComplete();
625
626 TestHelper.assertError(list, 0, CompositeException.class);
627 List<Throwable> ce = TestHelper.compositeList(list.get(0));
628 TestHelper.assertError(ce, 0, NullPointerException.class, "Subscription not set!");
629 TestHelper.assertError(ce, 1, TestException.class);
630 } finally {
631 RxJavaPlugins.reset();
632 }
633 }
634
635 }