/** * Copyright 2016 Netflix, Inc. * * Licensed under the Apache License, Version 2.0 (the "License"); you may not * use this file except in compliance with the License. You may obtain a copy of * the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the * License for the specific language governing permissions and limitations under * the License. */ package rx.schedulers; import rx.CapturingUncaughtExceptionHandler; import rx.Observable; import rx.Observer; import rx.Scheduler; import rx.functions.Action0; import rx.functions.Actions; import rx.internal.schedulers.NewThreadWorker; import java.lang.management.ManagementFactory; import java.lang.management.MemoryMXBean; import java.lang.management.MemoryUsage; import java.util.concurrent.CountDownLatch; import java.util.concurrent.TimeUnit; import static org.junit.Assert.assertEquals; import static org.junit.Assert.fail; public final class SchedulerTests { private SchedulerTests() { // No instances. } /** * Verifies that the given Scheduler delivers unhandled errors to its executing thread's * {@link java.lang.Thread.UncaughtExceptionHandler}. *
* Schedulers which execute on a separate thread from their calling thread should exhibit this behavior. Schedulers * which execute on their calling thread may not. * * @param scheduler the scheduler to test * @throws InterruptedException if some wait is interrupted */ public static void testUnhandledErrorIsDeliveredToThreadHandler(Scheduler scheduler) throws InterruptedException { Thread.UncaughtExceptionHandler originalHandler = Thread.getDefaultUncaughtExceptionHandler(); try { CapturingUncaughtExceptionHandler handler = new CapturingUncaughtExceptionHandler(); Thread.setDefaultUncaughtExceptionHandler(handler); IllegalStateException error = new IllegalStateException("Should be delivered to handler"); Observable.error(error) .subscribeOn(scheduler) .subscribe(); if (!handler.completed.await(3, TimeUnit.SECONDS)) { fail("timed out"); } assertEquals("Should have received exactly 1 exception", 1, handler.count); Throwable cause = handler.caught; while (cause != null) { if (error.equals(cause)) break; if (cause == cause.getCause()) break; cause = cause.getCause(); } assertEquals("Our error should have been delivered to the handler", error, cause); } finally { Thread.setDefaultUncaughtExceptionHandler(originalHandler); } } /** * Verifies that the given Scheduler does not deliver handled errors to its executing Thread's * {@link java.lang.Thread.UncaughtExceptionHandler}. *
* This is a companion test to {@link #testUnhandledErrorIsDeliveredToThreadHandler}, and is needed only for the * same Schedulers. * @param scheduler the scheduler to test * @throws InterruptedException if some wait is interrupted */ public static void testHandledErrorIsNotDeliveredToThreadHandler(Scheduler scheduler) throws InterruptedException { Thread.UncaughtExceptionHandler originalHandler = Thread.getDefaultUncaughtExceptionHandler(); try { CapturingUncaughtExceptionHandler handler = new CapturingUncaughtExceptionHandler(); CapturingObserver