Search in sources :

Example 31 with Scheduler

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

the class ExecutorSchedulerInterruptibleTest 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 : EmptyDisposable(io.reactivex.rxjava3.internal.disposables.EmptyDisposable) Disposable(io.reactivex.rxjava3.disposables.Disposable) Scheduler(io.reactivex.rxjava3.core.Scheduler) Test(org.junit.Test)

Example 32 with Scheduler

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

the class AbstractSchedulerTests method recursiveExecutionWithDelayTime.

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

            int state;

            @Override
            public void run() {
                i.set(state);
                if (state++ < 100) {
                    inner.schedule(this, 1, TimeUnit.MILLISECONDS);
                } else {
                    latch.countDown();
                }
            }
        });
        latch.await();
        assertEquals(100, i.get());
    } finally {
        inner.dispose();
    }
}
Also used : Worker(io.reactivex.rxjava3.core.Scheduler.Worker) TrampolineScheduler(io.reactivex.rxjava3.internal.schedulers.TrampolineScheduler) Test(org.junit.Test)

Example 33 with Scheduler

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

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

the class AbstractSchedulerTests method nestedActions.

@Test
public void nestedActions() throws InterruptedException {
    Scheduler scheduler = getScheduler();
    final Scheduler.Worker inner = scheduler.createWorker();
    try {
        final CountDownLatch latch = new CountDownLatch(1);
        final Runnable firstStepStart = mock(Runnable.class);
        final Runnable firstStepEnd = mock(Runnable.class);
        final Runnable secondStepStart = mock(Runnable.class);
        final Runnable secondStepEnd = mock(Runnable.class);
        final Runnable thirdStepStart = mock(Runnable.class);
        final Runnable thirdStepEnd = mock(Runnable.class);
        final Runnable firstAction = new Runnable() {

            @Override
            public void run() {
                firstStepStart.run();
                firstStepEnd.run();
                latch.countDown();
            }
        };
        final Runnable secondAction = new Runnable() {

            @Override
            public void run() {
                secondStepStart.run();
                inner.schedule(firstAction);
                secondStepEnd.run();
            }
        };
        final Runnable thirdAction = new Runnable() {

            @Override
            public void run() {
                thirdStepStart.run();
                inner.schedule(secondAction);
                thirdStepEnd.run();
            }
        };
        InOrder inOrder = inOrder(firstStepStart, firstStepEnd, secondStepStart, secondStepEnd, thirdStepStart, thirdStepEnd);
        inner.schedule(thirdAction);
        latch.await();
        inOrder.verify(thirdStepStart, times(1)).run();
        inOrder.verify(thirdStepEnd, times(1)).run();
        inOrder.verify(secondStepStart, times(1)).run();
        inOrder.verify(secondStepEnd, times(1)).run();
        inOrder.verify(firstStepStart, times(1)).run();
        inOrder.verify(firstStepEnd, times(1)).run();
    } finally {
        inner.dispose();
    }
}
Also used : Worker(io.reactivex.rxjava3.core.Scheduler.Worker) InOrder(org.mockito.InOrder) TrampolineScheduler(io.reactivex.rxjava3.internal.schedulers.TrampolineScheduler) Test(org.junit.Test)

Example 35 with Scheduler

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

the class AbstractSchedulerTests method subscribeOnNestedConcurrency.

@Test
public final void subscribeOnNestedConcurrency() throws InterruptedException {
    final Scheduler scheduler = getScheduler();
    Flowable<String> f = Flowable.fromArray("one", "two", "three", "four", "five", "six", "seven", "eight", "nine", "ten").flatMap(new Function<String, Flowable<String>>() {

        @Override
        public Flowable<String> apply(final String v) {
            return Flowable.unsafeCreate(new Publisher<String>() {

                @Override
                public void subscribe(Subscriber<? super String> subscriber) {
                    subscriber.onSubscribe(new BooleanSubscription());
                    subscriber.onNext("value_after_map-" + v);
                    subscriber.onComplete();
                }
            }).subscribeOn(scheduler);
        }
    });
    ConcurrentObserverValidator<String> observer = new ConcurrentObserverValidator<>();
    f.subscribe(observer);
    if (!observer.completed.await(3000, TimeUnit.MILLISECONDS)) {
        fail("timed out");
    }
    if (observer.error.get() != null) {
        observer.error.get().printStackTrace();
        fail("Error: " + observer.error.get().getMessage());
    }
}
Also used : TrampolineScheduler(io.reactivex.rxjava3.internal.schedulers.TrampolineScheduler) Test(org.junit.Test)

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