Search in sources :

Example 41 with Worker

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

the class FlowableSubscribeOnTest method deferredRequestRace.

@Test
public void deferredRequestRace() {
    for (int i = 0; i < TestHelper.RACE_DEFAULT_LOOPS; i++) {
        final TestSubscriber<Integer> ts = new TestSubscriber<>(0L);
        Worker w = Schedulers.computation().createWorker();
        final SubscribeOnSubscriber<Integer> so = new SubscribeOnSubscriber<>(ts, w, Flowable.<Integer>never(), true);
        ts.onSubscribe(so);
        final BooleanSubscription bs = new BooleanSubscription();
        try {
            Runnable r1 = new Runnable() {

                @Override
                public void run() {
                    so.onSubscribe(bs);
                }
            };
            Runnable r2 = new Runnable() {

                @Override
                public void run() {
                    so.request(1);
                }
            };
            TestHelper.race(r1, r2);
        } finally {
            w.dispose();
        }
    }
}
Also used : BooleanSubscription(io.reactivex.rxjava3.internal.subscriptions.BooleanSubscription) SubscribeOnSubscriber(io.reactivex.rxjava3.internal.operators.flowable.FlowableSubscribeOn.SubscribeOnSubscriber) Worker(io.reactivex.rxjava3.core.Scheduler.Worker) Test(org.junit.Test)

Example 42 with Worker

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

the class AbstractSchedulerConcurrencyTests method recursionFromOuterActionAndUnsubscribeInside.

@Test
public void recursionFromOuterActionAndUnsubscribeInside() throws InterruptedException {
    final CountDownLatch latch = new CountDownLatch(1);
    final Worker inner = getScheduler().createWorker();
    try {
        inner.schedule(new Runnable() {

            int i;

            @Override
            public void run() {
                i++;
                if (i % 100000 == 0) {
                    System.out.println(i + "  Total Memory: " + Runtime.getRuntime().totalMemory() + "  Free: " + Runtime.getRuntime().freeMemory());
                }
                if (i < 1000000L) {
                    inner.schedule(this);
                } else {
                    latch.countDown();
                }
            }
        });
        latch.await();
    } finally {
        inner.dispose();
    }
}
Also used : Worker(io.reactivex.rxjava3.core.Scheduler.Worker) Test(org.junit.Test)

Example 43 with Worker

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

the class CachedThreadSchedulerTest method shutdownRejects.

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

        @Override
        public void run() {
            calls[0]++;
        }
    };
    IoScheduler s = new IoScheduler();
    s.shutdown();
    s.shutdown();
    s.scheduleDirect(r);
    s.scheduleDirect(r, 1, TimeUnit.SECONDS);
    s.schedulePeriodicallyDirect(r, 1, 1, TimeUnit.SECONDS);
    Worker w = s.createWorker();
    w.dispose();
    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 : IoScheduler(io.reactivex.rxjava3.internal.schedulers.IoScheduler) Worker(io.reactivex.rxjava3.core.Scheduler.Worker) Test(org.junit.Test) SuppressUndeliverable(io.reactivex.rxjava3.testsupport.SuppressUndeliverable)

Example 44 with Worker

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

the class AbstractSchedulerConcurrencyTests method unsubscribeRecursiveScheduleFromOutside.

@Test
public void unsubscribeRecursiveScheduleFromOutside() throws InterruptedException {
    final CountDownLatch latch = new CountDownLatch(1);
    final CountDownLatch unsubscribeLatch = new CountDownLatch(1);
    final AtomicInteger counter = new AtomicInteger();
    final Worker inner = getScheduler().createWorker();
    try {
        inner.schedule(new Runnable() {

            @Override
            public void run() {
                inner.schedule(new Runnable() {

                    int i;

                    @Override
                    public void run() {
                        System.out.println("Run: " + i++);
                        if (i == 10) {
                            latch.countDown();
                            try {
                                // wait for unsubscribe to finish so we are not racing it
                                unsubscribeLatch.await();
                            } catch (InterruptedException e) {
                            // we expect the countDown if unsubscribe is not working
                            // or to be interrupted if unsubscribe is successful since
                            // the unsubscribe will interrupt it as it is calling Future.cancel(true)
                            // so we will ignore the stacktrace
                            }
                        }
                        counter.incrementAndGet();
                        inner.schedule(this);
                    }
                });
            }
        });
        latch.await();
        inner.dispose();
        unsubscribeLatch.countDown();
        // let time pass to see if the scheduler is still doing work
        Thread.sleep(200);
        assertEquals(10, counter.get());
    } finally {
        inner.dispose();
    }
}
Also used : Worker(io.reactivex.rxjava3.core.Scheduler.Worker) Test(org.junit.Test)

Example 45 with Worker

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

the class Scheduler method schedulePeriodicallyDirect.

/**
 * Schedules a periodic execution of the given task with the given initial time delay and repeat period.
 *
 * <p>
 * This method is safe to be called from multiple threads but there are no
 * ordering guarantees between tasks.
 *
 * <p>
 * The periodic execution is at a fixed rate, that is, the first execution will be after the
 * {@code initialDelay}, the second after {@code initialDelay + period}, the third after
 * {@code initialDelay + 2 * period}, and so on.
 *
 * @param run the task to schedule
 * @param initialDelay the initial delay amount, non-positive values indicate non-delayed scheduling
 * @param period the period at which the task should be re-executed
 * @param unit the unit of measure of the delay amount
 * @return the Disposable that let's one cancel this particular delayed task.
 * @throws NullPointerException if {@code run} or {@code unit} is {@code null}
 * @since 2.0
 */
@NonNull
public Disposable schedulePeriodicallyDirect(@NonNull Runnable run, long initialDelay, long period, @NonNull TimeUnit unit) {
    final Worker w = createWorker();
    final Runnable decoratedRun = RxJavaPlugins.onSchedule(run);
    PeriodicDirectTask periodicTask = new PeriodicDirectTask(decoratedRun, w);
    Disposable d = w.schedulePeriodically(periodicTask, initialDelay, period, unit);
    if (d == EmptyDisposable.INSTANCE) {
        return d;
    }
    return periodicTask;
}
Also used : Disposable(io.reactivex.rxjava3.disposables.Disposable)

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