Debian Codegraph @

src/test/java/io/reactivex/rxjava3/observable/ObservableErrorHandlingTests.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.observable;
15
16 import static org.junit.Assert.assertNotNull;
17
18 import java.util.concurrent.*;
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.observers.DefaultObserver;
25 import io.reactivex.rxjava3.schedulers.Schedulers;
26
27 public class ObservableErrorHandlingTests extends RxJavaTest {
28
29 /**
30 * Test that an error from a user provided Observer.onNext is handled and emitted to the onError.
31 * @throws InterruptedException if the test is interrupted
32 */
33 @Test
34 public void onNextError() throws InterruptedException {
35 final CountDownLatch latch = new CountDownLatch(1);
36 final AtomicReference<Throwable> caughtError = new AtomicReference<>();
37 Observable<Long> o = Observable.interval(50, TimeUnit.MILLISECONDS);
38 Observer<Long> observer = new DefaultObserver<Long>() {
39
40 @Override
41 public void onComplete() {
42 System.out.println("completed");
43 latch.countDown();
44 }
45
46 @Override
47 public void onError(Throwable e) {
48 System.out.println("error: " + e);
49 caughtError.set(e);
50 latch.countDown();
51 }
52
53 @Override
54 public void onNext(Long args) {
55 throw new RuntimeException("forced failure");
56 }
57 };
58 o.safeSubscribe(observer);
59
60 latch.await(2000, TimeUnit.MILLISECONDS);
61 assertNotNull(caughtError.get());
62 }
63
64 /**
65 * Test that an error from a user provided Observer.onNext is handled and emitted to the onError
66 * even when done across thread boundaries with observeOn.
67 * @throws InterruptedException if the test is interrupted
68 */
69 @Test
70 public void onNextErrorAcrossThread() throws InterruptedException {
71 final CountDownLatch latch = new CountDownLatch(1);
72 final AtomicReference<Throwable> caughtError = new AtomicReference<>();
73 Observable<Long> o = Observable.interval(50, TimeUnit.MILLISECONDS);
74 Observer<Long> observer = new DefaultObserver<Long>() {
75
76 @Override
77 public void onComplete() {
78 System.out.println("completed");
79 latch.countDown();
80 }
81
82 @Override
83 public void onError(Throwable e) {
84 System.out.println("error: " + e);
85 caughtError.set(e);
86 latch.countDown();
87 }
88
89 @Override
90 public void onNext(Long args) {
91 throw new RuntimeException("forced failure");
92 }
93 };
94 o.observeOn(Schedulers.newThread())
95 .safeSubscribe(observer);
96
97 latch.await(2000, TimeUnit.MILLISECONDS);
98 assertNotNull(caughtError.get());
99 }
100 }