Search in sources :

Example 1 with ComputationScheduler

use of io.reactivex.rxjava3.internal.schedulers.ComputationScheduler in project RxJava by ReactiveX.

the class ComputationSchedulerTests method shutdownRejects.

@Test
@SuppressUndeliverable
public void shutdownRejects() {
    final int[] calls = { 0 };
    Runnable r = new Runnable() {

        @Override
        public void run() {
            calls[0]++;
        }
    };
    Scheduler s = new ComputationScheduler();
    s.shutdown();
    s.shutdown();
    assertEquals(Disposable.disposed(), s.scheduleDirect(r));
    assertEquals(Disposable.disposed(), s.scheduleDirect(r, 1, TimeUnit.SECONDS));
    assertEquals(Disposable.disposed(), s.schedulePeriodicallyDirect(r, 1, 1, TimeUnit.SECONDS));
    Worker w = s.createWorker();
    w.dispose();
    assertTrue(w.isDisposed());
    assertEquals(Disposable.disposed(), w.schedule(r));
    assertEquals(Disposable.disposed(), w.schedule(r, 1, TimeUnit.SECONDS));
    assertEquals(Disposable.disposed(), w.schedulePeriodically(r, 1, 1, TimeUnit.SECONDS));
    assertEquals(0, calls[0]);
}
Also used : ComputationScheduler(io.reactivex.rxjava3.internal.schedulers.ComputationScheduler) Worker(io.reactivex.rxjava3.core.Scheduler.Worker) ComputationScheduler(io.reactivex.rxjava3.internal.schedulers.ComputationScheduler) Test(org.junit.Test) SuppressUndeliverable(io.reactivex.rxjava3.testsupport.SuppressUndeliverable)

Example 2 with ComputationScheduler

use of io.reactivex.rxjava3.internal.schedulers.ComputationScheduler in project RxJava by ReactiveX.

the class ComputationSchedulerTests method exceptionFromObservableShouldNotBeSwallowed.

@Test
public void exceptionFromObservableShouldNotBeSwallowed() throws Exception {
    CountDownLatch latch = new CountDownLatch(1);
    // #3 thread's uncaught exception handler
    Scheduler computationScheduler = new ComputationScheduler(new ThreadFactory() {

        @Override
        public Thread newThread(Runnable r) {
            Thread t = new Thread(r);
            t.setUncaughtExceptionHandler((thread, throwable) -> {
                latch.countDown();
            });
            return t;
        }
    });
    // #2 RxJava exception handler
    RxJavaPlugins.setErrorHandler(h -> {
        latch.countDown();
    });
    // and should not be swallowed.
    try {
        // #1 observer's onError()
        Observable.create(s -> {
            s.onNext(1);
            throw new OutOfMemoryError();
        }).subscribeOn(computationScheduler).subscribe(v -> {
        }, e -> {
            latch.countDown();
        });
        assertTrue(latch.await(2, TimeUnit.SECONDS));
    } finally {
        RxJavaPlugins.reset();
        computationScheduler.shutdown();
    }
}
Also used : AtomicInteger(java.util.concurrent.atomic.AtomicInteger) io.reactivex.rxjava3.functions(io.reactivex.rxjava3.functions) ComputationScheduler(io.reactivex.rxjava3.internal.schedulers.ComputationScheduler) java.util.concurrent(java.util.concurrent) Disposable(io.reactivex.rxjava3.disposables.Disposable) HashMap(java.util.HashMap) Test(org.junit.Test) io.reactivex.rxjava3.core(io.reactivex.rxjava3.core) Assert(org.junit.Assert) SuppressUndeliverable(io.reactivex.rxjava3.testsupport.SuppressUndeliverable) Worker(io.reactivex.rxjava3.core.Scheduler.Worker) RxJavaPlugins(io.reactivex.rxjava3.plugins.RxJavaPlugins) ComputationScheduler(io.reactivex.rxjava3.internal.schedulers.ComputationScheduler) ComputationScheduler(io.reactivex.rxjava3.internal.schedulers.ComputationScheduler) Test(org.junit.Test)

Example 3 with ComputationScheduler

use of io.reactivex.rxjava3.internal.schedulers.ComputationScheduler in project RxJava by ReactiveX.

the class ComputationSchedulerTests method exceptionFromObserverShouldNotBeSwallowed.

@Test
public void exceptionFromObserverShouldNotBeSwallowed() throws Exception {
    CountDownLatch latch = new CountDownLatch(1);
    // #3 thread's uncaught exception handler
    Scheduler computationScheduler = new ComputationScheduler(new ThreadFactory() {

        @Override
        public Thread newThread(Runnable r) {
            Thread t = new Thread(r);
            t.setUncaughtExceptionHandler((thread, throwable) -> {
                latch.countDown();
            });
            return t;
        }
    });
    // #2 RxJava exception handler
    RxJavaPlugins.setErrorHandler(h -> {
        latch.countDown();
    });
    // and should not be swallowed.
    try {
        // #1 observer's onError()
        Flowable.interval(500, TimeUnit.MILLISECONDS, computationScheduler).subscribe(v -> {
            throw new OutOfMemoryError();
        }, e -> {
            latch.countDown();
        });
        assertTrue(latch.await(2, TimeUnit.SECONDS));
    } finally {
        RxJavaPlugins.reset();
        computationScheduler.shutdown();
    }
}
Also used : AtomicInteger(java.util.concurrent.atomic.AtomicInteger) io.reactivex.rxjava3.functions(io.reactivex.rxjava3.functions) ComputationScheduler(io.reactivex.rxjava3.internal.schedulers.ComputationScheduler) java.util.concurrent(java.util.concurrent) Disposable(io.reactivex.rxjava3.disposables.Disposable) HashMap(java.util.HashMap) Test(org.junit.Test) io.reactivex.rxjava3.core(io.reactivex.rxjava3.core) Assert(org.junit.Assert) SuppressUndeliverable(io.reactivex.rxjava3.testsupport.SuppressUndeliverable) Worker(io.reactivex.rxjava3.core.Scheduler.Worker) RxJavaPlugins(io.reactivex.rxjava3.plugins.RxJavaPlugins) ComputationScheduler(io.reactivex.rxjava3.internal.schedulers.ComputationScheduler) ComputationScheduler(io.reactivex.rxjava3.internal.schedulers.ComputationScheduler) Test(org.junit.Test)

Aggregations

Worker (io.reactivex.rxjava3.core.Scheduler.Worker)3 ComputationScheduler (io.reactivex.rxjava3.internal.schedulers.ComputationScheduler)3 SuppressUndeliverable (io.reactivex.rxjava3.testsupport.SuppressUndeliverable)3 Test (org.junit.Test)3 io.reactivex.rxjava3.core (io.reactivex.rxjava3.core)2 Disposable (io.reactivex.rxjava3.disposables.Disposable)2 io.reactivex.rxjava3.functions (io.reactivex.rxjava3.functions)2 RxJavaPlugins (io.reactivex.rxjava3.plugins.RxJavaPlugins)2 HashMap (java.util.HashMap)2 java.util.concurrent (java.util.concurrent)2 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)2 Assert (org.junit.Assert)2