Search in sources :

Example 86 with Scheduler

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

the class FlowableSkipTimedTest method skipTimedErrorBeforeTime.

@Test
public void skipTimedErrorBeforeTime() {
    TestScheduler scheduler = new TestScheduler();
    PublishProcessor<Integer> source = PublishProcessor.create();
    Flowable<Integer> result = source.skip(1, TimeUnit.SECONDS, scheduler);
    Subscriber<Object> subscriber = TestHelper.mockSubscriber();
    result.subscribe(subscriber);
    source.onNext(1);
    source.onNext(2);
    source.onNext(3);
    source.onError(new TestException());
    scheduler.advanceTimeBy(1, TimeUnit.SECONDS);
    InOrder inOrder = inOrder(subscriber);
    inOrder.verify(subscriber).onError(any(TestException.class));
    inOrder.verifyNoMoreInteractions();
    verify(subscriber, never()).onNext(any());
    verify(subscriber, never()).onComplete();
}
Also used : InOrder(org.mockito.InOrder) TestException(io.reactivex.rxjava3.exceptions.TestException) TestScheduler(io.reactivex.rxjava3.schedulers.TestScheduler) Test(org.junit.Test)

Example 87 with Scheduler

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

the class FlowableSkipTimedTest method skipTimedFinishBeforeTime.

@Test
public void skipTimedFinishBeforeTime() {
    TestScheduler scheduler = new TestScheduler();
    PublishProcessor<Integer> source = PublishProcessor.create();
    Flowable<Integer> result = source.skip(1, TimeUnit.SECONDS, scheduler);
    Subscriber<Object> subscriber = TestHelper.mockSubscriber();
    result.subscribe(subscriber);
    source.onNext(1);
    source.onNext(2);
    source.onNext(3);
    source.onComplete();
    scheduler.advanceTimeBy(1, TimeUnit.SECONDS);
    InOrder inOrder = inOrder(subscriber);
    inOrder.verify(subscriber).onComplete();
    inOrder.verifyNoMoreInteractions();
    verify(subscriber, never()).onNext(any());
    verify(subscriber, never()).onError(any(Throwable.class));
}
Also used : InOrder(org.mockito.InOrder) TestScheduler(io.reactivex.rxjava3.schedulers.TestScheduler) Test(org.junit.Test)

Example 88 with Scheduler

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

the class FlowableSkipTimedTest method skipTimed.

@Test
public void skipTimed() {
    TestScheduler scheduler = new TestScheduler();
    PublishProcessor<Integer> source = PublishProcessor.create();
    Flowable<Integer> result = source.skip(1, TimeUnit.SECONDS, scheduler);
    Subscriber<Object> subscriber = TestHelper.mockSubscriber();
    result.subscribe(subscriber);
    source.onNext(1);
    source.onNext(2);
    source.onNext(3);
    scheduler.advanceTimeBy(1, TimeUnit.SECONDS);
    source.onNext(4);
    source.onNext(5);
    source.onNext(6);
    source.onComplete();
    InOrder inOrder = inOrder(subscriber);
    inOrder.verify(subscriber, never()).onNext(1);
    inOrder.verify(subscriber, never()).onNext(2);
    inOrder.verify(subscriber, never()).onNext(3);
    inOrder.verify(subscriber).onNext(4);
    inOrder.verify(subscriber).onNext(5);
    inOrder.verify(subscriber).onNext(6);
    inOrder.verify(subscriber).onComplete();
    inOrder.verifyNoMoreInteractions();
    verify(subscriber, never()).onError(any(Throwable.class));
}
Also used : InOrder(org.mockito.InOrder) TestScheduler(io.reactivex.rxjava3.schedulers.TestScheduler) Test(org.junit.Test)

Example 89 with Scheduler

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

the class FlowableTimerTest method timerInterruptible.

@Test
public void timerInterruptible() throws Exception {
    ScheduledExecutorService exec = Executors.newSingleThreadScheduledExecutor();
    try {
        for (Scheduler s : new Scheduler[] { Schedulers.single(), Schedulers.computation(), Schedulers.newThread(), Schedulers.io(), Schedulers.from(exec, true) }) {
            final AtomicBoolean interrupted = new AtomicBoolean();
            TestSubscriber<Long> ts = Flowable.timer(1, TimeUnit.MILLISECONDS, s).map(new Function<Long, Long>() {

                @Override
                public Long apply(Long v) throws Exception {
                    try {
                        Thread.sleep(3000);
                    } catch (InterruptedException ex) {
                        interrupted.set(true);
                    }
                    return v;
                }
            }).test();
            Thread.sleep(500);
            ts.cancel();
            Thread.sleep(500);
            assertTrue(s.getClass().getSimpleName(), interrupted.get());
        }
    } finally {
        exec.shutdown();
    }
}
Also used : AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) Function(io.reactivex.rxjava3.functions.Function)

Example 90 with Scheduler

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

the class RxJavaPluginsTest method createNewThreadScheduler.

@Test
public void createNewThreadScheduler() {
    final String name = "NewThreadSchedulerTest";
    ThreadFactory factory = new ThreadFactory() {

        @Override
        public Thread newThread(Runnable r) {
            return new Thread(r, name);
        }
    };
    final Scheduler customScheduler = RxJavaPlugins.createNewThreadScheduler(factory);
    RxJavaPlugins.setNewThreadSchedulerHandler(new Function<Scheduler, Scheduler>() {

        @Override
        public Scheduler apply(Scheduler scheduler) throws Exception {
            return customScheduler;
        }
    });
    try {
        verifyThread(Schedulers.newThread(), name);
    } finally {
        customScheduler.shutdown();
        RxJavaPlugins.reset();
    }
}
Also used : ImmediateThinScheduler(io.reactivex.rxjava3.internal.schedulers.ImmediateThinScheduler) 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