Debian Codegraph @

src/test/java/io/reactivex/rxjava3/schedulers/SchedulerTestHelper.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.schedulers;
15
16 import static org.junit.Assert.*;
17
18 import java.util.concurrent.*;
19
20 import io.reactivex.rxjava3.core.*;
21 import io.reactivex.rxjava3.subscribers.DefaultSubscriber;
22
23 final class SchedulerTestHelper {
24 private SchedulerTestHelper() {
25 // No instances.
26 }
27
28 /**
29 * Verifies that the given Scheduler does not deliver handled errors to its executing Thread's
30 * {@link java.lang.Thread.UncaughtExceptionHandler}.
31 *
32 * @param scheduler {@link Scheduler} to verify.
33 */
34 static void handledErrorIsNotDeliveredToThreadHandler(Scheduler scheduler) throws InterruptedException {
35 Thread.UncaughtExceptionHandler originalHandler = Thread.getDefaultUncaughtExceptionHandler();
36 try {
37 CapturingUncaughtExceptionHandler handler = new CapturingUncaughtExceptionHandler();
38 CapturingObserver<Object> observer = new CapturingObserver<>();
39 Thread.setDefaultUncaughtExceptionHandler(handler);
40 IllegalStateException error = new IllegalStateException("Should be delivered to handler");
41 Flowable.error(error)
42 .subscribeOn(scheduler)
43 .subscribe(observer);
44
45 if (!observer.completed.await(3, TimeUnit.SECONDS)) {
46 fail("timed out");
47 }
48
49 if (handler.count != 0) {
50 handler.caught.printStackTrace();
51 }
52 assertEquals("Handler should not have received anything: " + handler.caught, 0, handler.count);
53 assertEquals("Observer should have received an error", 1, observer.errorCount);
54 assertEquals("Observer should not have received a next value", 0, observer.nextCount);
55
56 Throwable cause = observer.error;
57 while (cause != null) {
58 if (error.equals(cause)) { break; }
59 if (cause == cause.getCause()) { break; }
60 cause = cause.getCause();
61 }
62 assertEquals("Our error should have been delivered to the observer", error, cause);
63 } finally {
64 Thread.setDefaultUncaughtExceptionHandler(originalHandler);
65 }
66 }
67
68 private static final class CapturingUncaughtExceptionHandler implements Thread.UncaughtExceptionHandler {
69 int count;
70 Throwable caught;
71 CountDownLatch completed = new CountDownLatch(1);
72
73 @Override
74 public void uncaughtException(Thread t, Throwable e) {
75 count++;
76 caught = e;
77 completed.countDown();
78 }
79 }
80
81 static final class CapturingObserver<T> extends DefaultSubscriber<T> {
82 CountDownLatch completed = new CountDownLatch(1);
83 int errorCount;
84 int nextCount;
85 Throwable error;
86
87 @Override
88 public void onComplete() {
89 }
90
91 @Override
92 public void onError(Throwable e) {
93 errorCount++;
94 error = e;
95 completed.countDown();
96 }
97
98 @Override
99 public void onNext(T t) {
100 nextCount++;
101 }
102 }
103 }