Search in sources :

Example 26 with Worker

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

the class TrampolineSchedulerTest method doWorkOnNewTrampoline.

private static Worker doWorkOnNewTrampoline(final String key, final ArrayList<String> workDone) {
    Worker worker = Schedulers.trampoline().createWorker();
    worker.schedule(new Runnable() {

        @Override
        public void run() {
            String msg = key + ".1";
            workDone.add(msg);
            System.out.println(msg);
            Worker worker3 = Schedulers.trampoline().createWorker();
            worker3.schedule(createPrintAction(key + ".B.1", workDone));
            worker3.schedule(createPrintAction(key + ".B.2", workDone));
        }
    });
    return worker;
}
Also used : Worker(io.reactivex.Scheduler.Worker)

Example 27 with Worker

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

the class TrampolineSchedulerTest method testTrampolineWorkerHandlesConcurrentScheduling.

/**
     * 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 testTrampolineWorkerHandlesConcurrentScheduling() {
    final Worker trampolineWorker = Schedulers.trampoline().createWorker();
    final Subscriber<Object> observer = TestHelper.mockSubscriber();
    final TestSubscriber<Disposable> ts = new TestSubscriber<Disposable>(observer);
    // 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.awaitTerminalEvent();
    ts.assertNoErrors();
}
Also used : CompositeDisposable(io.reactivex.disposables.CompositeDisposable) Disposable(io.reactivex.disposables.Disposable) Worker(io.reactivex.Scheduler.Worker) TestSubscriber(io.reactivex.subscribers.TestSubscriber) Publisher(org.reactivestreams.Publisher) Test(org.junit.Test)

Example 28 with Worker

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

the class ExecutorSchedulerTest method testCancelledTasksDontRun.

@Test
public void testCancelledTasksDontRun() {
    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 {
        Disposable s1 = w.schedule(task);
        Disposable s2 = w.schedule(task);
        Disposable s3 = w.schedule(task);
        s1.dispose();
        s2.dispose();
        s3.dispose();
        exec.executeAll();
        assertEquals(0, calls.get());
    } finally {
        w.dispose();
    }
}
Also used : Disposable(io.reactivex.disposables.Disposable) EmptyDisposable(io.reactivex.internal.disposables.EmptyDisposable) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) Worker(io.reactivex.Scheduler.Worker)

Example 29 with Worker

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

the class ExecutorSchedulerTest method disposeRace.

@Test
public void disposeRace() {
    ExecutorService exec = Executors.newSingleThreadExecutor();
    final Scheduler s = Schedulers.from(exec);
    try {
        for (int i = 0; i < 500; i++) {
            final Worker w = s.createWorker();
            final AtomicInteger c = new AtomicInteger(2);
            w.schedule(new Runnable() {

                @Override
                public void run() {
                    c.decrementAndGet();
                    while (c.get() != 0) {
                    }
                }
            });
            c.decrementAndGet();
            while (c.get() != 0) {
            }
            w.dispose();
        }
    } finally {
        exec.shutdownNow();
    }
}
Also used : AtomicInteger(java.util.concurrent.atomic.AtomicInteger) Worker(io.reactivex.Scheduler.Worker)

Example 30 with Worker

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

the class SchedulerLifecycleTest method tryOutSchedulers.

private void tryOutSchedulers() throws InterruptedException {
    final CountDownLatch cdl = new CountDownLatch(4);
    final Runnable countAction = new Runnable() {

        @Override
        public void run() {
            cdl.countDown();
        }
    };
    CompositeDisposable csub = new CompositeDisposable();
    try {
        Worker w1 = Schedulers.computation().createWorker();
        csub.add(w1);
        w1.schedule(countAction);
        Worker w2 = Schedulers.io().createWorker();
        csub.add(w2);
        w2.schedule(countAction);
        Worker w3 = Schedulers.newThread().createWorker();
        csub.add(w3);
        w3.schedule(countAction);
        Worker w4 = Schedulers.single().createWorker();
        csub.add(w4);
        w4.schedule(countAction);
        if (!cdl.await(3, TimeUnit.SECONDS)) {
            fail("countAction was not run by every worker");
        }
    } finally {
        csub.dispose();
    }
}
Also used : Worker(io.reactivex.Scheduler.Worker) CompositeDisposable(io.reactivex.disposables.CompositeDisposable)

Aggregations

Worker (io.reactivex.Scheduler.Worker)61 Test (org.junit.Test)40 CountingRunnable (io.reactivex.android.testutil.CountingRunnable)20 Disposable (io.reactivex.disposables.Disposable)12 AtomicReference (java.util.concurrent.atomic.AtomicReference)6 Ignore (org.junit.Ignore)6 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)5 CompositeDisposable (io.reactivex.disposables.CompositeDisposable)3 EmptyDisposable (io.reactivex.internal.disposables.EmptyDisposable)2 Scheduler (io.reactivex.Scheduler)1 ConditionalSubscriber (io.reactivex.internal.fuseable.ConditionalSubscriber)1 SubscribeOnSubscriber (io.reactivex.internal.operators.flowable.FlowableSubscribeOn.SubscribeOnSubscriber)1 SpscArrayQueue (io.reactivex.internal.queue.SpscArrayQueue)1 ComputationScheduler (io.reactivex.internal.schedulers.ComputationScheduler)1 IoScheduler (io.reactivex.internal.schedulers.IoScheduler)1 NewThreadWorker (io.reactivex.internal.schedulers.NewThreadWorker)1 ScheduledWorker (io.reactivex.internal.schedulers.SingleScheduler.ScheduledWorker)1 BooleanSubscription (io.reactivex.internal.subscriptions.BooleanSubscription)1 TestScheduler (io.reactivex.schedulers.TestScheduler)1 TestSubscriber (io.reactivex.subscribers.TestSubscriber)1