Search in sources :

Example 11 with TestSubscriber

use of io.reactivex.subscribers.TestSubscriber 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> observer = TestHelper.mockSubscriber();
    TestSubscriber<String> ts = new TestSubscriber<String>(observer);
    source.subscribe(ts);
    testScheduler.advanceTimeBy(2, TimeUnit.SECONDS);
    underlyingSubject.onNext("One");
    testScheduler.advanceTimeBy(4, TimeUnit.SECONDS);
    underlyingSubject.onComplete();
    InOrder inOrder = inOrder(observer);
    inOrder.verify(observer, times(1)).onNext("One");
    inOrder.verify(observer, times(1)).onNext("a");
    inOrder.verify(observer, times(1)).onNext("b");
    inOrder.verify(observer, times(1)).onNext("c");
    inOrder.verify(observer, times(1)).onComplete();
    inOrder.verifyNoMoreInteractions();
    ts.dispose();
}
Also used : InOrder(org.mockito.InOrder) TestSubscriber(io.reactivex.subscribers.TestSubscriber)

Example 12 with TestSubscriber

use of io.reactivex.subscribers.TestSubscriber in project RxJava by ReactiveX.

the class FlowableTimeoutTests method shouldUnsubscribeFromUnderlyingSubscriptionOnImmediatelyComplete.

@Test
@Ignore("s should be considered cancelled upon executing onComplete and not expect downstream to call cancel")
public void shouldUnsubscribeFromUnderlyingSubscriptionOnImmediatelyComplete() {
    // From https://github.com/ReactiveX/RxJava/pull/951
    final Subscription s = mock(Subscription.class);
    Flowable<String> immediatelyComplete = Flowable.unsafeCreate(new Publisher<String>() {

        @Override
        public void subscribe(Subscriber<? super String> subscriber) {
            subscriber.onSubscribe(s);
            subscriber.onComplete();
        }
    });
    TestScheduler testScheduler = new TestScheduler();
    Flowable<String> observableWithTimeout = immediatelyComplete.timeout(1000, TimeUnit.MILLISECONDS, testScheduler);
    Subscriber<String> observer = TestHelper.mockSubscriber();
    TestSubscriber<String> ts = new TestSubscriber<String>(observer);
    observableWithTimeout.subscribe(ts);
    testScheduler.advanceTimeBy(2000, TimeUnit.MILLISECONDS);
    InOrder inOrder = inOrder(observer);
    inOrder.verify(observer).onComplete();
    inOrder.verifyNoMoreInteractions();
    verify(s, times(1)).cancel();
}
Also used : InOrder(org.mockito.InOrder) TestSubscriber(io.reactivex.subscribers.TestSubscriber) BooleanSubscription(io.reactivex.internal.subscriptions.BooleanSubscription) TestScheduler(io.reactivex.schedulers.TestScheduler)

Example 13 with TestSubscriber

use of io.reactivex.subscribers.TestSubscriber in project RxJava by ReactiveX.

the class FlowableTimeoutTests method shouldSwitchToOtherIfOnErrorNotWithinTimeout.

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

Example 14 with TestSubscriber

use of io.reactivex.subscribers.TestSubscriber in project RxJava by ReactiveX.

the class FlowableTimeoutTests method shouldTimeoutIfSynchronizedFlowableEmitFirstOnNextNotWithinTimeout.

@Test
public void shouldTimeoutIfSynchronizedFlowableEmitFirstOnNextNotWithinTimeout() throws InterruptedException {
    final CountDownLatch exit = new CountDownLatch(1);
    final CountDownLatch timeoutSetuped = new CountDownLatch(1);
    final Subscriber<String> observer = TestHelper.mockSubscriber();
    final TestSubscriber<String> ts = new TestSubscriber<String>(observer);
    new Thread(new Runnable() {

        @Override
        public void run() {
            Flowable.unsafeCreate(new Publisher<String>() {

                @Override
                public void subscribe(Subscriber<? super String> subscriber) {
                    subscriber.onSubscribe(new BooleanSubscription());
                    try {
                        timeoutSetuped.countDown();
                        exit.await();
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                    subscriber.onNext("a");
                    subscriber.onComplete();
                }
            }).timeout(1, TimeUnit.SECONDS, testScheduler).subscribe(ts);
        }
    }).start();
    timeoutSetuped.await();
    testScheduler.advanceTimeBy(2, TimeUnit.SECONDS);
    InOrder inOrder = inOrder(observer);
    inOrder.verify(observer, times(1)).onError(isA(TimeoutException.class));
    inOrder.verifyNoMoreInteractions();
    // exit the thread
    exit.countDown();
}
Also used : BooleanSubscription(io.reactivex.internal.subscriptions.BooleanSubscription) InOrder(org.mockito.InOrder) TestSubscriber(io.reactivex.subscribers.TestSubscriber)

Example 15 with TestSubscriber

use of io.reactivex.subscribers.TestSubscriber in project RxJava by ReactiveX.

the class FlowableTimeoutTests method shouldSwitchToOtherAndCanBeUnsubscribedIfOnNextNotWithinTimeout.

@Test
public void shouldSwitchToOtherAndCanBeUnsubscribedIfOnNextNotWithinTimeout() {
    PublishProcessor<String> other = PublishProcessor.create();
    Flowable<String> source = underlyingSubject.timeout(TIMEOUT, TIME_UNIT, testScheduler, other);
    Subscriber<String> observer = TestHelper.mockSubscriber();
    TestSubscriber<String> ts = new TestSubscriber<String>(observer);
    source.subscribe(ts);
    testScheduler.advanceTimeBy(2, TimeUnit.SECONDS);
    underlyingSubject.onNext("One");
    testScheduler.advanceTimeBy(4, TimeUnit.SECONDS);
    underlyingSubject.onNext("Two");
    other.onNext("a");
    other.onNext("b");
    ts.dispose();
    // The following messages should not be delivered.
    other.onNext("c");
    other.onNext("d");
    other.onComplete();
    InOrder inOrder = inOrder(observer);
    inOrder.verify(observer, times(1)).onNext("One");
    inOrder.verify(observer, times(1)).onNext("a");
    inOrder.verify(observer, times(1)).onNext("b");
    inOrder.verifyNoMoreInteractions();
}
Also used : InOrder(org.mockito.InOrder) TestSubscriber(io.reactivex.subscribers.TestSubscriber)

Aggregations

TestSubscriber (io.reactivex.subscribers.TestSubscriber)63 Test (org.junit.Test)41 BooleanSubscription (io.reactivex.internal.subscriptions.BooleanSubscription)20 TestException (io.reactivex.exceptions.TestException)18 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)16 InOrder (org.mockito.InOrder)11 Disposable (io.reactivex.disposables.Disposable)9 TestScheduler (io.reactivex.schedulers.TestScheduler)6 AtomicLong (java.util.concurrent.atomic.AtomicLong)6 BooleanSupplier (io.reactivex.functions.BooleanSupplier)5 ArrayDeque (java.util.ArrayDeque)5 IOException (java.io.IOException)2 AtomicBoolean (java.util.concurrent.atomic.AtomicBoolean)2 Subscriber (org.reactivestreams.Subscriber)2 Flowable (io.reactivex.Flowable)1 Worker (io.reactivex.Scheduler.Worker)1 CompositeDisposable (io.reactivex.disposables.CompositeDisposable)1 Action (io.reactivex.functions.Action)1 Function (io.reactivex.functions.Function)1 LongConsumer (io.reactivex.functions.LongConsumer)1