@Override public void onComplete(@NonNull Task<Void> task) { if (isDisposed()) return; if (!task.isSuccessful()) { Exception exception = task.getException(); if (terminated) { RxJavaPlugins.onError(exception); } else { try { terminated = true; observer.onError(exception); } catch (Throwable t) { Exceptions.throwIfFatal(t); RxJavaPlugins.onError(new CompositeException(task.getException(), t)); } } } }
@Override public Disposable schedule(Runnable run, long delay, TimeUnit unit) { if (run == null) throw new NullPointerException("run == null"); if (unit == null) throw new NullPointerException("unit == null"); if (disposed) { return Disposables.disposed(); } run = RxJavaPlugins.onSchedule(run); ScheduledRunnable scheduled = new ScheduledRunnable(handler, run); Message message = Message.obtain(handler, scheduled); message.obj = this; // Used as token for batch disposal of this worker's runnables. handler.sendMessageDelayed(message, Math.max(0L, unit.toMillis(delay))); // Re-check disposed state for removing in case we were racing a call to dispose(). if (disposed) { handler.removeCallbacks(scheduled); return Disposables.disposed(); } return scheduled; }
@Test public void directScheduleOnceWithDelayUsesHook() { final CountingRunnable newCounter = new CountingRunnable(); final AtomicReference<Runnable> runnableRef = new AtomicReference<>(); RxJavaPlugins.setScheduleHandler(new Function<Runnable, Runnable>() { @Override public Runnable apply(Runnable runnable) { runnableRef.set(runnable); return newCounter; } }); CountingRunnable counter = new CountingRunnable(); scheduler.scheduleDirect(counter, 1, MINUTES); // Verify our runnable was passed to the schedulers hook. assertSame(counter, runnableRef.get()); idleMainLooper(1, MINUTES); runUiThreadTasks(); // Verify the scheduled runnable was the one returned from the hook. assertEquals(1, newCounter.get()); assertEquals(0, counter.get()); }
@Test public void workerScheduleOnceUsesHook() { final CountingRunnable newCounter = new CountingRunnable(); final AtomicReference<Runnable> runnableRef = new AtomicReference<>(); RxJavaPlugins.setScheduleHandler(new Function<Runnable, Runnable>() { @Override public Runnable apply(Runnable runnable) { runnableRef.set(runnable); return newCounter; } }); Worker worker = scheduler.createWorker(); CountingRunnable counter = new CountingRunnable(); worker.schedule(counter); // Verify our runnable was passed to the schedulers hook. assertSame(counter, runnableRef.get()); runUiThreadTasks(); // Verify the scheduled runnable was the one returned from the hook. assertEquals(1, newCounter.get()); assertEquals(0, counter.get()); }
@Test public void workerScheduleOnceWithDelayUsesHook() { final CountingRunnable newCounter = new CountingRunnable(); final AtomicReference<Runnable> runnableRef = new AtomicReference<>(); RxJavaPlugins.setScheduleHandler(new Function<Runnable, Runnable>() { @Override public Runnable apply(Runnable runnable) { runnableRef.set(runnable); return newCounter; } }); Worker worker = scheduler.createWorker(); CountingRunnable counter = new CountingRunnable(); worker.schedule(counter, 1, MINUTES); // Verify our runnable was passed to the schedulers hook. assertSame(counter, runnableRef.get()); idleMainLooper(1, MINUTES); runUiThreadTasks(); // Verify the scheduled runnable was the one returned from the hook. assertEquals(1, newCounter.get()); assertEquals(0, counter.get()); }
@Override public void run() { Runnable r = get(); if (r != null && compareAndSet(r, null)) { try { try { r.run(); } catch (Throwable ex) { Exceptions.throwIfFatal(ex); RxJavaPlugins.onError(ex); } } finally { remove(this); } } }
@Override public void onResponse(Call<T> call, Response<T> response) { if (call.isCanceled()) return; try { observer.onNext(response); if (!call.isCanceled()) { terminated = true; observer.onComplete(); } } catch (Throwable t) { if (terminated) { RxJavaPlugins.onError(t); } else if (!call.isCanceled()) { try { observer.onError(t); } catch (Throwable inner) { Exceptions.throwIfFatal(inner); RxJavaPlugins.onError(new CompositeException(t, inner)); } } } }
@Override protected void subscribeActual(MaybeObserver<? super T> s) { MaybeObserver<? super T> observer; try { observer = ObjectHelper.requireNonNull(s, "Null Observer"); } catch (Throwable e) { Exceptions.throwIfFatal(e); // can't call onError because no way to know if a Disposable has been set or not // can't call onSubscribe because the call might have set a Disposable already RxJavaPlugins.onError(e); NullPointerException npe = new NullPointerException("Actually not, but can't throw other exceptions due to RS"); npe.initCause(e); throw npe; } source.subscribe(new AttachMaybeObserver<>(observer, this.compositeDisposable)); }
@Override protected void subscribeActual(SingleObserver<? super T> s) { SingleObserver<? super T> observer; try { observer = ObjectHelper.requireNonNull(s, "Null Observer"); } catch (Throwable e) { Exceptions.throwIfFatal(e); // can't call onError because no way to know if a Disposable has been set or not // can't call onSubscribe because the call might have set a Disposable already RxJavaPlugins.onError(e); NullPointerException npe = new NullPointerException("Actually not, but can't throw other exceptions due to RS"); npe.initCause(e); throw npe; } source.subscribe(new AttachSingleObserver<>(observer, this.compositeDisposable)); }
@Test public void responseThrowingInOnSuccessDeliveredToPlugin() { server.enqueue(new MockResponse()); final AtomicReference<Throwable> throwableRef = new AtomicReference<>(); RxJavaPlugins.setErrorHandler(new Consumer<Throwable>() { @Override public void accept(Throwable throwable) throws Exception { if (!throwableRef.compareAndSet(null, throwable)) { throw Exceptions.propagate(throwable); } } }); RecordingMaybeObserver<Response<String>> observer = subscriberRule.create(); final RuntimeException e = new RuntimeException(); service.response().subscribe(new ForwardingObserver<Response<String>>(observer) { @Override public void onSuccess(Response<String> value) { throw e; } }); assertThat(throwableRef.get()).isSameAs(e); }
@Test public void resultThrowingInOnSuccessDeliveredToPlugin() { server.enqueue(new MockResponse()); final AtomicReference<Throwable> throwableRef = new AtomicReference<>(); RxJavaPlugins.setErrorHandler(new Consumer<Throwable>() { @Override public void accept(Throwable throwable) throws Exception { if (!throwableRef.compareAndSet(null, throwable)) { throw Exceptions.propagate(throwable); } } }); RecordingMaybeObserver<Result<String>> observer = subscriberRule.create(); final RuntimeException e = new RuntimeException(); service.result().subscribe(new ForwardingObserver<Result<String>>(observer) { @Override public void onSuccess(Result<String> value) { throw e; } }); assertThat(throwableRef.get()).isSameAs(e); }
@Test public void throwingInOnCompleteDeliveredToPlugin() { server.enqueue(new MockResponse()); final AtomicReference<Throwable> errorRef = new AtomicReference<>(); RxJavaPlugins.setErrorHandler(new Consumer<Throwable>() { @Override public void accept(Throwable throwable) throws Exception { if (!errorRef.compareAndSet(null, throwable)) { throw Exceptions.propagate(throwable); // Don't swallow secondary errors! } } }); RecordingCompletableObserver observer = observerRule.create(); final RuntimeException e = new RuntimeException(); service.completable().subscribe(new ForwardingCompletableObserver(observer) { @Override public void onComplete() { throw e; } }); assertThat(errorRef.get()).isSameAs(e); }
@Test public void bodyThrowingInOnCompleteDeliveredToPlugin() { server.enqueue(new MockResponse()); final AtomicReference<Throwable> throwableRef = new AtomicReference<>(); RxJavaPlugins.setErrorHandler(new Consumer<Throwable>() { @Override public void accept(Throwable throwable) throws Exception { if (!throwableRef.compareAndSet(null, throwable)) { throw Exceptions.propagate(throwable); } } }); RecordingObserver<String> observer = subscriberRule.create(); final RuntimeException e = new RuntimeException(); service.body().subscribe(new ForwardingObserver<String>(observer) { @Override public void onComplete() { throw e; } }); observer.assertAnyValue(); assertThat(throwableRef.get()).isSameAs(e); }
@Test public void bodyThrowingInOnSuccessDeliveredToPlugin() { server.enqueue(new MockResponse()); final AtomicReference<Throwable> throwableRef = new AtomicReference<>(); RxJavaPlugins.setErrorHandler(new Consumer<Throwable>() { @Override public void accept(Throwable throwable) throws Exception { if (!throwableRef.compareAndSet(null, throwable)) { throw Exceptions.propagate(throwable); } } }); RecordingSingleObserver<String> observer = subscriberRule.create(); final RuntimeException e = new RuntimeException(); service.body().subscribe(new ForwardingObserver<String>(observer) { @Override public void onSuccess(String value) { throw e; } }); assertThat(throwableRef.get()).isSameAs(e); }
@Test public void responseThrowingInOnSuccessDeliveredToPlugin() { server.enqueue(new MockResponse()); final AtomicReference<Throwable> throwableRef = new AtomicReference<>(); RxJavaPlugins.setErrorHandler(new Consumer<Throwable>() { @Override public void accept(Throwable throwable) throws Exception { if (!throwableRef.compareAndSet(null, throwable)) { throw Exceptions.propagate(throwable); } } }); RecordingSingleObserver<Response<String>> observer = subscriberRule.create(); final RuntimeException e = new RuntimeException(); service.response().subscribe(new ForwardingObserver<Response<String>>(observer) { @Override public void onSuccess(Response<String> value) { throw e; } }); assertThat(throwableRef.get()).isSameAs(e); }
@Test public void resultThrowingInOnSuccessDeliveredToPlugin() { server.enqueue(new MockResponse()); final AtomicReference<Throwable> throwableRef = new AtomicReference<>(); RxJavaPlugins.setErrorHandler(new Consumer<Throwable>() { @Override public void accept(Throwable throwable) throws Exception { if (!throwableRef.compareAndSet(null, throwable)) { throw Exceptions.propagate(throwable); } } }); RecordingSingleObserver<Result<String>> observer = subscriberRule.create(); final RuntimeException e = new RuntimeException(); service.result().subscribe(new ForwardingObserver<Result<String>>(observer) { @Override public void onSuccess(Result<String> value) { throw e; } }); assertThat(throwableRef.get()).isSameAs(e); }
@Test public void bodyThrowingInOnCompleteDeliveredToPlugin() { server.enqueue(new MockResponse()); final AtomicReference<Throwable> throwableRef = new AtomicReference<>(); RxJavaPlugins.setErrorHandler(new Consumer<Throwable>() { @Override public void accept(Throwable throwable) throws Exception { if (!throwableRef.compareAndSet(null, throwable)) { throw Exceptions.propagate(throwable); } } }); RecordingSubscriber<String> subscriber = subscriberRule.create(); final RuntimeException e = new RuntimeException(); service.body().subscribe(new ForwardingSubscriber<String>(subscriber) { @Override public void onComplete() { throw e; } }); subscriber.assertAnyValue(); assertThat(throwableRef.get()).isSameAs(e); }
@Test public void responseThrowingInOnCompleteDeliveredToPlugin() { server.enqueue(new MockResponse()); final AtomicReference<Throwable> throwableRef = new AtomicReference<>(); RxJavaPlugins.setErrorHandler(new Consumer<Throwable>() { @Override public void accept(Throwable throwable) throws Exception { if (!throwableRef.compareAndSet(null, throwable)) { throw Exceptions.propagate(throwable); } } }); RecordingSubscriber<Response<String>> subscriber = subscriberRule.create(); final RuntimeException e = new RuntimeException(); service.response().subscribe(new ForwardingSubscriber<Response<String>>(subscriber) { @Override public void onComplete() { throw e; } }); subscriber.assertAnyValue(); assertThat(throwableRef.get()).isSameAs(e); }
@Override public void onComplete(@NonNull Task<T> task) { if (isDisposed()) return; if (task.isSuccessful()) { T result = task.getResult(); if (result == null) { observer.onComplete(); } else { observer.onSuccess(result); } } else { try { observer.onError(task.getException()); } catch (Throwable t) { Exceptions.throwIfFatal(t); RxJavaPlugins.onError(new CompositeException(task.getException(), t)); } } }
@Test public void bodyThrowingInOnSuccessDeliveredToPlugin() { server.enqueue(new MockResponse()); final AtomicReference<Throwable> throwableRef = new AtomicReference<>(); RxJavaPlugins.setErrorHandler(new Consumer<Throwable>() { @Override public void accept(Throwable throwable) throws Exception { if (!throwableRef.compareAndSet(null, throwable)) { throw Exceptions.propagate(throwable); } } }); RecordingMaybeObserver<String> observer = subscriberRule.create(); final RuntimeException e = new RuntimeException(); service.body().subscribe(new ForwardingObserver<String>(observer) { @Override public void onSuccess(String value) { throw e; } }); assertThat(throwableRef.get()).isSameAs(e); }
@Override public Disposable schedule(Runnable run, long delay, TimeUnit unit) { if (run == null) throw new NullPointerException("run == null"); if (unit == null) throw new NullPointerException("unit == null"); if (disposed) { return Disposables.disposed(); } run = RxJavaPlugins.onSchedule(run); ScheduledRunnable scheduled = new ScheduledRunnable(run); executeRunnable(title, delay, unit, scheduled); // Re-check disposed state for removing in case we were racing a // call to dispose(). if (disposed) { return Disposables.disposed(); } return scheduled; }
@Test public void responseThrowingInOnCompleteDeliveredToPlugin() { server.enqueue(new MockResponse()); final AtomicReference<Throwable> throwableRef = new AtomicReference<>(); RxJavaPlugins.setErrorHandler(new Consumer<Throwable>() { @Override public void accept(Throwable throwable) throws Exception { if (!throwableRef.compareAndSet(null, throwable)) { throw Exceptions.propagate(throwable); } } }); RecordingObserver<Response<String>> observer = subscriberRule.create(); final RuntimeException e = new RuntimeException(); service.response().subscribe(new ForwardingObserver<Response<String>>(observer) { @Override public void onComplete() { throw e; } }); observer.assertAnyValue(); assertThat(throwableRef.get()).isSameAs(e); }
@Test public void resultThrowingInOnCompletedDeliveredToPlugin() { server.enqueue(new MockResponse()); final AtomicReference<Throwable> throwableRef = new AtomicReference<>(); RxJavaPlugins.setErrorHandler(new Consumer<Throwable>() { @Override public void accept(Throwable throwable) throws Exception { if (!throwableRef.compareAndSet(null, throwable)) { throw Exceptions.propagate(throwable); } } }); RecordingObserver<Result<String>> observer = subscriberRule.create(); final RuntimeException e = new RuntimeException(); service.result().subscribe(new ForwardingObserver<Result<String>>(observer) { @Override public void onComplete() { throw e; } }); observer.assertAnyValue(); assertThat(throwableRef.get()).isSameAs(e); }
@Test public void throwingInOnCompleteDeliveredToPlugin() throws InterruptedException { server.enqueue(new MockResponse()); final CountDownLatch latch = new CountDownLatch(1); final AtomicReference<Throwable> errorRef = new AtomicReference<>(); RxJavaPlugins.setErrorHandler(new Consumer<Throwable>() { @Override public void accept(Throwable throwable) throws Exception { if (!errorRef.compareAndSet(null, throwable)) { throw Exceptions.propagate(throwable); // Don't swallow secondary errors! } latch.countDown(); } }); TestObserver<Void> observer = new TestObserver<>(); final RuntimeException e = new RuntimeException(); service.completable().subscribe(new ForwardingCompletableObserver(observer) { @Override public void onComplete() { throw e; } }); latch.await(1, SECONDS); assertThat(errorRef.get()).isSameAs(e); }
@BeforeClass public static void setupRxJavaPlugins() { RxJavaPlugins.setIoSchedulerHandler(new Function<Scheduler, Scheduler>() { @Override public Scheduler apply(Scheduler scheduler) throws Exception { return Schedulers.trampoline(); } }); RxAndroidPlugins.setInitMainThreadSchedulerHandler(new Function<Callable<Scheduler>, Scheduler>() { @Override public Scheduler apply(Callable<Scheduler> schedulerCallable) throws Exception { return Schedulers.trampoline(); } }); }
@Override public Statement apply(final Statement base, Description description) { return new Statement() { @Override public void evaluate() throws Throwable { RxJavaPlugins.setIoSchedulerHandler(scheduler -> Schedulers.trampoline()); RxJavaPlugins.setComputationSchedulerHandler(scheduler -> Schedulers.trampoline()); RxJavaPlugins.setNewThreadSchedulerHandler(scheduler -> Schedulers.trampoline()); RxAndroidPlugins.setMainThreadSchedulerHandler(scheduler -> Schedulers.trampoline()); try { base.evaluate(); } finally { RxJavaPlugins.reset(); } } }; }
@BeforeClass public static void setupClass() { RxAndroidPlugins.setInitMainThreadSchedulerHandler(new Function<Callable<Scheduler>, Scheduler>() { @Override public Scheduler apply(Callable<Scheduler> schedulerCallable) throws Exception { return Schedulers.trampoline(); } }); RxJavaPlugins.setIoSchedulerHandler(new Function<Scheduler, Scheduler>() { @Override public Scheduler apply(Scheduler scheduler) throws Exception { return Schedulers.trampoline(); } }); }
@Override public Statement apply(final Statement base, Description description) { return new Statement() { @Override public void evaluate() throws Throwable { RxJavaPlugins.reset(); RxJavaPlugins.setIoSchedulerHandler(scheduler -> Schedulers.trampoline()); RxJavaPlugins.setComputationSchedulerHandler(scheduler -> Schedulers.trampoline()); RxJavaPlugins.setNewThreadSchedulerHandler(scheduler -> Schedulers.trampoline()); try { base.evaluate(); } finally { RxJavaPlugins.reset(); } } }; }
@Override public Statement apply(Statement base, Description description) { return new Statement() { @Override public void evaluate() throws Throwable { RxAndroidPlugins.reset(); RxAndroidPlugins.setInitMainThreadSchedulerHandler(scheduler -> Schedulers.trampoline()); RxJavaPlugins.reset(); RxJavaPlugins.setIoSchedulerHandler(schedulerCallable -> Schedulers.trampoline()); base.evaluate(); RxAndroidPlugins.reset(); RxJavaPlugins.reset(); } }; }
@Override public void onCreate() { super.onCreate(); Fabric.with(this, new Crashlytics(), new Answers()); Log.useEnvironment(BuildConfig.DEBUG ? Log.Environment.DEBUG : Log.Environment.RELEASE); Log.i(TAG, "APP LAUNCHED"); BusProvider.getBus().register(this); sInstance = this; RxJavaPlugins.setErrorHandler(this::uncaughtRxException); setupMetadataRealm(); setupFonts(); initOkHttpClient(); initPicasso(); NetworkService networkService = new NetworkService(); mHACKListener = networkService; networkService.start(mOkHttpClient); mAnalyticsService = new AnalyticsService(BusProvider.getBus()); mAnalyticsService.start(); }
/** * Disable the assembly tracking. */ public static void disable() { if (lock.compareAndSet(false, true)) { RxJavaPlugins.setOnCompletableAssembly(null); RxJavaPlugins.setOnSingleAssembly(null); RxJavaPlugins.setOnMaybeAssembly(null); RxJavaPlugins.setOnObservableAssembly(null); RxJavaPlugins.setOnFlowableAssembly(null); RxJavaPlugins.setOnConnectableObservableAssembly(null); RxJavaPlugins.setOnConnectableFlowableAssembly(null); RxJavaPlugins.setOnParallelAssembly(null); lock.set(false); } }
/** * Class constructor */ @Inject public TaskManager(AppConfig config, Set<ExtractProcessor<? extends Extract>> extractProcessors, Set<LoadProcessor<? extends Load>> loadProcessors, ShutdownHook shutdownHook) { this.extractProcessors = extractProcessors; this.loadProcessors = loadProcessors; this.runOnce = config.global().runOnce(); this.shutdownHook = shutdownHook; // prioritize tasks based on their place in the pipeline Integer ioPoolSize = config.global().ioPoolsThreadSize(); extractScheduler = initExtractScheduler(ioPoolSize); transformScheduler = initTransformScheduler(ioPoolSize); loadScheduler = initLoadScheduler(ioPoolSize); // handle irrecoverable errors: allow graceful shutdown RxJavaPlugins.setErrorHandler(throwable -> { onError(throwable); // mark processing as completed after a short delay Flowable.timer(1, TimeUnit.SECONDS).doOnNext((i) -> notifyCompleted()).subscribe(); }); }
@Override public void onError(Throwable throwable) { try { Result<R> error = Result.error(throwable); observer.onNext( new ProgressBean<>(-1, -1, error) ); } catch (Throwable t) { try { observer.onError(t); } catch (Throwable inner) { Exceptions.throwIfFatal(inner); RxJavaPlugins.onError(new CompositeException(t, inner)); } return; } observer.onComplete(); }