Search in sources :

Example 61 with Flowable

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

the class FlowableTimeoutTests method shouldSwitchToOtherIfOnCompletedNotWithinTimeout.

@Test
public void shouldSwitchToOtherIfOnCompletedNotWithinTimeout() {
    Flowable<String> other = Flowable.just("a", "b", "c");
    Flowable<String> source = underlyingSubject.timeout(TIMEOUT, TIME_UNIT, testScheduler, other);
    Subscriber<String> subscriber = TestHelper.mockSubscriber();
    TestSubscriber<String> ts = new TestSubscriber<>(subscriber);
    source.subscribe(ts);
    testScheduler.advanceTimeBy(2, TimeUnit.SECONDS);
    underlyingSubject.onNext("One");
    testScheduler.advanceTimeBy(4, TimeUnit.SECONDS);
    underlyingSubject.onComplete();
    InOrder inOrder = inOrder(subscriber);
    inOrder.verify(subscriber, times(1)).onNext("One");
    inOrder.verify(subscriber, times(1)).onNext("a");
    inOrder.verify(subscriber, times(1)).onNext("b");
    inOrder.verify(subscriber, times(1)).onNext("c");
    inOrder.verify(subscriber, times(1)).onComplete();
    inOrder.verifyNoMoreInteractions();
    ts.cancel();
}
Also used : InOrder(org.mockito.InOrder) TestSubscriber(io.reactivex.rxjava3.subscribers.TestSubscriber)

Example 62 with Flowable

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

the class FlowableTimeoutTests method shouldSwitchToOtherIfOnNextNotWithinTimeout.

@Test
public void shouldSwitchToOtherIfOnNextNotWithinTimeout() {
    Flowable<String> other = Flowable.just("a", "b", "c");
    Flowable<String> source = underlyingSubject.timeout(TIMEOUT, TIME_UNIT, testScheduler, other);
    Subscriber<String> subscriber = TestHelper.mockSubscriber();
    TestSubscriber<String> ts = new TestSubscriber<>(subscriber);
    source.subscribe(ts);
    testScheduler.advanceTimeBy(2, TimeUnit.SECONDS);
    underlyingSubject.onNext("One");
    testScheduler.advanceTimeBy(4, TimeUnit.SECONDS);
    underlyingSubject.onNext("Two");
    InOrder inOrder = inOrder(subscriber);
    inOrder.verify(subscriber, times(1)).onNext("One");
    inOrder.verify(subscriber, times(1)).onNext("a");
    inOrder.verify(subscriber, times(1)).onNext("b");
    inOrder.verify(subscriber, times(1)).onNext("c");
    inOrder.verify(subscriber, times(1)).onComplete();
    inOrder.verifyNoMoreInteractions();
    ts.cancel();
}
Also used : InOrder(org.mockito.InOrder) TestSubscriber(io.reactivex.rxjava3.subscribers.TestSubscriber)

Example 63 with Flowable

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

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

use of io.reactivex.rxjava3.core.Flowable 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)

Aggregations

Test (org.junit.Test)159 BooleanSubscription (io.reactivex.rxjava3.internal.subscriptions.BooleanSubscription)121 TestException (io.reactivex.rxjava3.exceptions.TestException)95 TestSubscriber (io.reactivex.rxjava3.subscribers.TestSubscriber)49 InOrder (org.mockito.InOrder)41 IOException (java.io.IOException)27 TestScheduler (io.reactivex.rxjava3.schedulers.TestScheduler)22 Disposable (io.reactivex.rxjava3.disposables.Disposable)21 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)12 io.reactivex.rxjava3.core (io.reactivex.rxjava3.core)11 io.reactivex.rxjava3.testsupport (io.reactivex.rxjava3.testsupport)9 java.util (java.util)9 Assert (org.junit.Assert)9 Subscriber (org.reactivestreams.Subscriber)9 GroupedFlowable (io.reactivex.rxjava3.flowables.GroupedFlowable)8 java.util.concurrent.atomic (java.util.concurrent.atomic)8 Worker (io.reactivex.rxjava3.core.Scheduler.Worker)7 io.reactivex.rxjava3.functions (io.reactivex.rxjava3.functions)7 Functions (io.reactivex.rxjava3.internal.functions.Functions)7 RxJavaPlugins (io.reactivex.rxjava3.plugins.RxJavaPlugins)7