Search in sources :

Example 21 with Worker

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

the class TrampolineSchedulerTest method trampolineWorkerHandlesConcurrentScheduling.

/**
 * This is a regression test for #1702. Concurrent work scheduling that is improperly synchronized can cause an
 * action to be added or removed onto the priority queue during a poll, which can result in NPEs during queue
 * sifting. While it is difficult to isolate the issue directly, we can easily trigger the behavior by spamming the
 * trampoline with enqueue requests from multiple threads concurrently.
 */
@Test
public void trampolineWorkerHandlesConcurrentScheduling() {
    final Worker trampolineWorker = Schedulers.trampoline().createWorker();
    final Subscriber<Object> subscriber = TestHelper.mockSubscriber();
    final TestSubscriber<Disposable> ts = new TestSubscriber<>(subscriber);
    // Spam the trampoline with actions.
    Flowable.range(0, 50).flatMap(new Function<Integer, Publisher<Disposable>>() {

        @Override
        public Publisher<Disposable> apply(Integer count) {
            return Flowable.interval(1, TimeUnit.MICROSECONDS).map(new Function<Long, Disposable>() {

                @Override
                public Disposable apply(Long ount1) {
                    return trampolineWorker.schedule(Functions.EMPTY_RUNNABLE);
                }
            }).take(100);
        }
    }).subscribeOn(Schedulers.computation()).subscribe(ts);
    ts.awaitDone(5, TimeUnit.SECONDS);
    ts.assertNoErrors();
}
Also used : Worker(io.reactivex.rxjava3.core.Scheduler.Worker) TestSubscriber(io.reactivex.rxjava3.subscribers.TestSubscriber) Test(org.junit.Test)

Example 22 with Worker

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

the class ExecutorSchedulerTest method rejectingExecutorWorker.

@Test
public void rejectingExecutorWorker() {
    ScheduledExecutorService exec = Executors.newSingleThreadScheduledExecutor();
    exec.shutdown();
    List<Throwable> errors = TestHelper.trackPluginErrors();
    try {
        Worker s = Schedulers.from(exec).createWorker();
        assertSame(EmptyDisposable.INSTANCE, s.schedule(Functions.EMPTY_RUNNABLE));
        s = Schedulers.from(exec).createWorker();
        assertSame(EmptyDisposable.INSTANCE, s.schedule(Functions.EMPTY_RUNNABLE, 10, TimeUnit.MILLISECONDS));
        s = Schedulers.from(exec).createWorker();
        assertSame(EmptyDisposable.INSTANCE, s.schedulePeriodically(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 : Worker(io.reactivex.rxjava3.core.Scheduler.Worker) Test(org.junit.Test)

Example 23 with Worker

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

Example 24 with Worker

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

the class ExecutorSchedulerFairTest method cancelledWorkerDoesntRunTasks.

@Test
public void cancelledWorkerDoesntRunTasks() {
    final AtomicInteger calls = new AtomicInteger();
    Runnable task = new Runnable() {

        @Override
        public void run() {
            calls.getAndIncrement();
        }
    };
    TestExecutor exec = new TestExecutor();
    Scheduler custom = Schedulers.from(exec, false, true);
    Worker w = custom.createWorker();
    try {
        w.schedule(task);
        w.schedule(task);
        w.schedule(task);
    } finally {
        w.dispose();
    }
    exec.executeAll();
    assertEquals(0, calls.get());
}
Also used : AtomicInteger(java.util.concurrent.atomic.AtomicInteger) Worker(io.reactivex.rxjava3.core.Scheduler.Worker) Test(org.junit.Test)

Example 25 with Worker

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

the class ExecutorSchedulerTest method cancelledWorkerDoesntRunTasks.

@Test
public void cancelledWorkerDoesntRunTasks() {
    final AtomicInteger calls = new AtomicInteger();
    Runnable task = new Runnable() {

        @Override
        public void run() {
            calls.getAndIncrement();
        }
    };
    TestExecutor exec = new TestExecutor();
    Scheduler custom = Schedulers.from(exec);
    Worker w = custom.createWorker();
    try {
        w.schedule(task);
        w.schedule(task);
        w.schedule(task);
    } finally {
        w.dispose();
    }
    exec.executeAll();
    assertEquals(0, calls.get());
}
Also used : Scheduler(io.reactivex.rxjava3.core.Scheduler) Worker(io.reactivex.rxjava3.core.Scheduler.Worker) Test(org.junit.Test)

Aggregations

Worker (io.reactivex.rxjava3.core.Scheduler.Worker)91 Test (org.junit.Test)87 Disposable (io.reactivex.rxjava3.disposables.Disposable)28 CountingRunnable (io.reactivex.rxjava3.android.testutil.CountingRunnable)22 Scheduler (io.reactivex.rxjava3.core.Scheduler)18 EmptyDisposable (io.reactivex.rxjava3.internal.disposables.EmptyDisposable)10 AtomicReference (java.util.concurrent.atomic.AtomicReference)9 Ignore (org.junit.Ignore)7 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)6 RxJavaTest (io.reactivex.rxjava3.core.RxJavaTest)4 TrampolineScheduler (io.reactivex.rxjava3.internal.schedulers.TrampolineScheduler)4 SuppressUndeliverable (io.reactivex.rxjava3.testsupport.SuppressUndeliverable)4 IoScheduler (io.reactivex.rxjava3.internal.schedulers.IoScheduler)3 WorkerCallback (io.reactivex.rxjava3.internal.schedulers.SchedulerMultiWorkerSupport.WorkerCallback)3 CountDownLatch (java.util.concurrent.CountDownLatch)3 ThrowingRunnable (org.junit.function.ThrowingRunnable)3 Message (android.os.Message)2 CompositeDisposable (io.reactivex.rxjava3.disposables.CompositeDisposable)2 TestException (io.reactivex.rxjava3.exceptions.TestException)2 NewThreadWorker (io.reactivex.rxjava3.internal.schedulers.NewThreadWorker)2