Search in sources :

Example 81 with Scheduler

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

the class FlowableTakeTimedTest method takeTimed.

@Test
public void takeTimed() {
    TestScheduler scheduler = new TestScheduler();
    PublishProcessor<Integer> source = PublishProcessor.create();
    Flowable<Integer> result = source.take(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);
    InOrder inOrder = inOrder(subscriber);
    inOrder.verify(subscriber).onNext(1);
    inOrder.verify(subscriber).onNext(2);
    inOrder.verify(subscriber).onNext(3);
    inOrder.verify(subscriber).onComplete();
    inOrder.verifyNoMoreInteractions();
    verify(subscriber, never()).onNext(4);
    verify(subscriber, never()).onError(any(Throwable.class));
}
Also used : InOrder(org.mockito.InOrder) TestScheduler(io.reactivex.rxjava3.schedulers.TestScheduler) Test(org.junit.Test)

Example 82 with Scheduler

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

the class FlowableThrottleFirstTest method throttlingWithError.

@Test
public void throttlingWithError() {
    Flowable<String> source = Flowable.unsafeCreate(new Publisher<String>() {

        @Override
        public void subscribe(Subscriber<? super String> subscriber) {
            subscriber.onSubscribe(new BooleanSubscription());
            Exception error = new TestException();
            // Should be published since it is first
            publishNext(subscriber, 100, "one");
            // Should be skipped since onError will arrive before the timeout expires
            publishNext(subscriber, 200, "two");
            // Should be published as soon as the timeout expires.
            publishError(subscriber, 300, error);
        }
    });
    Flowable<String> sampled = source.throttleFirst(400, TimeUnit.MILLISECONDS, scheduler);
    sampled.subscribe(subscriber);
    InOrder inOrder = inOrder(subscriber);
    scheduler.advanceTimeTo(400, TimeUnit.MILLISECONDS);
    inOrder.verify(subscriber).onNext("one");
    inOrder.verify(subscriber).onError(any(TestException.class));
    inOrder.verifyNoMoreInteractions();
}
Also used : BooleanSubscription(io.reactivex.rxjava3.internal.subscriptions.BooleanSubscription) InOrder(org.mockito.InOrder)

Example 83 with Scheduler

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

the class FlowableThrottleFirstTest method before.

@Before
public void before() {
    scheduler = new TestScheduler();
    innerScheduler = scheduler.createWorker();
    subscriber = TestHelper.mockSubscriber();
}
Also used : TestScheduler(io.reactivex.rxjava3.schedulers.TestScheduler)

Example 84 with Scheduler

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

the class FlowableThrottleFirstTest method throttlingWithCompleted.

@Test
public void throttlingWithCompleted() {
    Flowable<String> source = Flowable.unsafeCreate(new Publisher<String>() {

        @Override
        public void subscribe(Subscriber<? super String> subscriber) {
            subscriber.onSubscribe(new BooleanSubscription());
            // publish as it's first
            publishNext(subscriber, 100, "one");
            // skip as it's last within the first 400
            publishNext(subscriber, 300, "two");
            // publish
            publishNext(subscriber, 900, "three");
            // skip
            publishNext(subscriber, 905, "four");
            // Should be published as soon as the timeout expires.
            publishCompleted(subscriber, 1000);
        }
    });
    Flowable<String> sampled = source.throttleFirst(400, TimeUnit.MILLISECONDS, scheduler);
    sampled.subscribe(subscriber);
    InOrder inOrder = inOrder(subscriber);
    scheduler.advanceTimeTo(1000, TimeUnit.MILLISECONDS);
    inOrder.verify(subscriber, times(1)).onNext("one");
    inOrder.verify(subscriber, times(0)).onNext("two");
    inOrder.verify(subscriber, times(1)).onNext("three");
    inOrder.verify(subscriber, times(0)).onNext("four");
    inOrder.verify(subscriber, times(1)).onComplete();
    inOrder.verifyNoMoreInteractions();
}
Also used : BooleanSubscription(io.reactivex.rxjava3.internal.subscriptions.BooleanSubscription) InOrder(org.mockito.InOrder)

Example 85 with Scheduler

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

the class FlowableSkipLastTimedTest method skipLastTimedErrorBeforeTime.

@Test
public void skipLastTimedErrorBeforeTime() {
    TestScheduler scheduler = new TestScheduler();
    PublishProcessor<Integer> source = PublishProcessor.create();
    Flowable<Integer> result = source.skipLast(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(1050, TimeUnit.MILLISECONDS);
    verify(subscriber).onError(any(TestException.class));
    verify(subscriber, never()).onComplete();
    verify(subscriber, never()).onNext(any());
}
Also used : TestException(io.reactivex.rxjava3.exceptions.TestException) 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