Search in sources :

Example 56 with TestScheduler

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

the class FlowableThrottleFirstTest method testThrottle.

@Test
public void testThrottle() {
    Subscriber<Integer> observer = TestHelper.mockSubscriber();
    TestScheduler s = new TestScheduler();
    PublishProcessor<Integer> o = PublishProcessor.create();
    o.throttleFirst(500, TimeUnit.MILLISECONDS, s).subscribe(observer);
    // send events with simulated time increments
    s.advanceTimeTo(0, TimeUnit.MILLISECONDS);
    // deliver
    o.onNext(1);
    // skip
    o.onNext(2);
    s.advanceTimeTo(501, TimeUnit.MILLISECONDS);
    // deliver
    o.onNext(3);
    s.advanceTimeTo(600, TimeUnit.MILLISECONDS);
    // skip
    o.onNext(4);
    s.advanceTimeTo(700, TimeUnit.MILLISECONDS);
    // skip
    o.onNext(5);
    // skip
    o.onNext(6);
    s.advanceTimeTo(1001, TimeUnit.MILLISECONDS);
    // deliver
    o.onNext(7);
    s.advanceTimeTo(1501, TimeUnit.MILLISECONDS);
    o.onComplete();
    InOrder inOrder = inOrder(observer);
    inOrder.verify(observer).onNext(1);
    inOrder.verify(observer).onNext(3);
    inOrder.verify(observer).onNext(7);
    inOrder.verify(observer).onComplete();
    inOrder.verifyNoMoreInteractions();
}
Also used : InOrder(org.mockito.InOrder) TestScheduler(io.reactivex.schedulers.TestScheduler)

Example 57 with TestScheduler

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

the class FlowableTimeoutTests method shouldUnsubscribeFromUnderlyingSubscriptionOnDispose.

@Test
public void shouldUnsubscribeFromUnderlyingSubscriptionOnDispose() {
    final PublishProcessor<String> subject = PublishProcessor.create();
    final TestScheduler scheduler = new TestScheduler();
    final TestSubscriber<String> observer = subject.timeout(100, TimeUnit.MILLISECONDS, scheduler).test();
    assertTrue(subject.hasSubscribers());
    observer.dispose();
    assertFalse(subject.hasSubscribers());
}
Also used : TestScheduler(io.reactivex.schedulers.TestScheduler)

Example 58 with TestScheduler

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

the class FlowableTimeoutTests method shouldUnsubscribeFromUnderlyingSubscriptionOnTimeout.

@Test
public void shouldUnsubscribeFromUnderlyingSubscriptionOnTimeout() throws InterruptedException {
    // From https://github.com/ReactiveX/RxJava/pull/951
    final Subscription s = mock(Subscription.class);
    Flowable<String> never = Flowable.unsafeCreate(new Publisher<String>() {

        @Override
        public void subscribe(Subscriber<? super String> subscriber) {
            subscriber.onSubscribe(s);
        }
    });
    TestScheduler testScheduler = new TestScheduler();
    Flowable<String> observableWithTimeout = never.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(TimeoutException.class));
    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 59 with TestScheduler

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

the class FlowableTimeoutTests method setUp.

@Before
public void setUp() {
    underlyingSubject = PublishProcessor.create();
    testScheduler = new TestScheduler();
    withTimeout = underlyingSubject.timeout(TIMEOUT, TIME_UNIT, testScheduler);
}
Also used : TestScheduler(io.reactivex.schedulers.TestScheduler)

Example 60 with TestScheduler

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

the class FlowableTakeTimedTest method testTakeTimed.

@Test
public void testTakeTimed() {
    TestScheduler scheduler = new TestScheduler();
    PublishProcessor<Integer> source = PublishProcessor.create();
    Flowable<Integer> result = source.take(1, TimeUnit.SECONDS, scheduler);
    Subscriber<Object> o = TestHelper.mockSubscriber();
    result.subscribe(o);
    source.onNext(1);
    source.onNext(2);
    source.onNext(3);
    scheduler.advanceTimeBy(1, TimeUnit.SECONDS);
    source.onNext(4);
    InOrder inOrder = inOrder(o);
    inOrder.verify(o).onNext(1);
    inOrder.verify(o).onNext(2);
    inOrder.verify(o).onNext(3);
    inOrder.verify(o).onComplete();
    inOrder.verifyNoMoreInteractions();
    verify(o, never()).onNext(4);
    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