Debian Codegraph @

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