|
| 1 | +package rx.schedulers; |
| 2 | + |
| 3 | +import rx.Observable; |
| 4 | +import rx.Observer; |
| 5 | +import rx.Scheduler; |
| 6 | + |
| 7 | +import java.util.concurrent.CountDownLatch; |
| 8 | +import java.util.concurrent.TimeUnit; |
| 9 | + |
| 10 | +import static org.junit.Assert.assertEquals; |
| 11 | +import static org.junit.Assert.fail; |
| 12 | + |
| 13 | +final class SchedulerTests { |
| 14 | + private SchedulerTests() { |
| 15 | + // No instances. |
| 16 | + } |
| 17 | + |
| 18 | + /** |
| 19 | + * Verifies that the given Scheduler delivers unhandled errors to its executing thread's |
| 20 | + * {@link java.lang.Thread.UncaughtExceptionHandler}. |
| 21 | + * <p> |
| 22 | + * Schedulers which execute on a separate thread from their calling thread should exhibit this behavior. Schedulers |
| 23 | + * which execute on their calling thread may not. |
| 24 | + */ |
| 25 | + static void testUnhandledErrorIsDeliveredToThreadHandler(Scheduler scheduler) throws InterruptedException { |
| 26 | + Thread.UncaughtExceptionHandler originalHandler = Thread.getDefaultUncaughtExceptionHandler(); |
| 27 | + try { |
| 28 | + CapturingUncaughtExceptionHandler handler = new CapturingUncaughtExceptionHandler(); |
| 29 | + Thread.setDefaultUncaughtExceptionHandler(handler); |
| 30 | + IllegalStateException error = new IllegalStateException("Should be delivered to handler"); |
| 31 | + Observable.error(error) |
| 32 | + .subscribeOn(scheduler) |
| 33 | + .subscribe(); |
| 34 | + |
| 35 | + if (!handler.completed.await(3, TimeUnit.SECONDS)) { |
| 36 | + fail("timed out"); |
| 37 | + } |
| 38 | + |
| 39 | + assertEquals("Should have received exactly 1 exception", 1, handler.count); |
| 40 | + Throwable cause = handler.caught; |
| 41 | + while (cause != null) { |
| 42 | + if (error.equals(cause)) break; |
| 43 | + if (cause == cause.getCause()) break; |
| 44 | + cause = cause.getCause(); |
| 45 | + } |
| 46 | + assertEquals("Our error should have been delivered to the handler", error, cause); |
| 47 | + } finally { |
| 48 | + Thread.setDefaultUncaughtExceptionHandler(originalHandler); |
| 49 | + } |
| 50 | + } |
| 51 | + |
| 52 | + /** |
| 53 | + * Verifies that the given Scheduler does not deliver handled errors to its executing Thread's |
| 54 | + * {@link java.lang.Thread.UncaughtExceptionHandler}. |
| 55 | + * <p> |
| 56 | + * This is a companion test to {@link #testUnhandledErrorIsDeliveredToThreadHandler}, and is needed only for the |
| 57 | + * same Schedulers. |
| 58 | + */ |
| 59 | + static void testHandledErrorIsNotDeliveredToThreadHandler(Scheduler scheduler) throws InterruptedException { |
| 60 | + Thread.UncaughtExceptionHandler originalHandler = Thread.getDefaultUncaughtExceptionHandler(); |
| 61 | + try { |
| 62 | + CapturingUncaughtExceptionHandler handler = new CapturingUncaughtExceptionHandler(); |
| 63 | + CapturingObserver<Object> observer = new CapturingObserver<Object>(); |
| 64 | + Thread.setDefaultUncaughtExceptionHandler(handler); |
| 65 | + IllegalStateException error = new IllegalStateException("Should be delivered to handler"); |
| 66 | + Observable.error(error) |
| 67 | + .subscribeOn(scheduler) |
| 68 | + .subscribe(observer); |
| 69 | + |
| 70 | + if (!observer.completed.await(3, TimeUnit.SECONDS)) { |
| 71 | + fail("timed out"); |
| 72 | + } |
| 73 | + |
| 74 | + assertEquals("Handler should not have received anything", 0, handler.count); |
| 75 | + assertEquals("Observer should have received an error", 1, observer.errorCount); |
| 76 | + assertEquals("Observer should not have received a next value", 0, observer.nextCount); |
| 77 | + |
| 78 | + Throwable cause = observer.error; |
| 79 | + while (cause != null) { |
| 80 | + if (error.equals(cause)) break; |
| 81 | + if (cause == cause.getCause()) break; |
| 82 | + cause = cause.getCause(); |
| 83 | + } |
| 84 | + assertEquals("Our error should have been delivered to the observer", error, cause); |
| 85 | + } finally { |
| 86 | + Thread.setDefaultUncaughtExceptionHandler(originalHandler); |
| 87 | + } |
| 88 | + } |
| 89 | + |
| 90 | + private static final class CapturingUncaughtExceptionHandler implements Thread.UncaughtExceptionHandler { |
| 91 | + int count = 0; |
| 92 | + Throwable caught; |
| 93 | + CountDownLatch completed = new CountDownLatch(1); |
| 94 | + |
| 95 | + @Override |
| 96 | + public void uncaughtException(Thread t, Throwable e) { |
| 97 | + count++; |
| 98 | + caught = e; |
| 99 | + completed.countDown(); |
| 100 | + } |
| 101 | + } |
| 102 | + |
| 103 | + private static final class CapturingObserver<T> implements Observer<T> { |
| 104 | + CountDownLatch completed = new CountDownLatch(1); |
| 105 | + int errorCount = 0; |
| 106 | + int nextCount = 0; |
| 107 | + Throwable error; |
| 108 | + |
| 109 | + @Override |
| 110 | + public void onCompleted() { |
| 111 | + } |
| 112 | + |
| 113 | + @Override |
| 114 | + public void onError(Throwable e) { |
| 115 | + errorCount++; |
| 116 | + error = e; |
| 117 | + completed.countDown(); |
| 118 | + } |
| 119 | + |
| 120 | + @Override |
| 121 | + public void onNext(T t) { |
| 122 | + nextCount++; |
| 123 | + } |
| 124 | + } |
| 125 | +} |
0 commit comments