Search in sources :

Example 36 with Scheduler

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

the class TestSchedulerTest method workerDisposed.

@Test
public void workerDisposed() {
    TestScheduler scheduler = new TestScheduler();
    Worker w = scheduler.createWorker();
    w.dispose();
    assertTrue(w.isDisposed());
}
Also used : Worker(io.reactivex.rxjava3.core.Scheduler.Worker) TestScheduler(io.reactivex.rxjava3.schedulers.TestScheduler) Test(org.junit.Test)

Example 37 with Scheduler

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

the class TestSchedulerTest method nestedSchedule.

@Test
public final void nestedSchedule() {
    final TestScheduler scheduler = new TestScheduler();
    final Scheduler.Worker inner = scheduler.createWorker();
    try {
        final Runnable calledOp = mock(Runnable.class);
        Flowable<Object> poller;
        poller = Flowable.unsafeCreate(new Publisher<Object>() {

            @Override
            public void subscribe(final Subscriber<? super Object> aSubscriber) {
                final BooleanSubscription bs = new BooleanSubscription();
                aSubscriber.onSubscribe(bs);
                inner.schedule(new Runnable() {

                    @Override
                    public void run() {
                        if (!bs.isCancelled()) {
                            calledOp.run();
                            inner.schedule(this, 5, TimeUnit.SECONDS);
                        }
                    }
                });
            }
        });
        InOrder inOrder = Mockito.inOrder(calledOp);
        Disposable sub;
        sub = poller.subscribe();
        scheduler.advanceTimeTo(6, TimeUnit.SECONDS);
        inOrder.verify(calledOp, times(2)).run();
        sub.dispose();
        scheduler.advanceTimeTo(11, TimeUnit.SECONDS);
        inOrder.verify(calledOp, never()).run();
        sub = poller.subscribe();
        scheduler.advanceTimeTo(12, TimeUnit.SECONDS);
        inOrder.verify(calledOp, times(1)).run();
    } finally {
        inner.dispose();
    }
}
Also used : Disposable(io.reactivex.rxjava3.disposables.Disposable) BooleanSubscription(io.reactivex.rxjava3.internal.subscriptions.BooleanSubscription) Worker(io.reactivex.rxjava3.core.Scheduler.Worker) TestScheduler(io.reactivex.rxjava3.schedulers.TestScheduler) TestScheduler(io.reactivex.rxjava3.schedulers.TestScheduler) Test(org.junit.Test)

Example 38 with Scheduler

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

the class ExecutorSchedulerTest method rejectingExecutor.

@Test
public void rejectingExecutor() {
    ScheduledExecutorService exec = Executors.newSingleThreadScheduledExecutor();
    exec.shutdown();
    Scheduler s = Schedulers.from(exec);
    List<Throwable> errors = TestHelper.trackPluginErrors();
    try {
        assertSame(EmptyDisposable.INSTANCE, s.scheduleDirect(Functions.EMPTY_RUNNABLE));
        assertSame(EmptyDisposable.INSTANCE, s.scheduleDirect(Functions.EMPTY_RUNNABLE, 10, TimeUnit.MILLISECONDS));
        assertSame(EmptyDisposable.INSTANCE, s.schedulePeriodicallyDirect(Functions.EMPTY_RUNNABLE, 10, 10, TimeUnit.MILLISECONDS));
        TestHelper.assertUndeliverable(errors, 0, RejectedExecutionException.class);
        TestHelper.assertUndeliverable(errors, 1, RejectedExecutionException.class);
        TestHelper.assertUndeliverable(errors, 2, RejectedExecutionException.class);
    } finally {
        RxJavaPlugins.reset();
    }
}
Also used : Scheduler(io.reactivex.rxjava3.core.Scheduler) Test(org.junit.Test)

Example 39 with Scheduler

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

the class ExecutorSchedulerTest method unwrapScheduleDirectTaskAfterDispose.

@Test
public void unwrapScheduleDirectTaskAfterDispose() {
    Scheduler scheduler = getScheduler();
    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();
    assertSame(Functions.EMPTY_RUNNABLE, wrapper.getWrappedRunnable());
}
Also used : Disposable(io.reactivex.rxjava3.disposables.Disposable) Scheduler(io.reactivex.rxjava3.core.Scheduler) Test(org.junit.Test)

Example 40 with Scheduler

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

the class NewThreadSchedulerTest method npeRegression.

/**
 * Regression test to ensure there is no NPE when the worker has been disposed.
 * @throws Exception on error
 */
@Test
@SuppressUndeliverable
public void npeRegression() throws Exception {
    Scheduler s = getScheduler();
    NewThreadWorker w = (NewThreadWorker) s.createWorker();
    w.dispose();
    // This method used to throw a NPE when the worker has been disposed and the parent is null
    w.scheduleActual(new Runnable() {

        @Override
        public void run() {
        }
    }, 0, TimeUnit.MILLISECONDS, null);
}
Also used : Scheduler(io.reactivex.rxjava3.core.Scheduler) NewThreadWorker(io.reactivex.rxjava3.internal.schedulers.NewThreadWorker) Test(org.junit.Test) SuppressUndeliverable(io.reactivex.rxjava3.testsupport.SuppressUndeliverable)

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