Search in sources :

Example 6 with TestScheduler

use of io.reactivex.schedulers.TestScheduler in project RxJava by ReactiveX.

the class CompletableDisposeOnTest method normal.

@Test
public void normal() {
    TestScheduler scheduler = new TestScheduler();
    final int[] call = { 0 };
    Completable.complete().doOnDispose(new Action() {

        @Override
        public void run() throws Exception {
            call[0]++;
        }
    }).unsubscribeOn(scheduler).test().assertResult();
    scheduler.triggerActions();
    assertEquals(0, call[0]);
}
Also used : Action(io.reactivex.functions.Action) TestScheduler(io.reactivex.schedulers.TestScheduler) Test(org.junit.Test)

Example 7 with TestScheduler

use of io.reactivex.schedulers.TestScheduler in project RxJava by ReactiveX.

the class BlockingFlowableLatestTest method testNextThrows.

@Test(timeout = 1000, expected = RuntimeException.class)
public void testNextThrows() {
    TestScheduler scheduler = new TestScheduler();
    Flowable<Long> source = Flowable.<Long>error(new RuntimeException("Forced failure!")).subscribeOn(scheduler);
    Iterable<Long> iter = source.blockingLatest();
    Iterator<Long> it = iter.iterator();
    scheduler.advanceTimeBy(1, TimeUnit.SECONDS);
    it.next();
}
Also used : TestScheduler(io.reactivex.schedulers.TestScheduler)

Example 8 with TestScheduler

use of io.reactivex.schedulers.TestScheduler in project RxJava by ReactiveX.

the class FlowableTimeoutTests method shouldUnsubscribeFromUnderlyingSubscriptionOnImmediatelyErrored.

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

        @Override
        public void subscribe(Subscriber<? super String> subscriber) {
            subscriber.onSubscribe(s);
            subscriber.onError(new IOException("Error"));
        }
    });
    TestScheduler testScheduler = new TestScheduler();
    Flowable<String> observableWithTimeout = immediatelyError.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).onError(isA(IOException.class));
    inOrder.verifyNoMoreInteractions();
    verify(s, times(1)).cancel();
}
Also used : InOrder(org.mockito.InOrder) TestSubscriber(io.reactivex.subscribers.TestSubscriber) IOException(java.io.IOException) BooleanSubscription(io.reactivex.internal.subscriptions.BooleanSubscription) TestScheduler(io.reactivex.schedulers.TestScheduler)

Example 9 with TestScheduler

use of io.reactivex.schedulers.TestScheduler 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 10 with TestScheduler

use of io.reactivex.schedulers.TestScheduler in project RxJava by ReactiveX.

the class FlowableSkipTimedTest method testSkipTimedFinishBeforeTime.

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

Aggregations

TestScheduler (io.reactivex.schedulers.TestScheduler)94 Test (org.junit.Test)53 InOrder (org.mockito.InOrder)26 TestException (io.reactivex.exceptions.TestException)11 Worker (io.reactivex.Scheduler.Worker)6 TestSubscriber (io.reactivex.subscribers.TestSubscriber)6 BooleanSubscription (io.reactivex.internal.subscriptions.BooleanSubscription)5 TestObserver (io.reactivex.observers.TestObserver)5 Disposable (io.reactivex.disposables.Disposable)3 Action (io.reactivex.functions.Action)3 AtomicLong (java.util.concurrent.atomic.AtomicLong)3 IOException (java.io.IOException)2 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)2 NonBlockingConnectionPool (org.davidmoten.rx.jdbc.pool.NonBlockingConnectionPool)1 Before (org.junit.Before)1