Search in sources :

Example 71 with TestScheduler

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

the class ObservableThrottleFirstTest method testThrottle.

@Test
public void testThrottle() {
    Observer<Integer> observer = TestHelper.mockObserver();
    TestScheduler s = new TestScheduler();
    PublishSubject<Integer> o = PublishSubject.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 72 with TestScheduler

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

the class ObservableTimeoutTests 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 Disposable s = mock(Disposable.class);
    Observable<String> immediatelyError = Observable.unsafeCreate(new ObservableSource<String>() {

        @Override
        public void subscribe(Observer<? super String> observer) {
            observer.onSubscribe(s);
            observer.onError(new IOException("Error"));
        }
    });
    TestScheduler testScheduler = new TestScheduler();
    Observable<String> observableWithTimeout = immediatelyError.timeout(1000, TimeUnit.MILLISECONDS, testScheduler);
    Observer<String> observer = TestHelper.mockObserver();
    TestObserver<String> ts = new TestObserver<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)).dispose();
}
Also used : InOrder(org.mockito.InOrder) IOException(java.io.IOException) TestScheduler(io.reactivex.schedulers.TestScheduler) TestObserver(io.reactivex.observers.TestObserver)

Example 73 with TestScheduler

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

the class ObservableTimeoutTests method shouldUnsubscribeFromUnderlyingSubscriptionOnTimeout.

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

        @Override
        public void subscribe(Observer<? super String> observer) {
            observer.onSubscribe(s);
        }
    });
    TestScheduler testScheduler = new TestScheduler();
    Observable<String> observableWithTimeout = never.timeout(1000, TimeUnit.MILLISECONDS, testScheduler);
    Observer<String> observer = TestHelper.mockObserver();
    TestObserver<String> ts = new TestObserver<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)).dispose();
}
Also used : InOrder(org.mockito.InOrder) TestScheduler(io.reactivex.schedulers.TestScheduler) TestObserver(io.reactivex.observers.TestObserver)

Example 74 with TestScheduler

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

the class ObservableTimeoutTests method shouldUnsubscribeFromUnderlyingSubscriptionOnDispose.

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

Example 75 with TestScheduler

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

the class ObservableTimeoutTests method setUp.

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

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