Search in sources :

Example 21 with Scheduler

use of io.reactivex.rxjava3.core.Scheduler in project RxJava by ReactiveX.

the class AbstractSchedulerTests method unwrapScheduleDirectTask.

@Test
public void unwrapScheduleDirectTask() {
    Scheduler scheduler = getScheduler();
    if (scheduler instanceof TrampolineScheduler) {
        // TrampolineScheduler always return EmptyDisposable
        return;
    }
    final CountDownLatch cdl = new CountDownLatch(1);
    Runnable countDownRunnable = new Runnable() {

        @Override
        public void run() {
            cdl.countDown();
        }
    };
    Disposable disposable = scheduler.scheduleDirect(countDownRunnable, 100, TimeUnit.MILLISECONDS);
    SchedulerRunnableIntrospection wrapper = (SchedulerRunnableIntrospection) disposable;
    assertSame(countDownRunnable, wrapper.getWrappedRunnable());
    disposable.dispose();
}
Also used : SequentialDisposable(io.reactivex.rxjava3.internal.disposables.SequentialDisposable) Disposable(io.reactivex.rxjava3.disposables.Disposable) TrampolineScheduler(io.reactivex.rxjava3.internal.schedulers.TrampolineScheduler) TrampolineScheduler(io.reactivex.rxjava3.internal.schedulers.TrampolineScheduler) Test(org.junit.Test)

Example 22 with Scheduler

use of io.reactivex.rxjava3.core.Scheduler 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 23 with Scheduler

use of io.reactivex.rxjava3.core.Scheduler 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 24 with Scheduler

use of io.reactivex.rxjava3.core.Scheduler in project RxJava by ReactiveX.

the class ExecutorSchedulerInterruptibleTest method runnableDisposedAsync2.

@Test
public void runnableDisposedAsync2() throws Exception {
    final Scheduler s = Schedulers.from(executor, true);
    Disposable d = s.scheduleDirect(Functions.EMPTY_RUNNABLE);
    while (!d.isDisposed()) {
        Thread.sleep(1);
    }
}
Also used : EmptyDisposable(io.reactivex.rxjava3.internal.disposables.EmptyDisposable) Disposable(io.reactivex.rxjava3.disposables.Disposable) Scheduler(io.reactivex.rxjava3.core.Scheduler) Test(org.junit.Test)

Example 25 with Scheduler

use of io.reactivex.rxjava3.core.Scheduler in project RxJava by ReactiveX.

the class ExecutorSchedulerInterruptibleTest method interruptibleDirectTask.

@Test
public void interruptibleDirectTask() throws Exception {
    Scheduler scheduler = getScheduler();
    final AtomicInteger sync = new AtomicInteger(2);
    final AtomicBoolean isInterrupted = new AtomicBoolean();
    Disposable d = scheduler.scheduleDirect(new Runnable() {

        @Override
        public void run() {
            if (sync.decrementAndGet() != 0) {
                while (sync.get() != 0) {
                }
            }
            try {
                Thread.sleep(1000);
            } catch (InterruptedException ex) {
                isInterrupted.set(true);
            }
        }
    });
    if (sync.decrementAndGet() != 0) {
        while (sync.get() != 0) {
        }
    }
    Thread.sleep(500);
    d.dispose();
    int i = 20;
    while (i-- > 0 && !isInterrupted.get()) {
        Thread.sleep(50);
    }
    assertTrue("Interruption did not propagate", isInterrupted.get());
}
Also used : EmptyDisposable(io.reactivex.rxjava3.internal.disposables.EmptyDisposable) Disposable(io.reactivex.rxjava3.disposables.Disposable) Scheduler(io.reactivex.rxjava3.core.Scheduler) Test(org.junit.Test)

Aggregations

Test (org.junit.Test)169 Disposable (io.reactivex.rxjava3.disposables.Disposable)69 Scheduler (io.reactivex.rxjava3.core.Scheduler)61 TestScheduler (io.reactivex.rxjava3.schedulers.TestScheduler)54 Worker (io.reactivex.rxjava3.core.Scheduler.Worker)50 TestException (io.reactivex.rxjava3.exceptions.TestException)34 EmptyDisposable (io.reactivex.rxjava3.internal.disposables.EmptyDisposable)32 InOrder (org.mockito.InOrder)32 TrampolineScheduler (io.reactivex.rxjava3.internal.schedulers.TrampolineScheduler)20 ImmediateThinScheduler (io.reactivex.rxjava3.internal.schedulers.ImmediateThinScheduler)13 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)10 BooleanSubscription (io.reactivex.rxjava3.internal.subscriptions.BooleanSubscription)9 EmptyScheduler (io.reactivex.rxjava3.android.testutil.EmptyScheduler)8 Observable (io.reactivex.rxjava3.core.Observable)7 RxJavaTest (io.reactivex.rxjava3.core.RxJavaTest)6 SequentialDisposable (io.reactivex.rxjava3.internal.disposables.SequentialDisposable)6 Action (io.reactivex.rxjava3.functions.Action)5 SuppressUndeliverable (io.reactivex.rxjava3.testsupport.SuppressUndeliverable)5 AtomicBoolean (java.util.concurrent.atomic.AtomicBoolean)5 AtomicReference (java.util.concurrent.atomic.AtomicReference)5