Search in sources :

Example 6 with TrampolineScheduler

use of io.reactivex.rxjava3.internal.schedulers.TrampolineScheduler 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 7 with TrampolineScheduler

use of io.reactivex.rxjava3.internal.schedulers.TrampolineScheduler in project RxJava by ReactiveX.

the class AbstractSchedulerTests method schedulePeriodicallyDirectZeroPeriod.

@Test
public void schedulePeriodicallyDirectZeroPeriod() throws Exception {
    Scheduler s = getScheduler();
    if (s instanceof TrampolineScheduler) {
        // can't properly stop a trampolined periodic task
        return;
    }
    for (int initial = 0; initial < 2; initial++) {
        final CountDownLatch cdl = new CountDownLatch(1);
        final SequentialDisposable sd = new SequentialDisposable();
        try {
            sd.replace(s.schedulePeriodicallyDirect(new Runnable() {

                int count;

                @Override
                public void run() {
                    if (++count == 10) {
                        sd.dispose();
                        cdl.countDown();
                    }
                }
            }, initial, 0, TimeUnit.MILLISECONDS));
            assertTrue("" + initial, cdl.await(5, TimeUnit.SECONDS));
        } finally {
            sd.dispose();
        }
    }
}
Also used : TrampolineScheduler(io.reactivex.rxjava3.internal.schedulers.TrampolineScheduler) SequentialDisposable(io.reactivex.rxjava3.internal.disposables.SequentialDisposable) TrampolineScheduler(io.reactivex.rxjava3.internal.schedulers.TrampolineScheduler) Test(org.junit.Test)

Example 8 with TrampolineScheduler

use of io.reactivex.rxjava3.internal.schedulers.TrampolineScheduler in project RxJava by ReactiveX.

the class ObservableInterval method subscribeActual.

@Override
public void subscribeActual(Observer<? super Long> observer) {
    IntervalObserver is = new IntervalObserver(observer);
    observer.onSubscribe(is);
    Scheduler sch = scheduler;
    if (sch instanceof TrampolineScheduler) {
        Worker worker = sch.createWorker();
        is.setResource(worker);
        worker.schedulePeriodically(is, initialDelay, period, unit);
    } else {
        Disposable d = sch.schedulePeriodicallyDirect(is, initialDelay, period, unit);
        is.setResource(d);
    }
}
Also used : Disposable(io.reactivex.rxjava3.disposables.Disposable) TrampolineScheduler(io.reactivex.rxjava3.internal.schedulers.TrampolineScheduler) Worker(io.reactivex.rxjava3.core.Scheduler.Worker) TrampolineScheduler(io.reactivex.rxjava3.internal.schedulers.TrampolineScheduler)

Example 9 with TrampolineScheduler

use of io.reactivex.rxjava3.internal.schedulers.TrampolineScheduler in project RxJava by ReactiveX.

the class AbstractSchedulerTests method unwrapDefaultPeriodicTask.

@Test
public void unwrapDefaultPeriodicTask() throws InterruptedException {
    Scheduler s = getScheduler();
    if (s 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 = s.schedulePeriodicallyDirect(countDownRunnable, 100, 100, TimeUnit.MILLISECONDS);
    SchedulerRunnableIntrospection wrapper = (SchedulerRunnableIntrospection) disposable;
    assertSame(countDownRunnable, wrapper.getWrappedRunnable());
    assertTrue(cdl.await(5, TimeUnit.SECONDS));
    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 10 with TrampolineScheduler

use of io.reactivex.rxjava3.internal.schedulers.TrampolineScheduler in project RxJava by ReactiveX.

the class AbstractSchedulerTests method schedulePeriodicallyZeroPeriod.

@Test
public void schedulePeriodicallyZeroPeriod() throws Exception {
    Scheduler s = getScheduler();
    if (s instanceof TrampolineScheduler) {
        // can't properly stop a trampolined periodic task
        return;
    }
    for (int initial = 0; initial < 2; initial++) {
        final CountDownLatch cdl = new CountDownLatch(1);
        final SequentialDisposable sd = new SequentialDisposable();
        Scheduler.Worker w = s.createWorker();
        try {
            sd.replace(w.schedulePeriodically(new Runnable() {

                int count;

                @Override
                public void run() {
                    if (++count == 10) {
                        sd.dispose();
                        cdl.countDown();
                    }
                }
            }, initial, 0, TimeUnit.MILLISECONDS));
            assertTrue("" + initial, cdl.await(5, TimeUnit.SECONDS));
        } finally {
            sd.dispose();
            w.dispose();
        }
    }
}
Also used : Worker(io.reactivex.rxjava3.core.Scheduler.Worker) TrampolineScheduler(io.reactivex.rxjava3.internal.schedulers.TrampolineScheduler) SequentialDisposable(io.reactivex.rxjava3.internal.disposables.SequentialDisposable) TrampolineScheduler(io.reactivex.rxjava3.internal.schedulers.TrampolineScheduler) Test(org.junit.Test)

Aggregations

TrampolineScheduler (io.reactivex.rxjava3.internal.schedulers.TrampolineScheduler)10 Disposable (io.reactivex.rxjava3.disposables.Disposable)8 SequentialDisposable (io.reactivex.rxjava3.internal.disposables.SequentialDisposable)6 Test (org.junit.Test)6 Worker (io.reactivex.rxjava3.core.Scheduler.Worker)5