Search in sources :

Example 16 with Scheduler

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

the class SingleTimeoutTest method shouldUnsubscribeFromUnderlyingSubscriptionOnDispose.

@Test
public void shouldUnsubscribeFromUnderlyingSubscriptionOnDispose() {
    final PublishSubject<String> subject = PublishSubject.create();
    final TestScheduler scheduler = new TestScheduler();
    final TestObserver<String> observer = subject.single("").timeout(100, TimeUnit.MILLISECONDS, scheduler).test();
    assertTrue(subject.hasObservers());
    observer.dispose();
    assertFalse(subject.hasObservers());
}
Also used : TestScheduler(io.reactivex.rxjava3.schedulers.TestScheduler) Test(org.junit.Test)

Example 17 with Scheduler

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

the class AbstractSchedulerTests method schedulePeriodicallyDirectDecoratesRunnable.

@Test
public void schedulePeriodicallyDirectDecoratesRunnable() throws InterruptedException {
    final Scheduler scheduler = getScheduler();
    if (scheduler instanceof TrampolineScheduler) {
        // Can't properly stop a trampolined periodic task.
        return;
    }
    final AtomicReference<Disposable> disposable = new AtomicReference<>();
    try {
        assertRunnableDecorated(new Runnable() {

            @Override
            public void run() {
                disposable.set(scheduler.schedulePeriodicallyDirect(Functions.EMPTY_RUNNABLE, 1, 10000, TimeUnit.MILLISECONDS));
            }
        });
    } finally {
        disposable.get().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 18 with Scheduler

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

the class AbstractSchedulerTests method scheduleDirectPeriodic.

@Test
public void scheduleDirectPeriodic() throws Exception {
    Scheduler s = getScheduler();
    if (s instanceof TrampolineScheduler) {
        // can't properly stop a trampolined periodic task
        return;
    }
    final CountDownLatch cdl = new CountDownLatch(5);
    Disposable d = s.schedulePeriodicallyDirect(new Runnable() {

        @Override
        public void run() {
            cdl.countDown();
        }
    }, 10, 10, TimeUnit.MILLISECONDS);
    try {
        assertTrue(cdl.await(5, TimeUnit.SECONDS));
    } finally {
        d.dispose();
    }
    assertTrue(d.isDisposed());
}
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 19 with Scheduler

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

the class AbstractSchedulerTests method sequenceOfDelayedActions.

@Test
public void sequenceOfDelayedActions() throws InterruptedException {
    Scheduler scheduler = getScheduler();
    final Scheduler.Worker inner = scheduler.createWorker();
    try {
        final CountDownLatch latch = new CountDownLatch(1);
        final Runnable first = mock(Runnable.class);
        final Runnable second = mock(Runnable.class);
        inner.schedule(new Runnable() {

            @Override
            public void run() {
                inner.schedule(first, 30, TimeUnit.MILLISECONDS);
                inner.schedule(second, 10, TimeUnit.MILLISECONDS);
                inner.schedule(new Runnable() {

                    @Override
                    public void run() {
                        latch.countDown();
                    }
                }, 40, TimeUnit.MILLISECONDS);
            }
        });
        latch.await();
        InOrder inOrder = inOrder(first, second);
        inOrder.verify(second, times(1)).run();
        inOrder.verify(first, 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 20 with Scheduler

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

the class AbstractSchedulerTests method recursiveExecution.

@Test
public final void recursiveExecution() throws InterruptedException {
    final 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() {

            @Override
            public void run() {
                if (i.incrementAndGet() < 100) {
                    inner.schedule(this);
                } 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)

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