Search in sources :

Example 26 with TestScheduler

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

the class ObservableThrottleWithTimeoutTests method testThrottle.

@Test
public void testThrottle() {
    Observer<Integer> observer = TestHelper.mockObserver();
    TestScheduler s = new TestScheduler();
    PublishSubject<Integer> o = PublishSubject.create();
    o.throttleWithTimeout(500, TimeUnit.MILLISECONDS, s).subscribe(observer);
    // send events with simulated time increments
    s.advanceTimeTo(0, TimeUnit.MILLISECONDS);
    // skip
    o.onNext(1);
    // deliver
    o.onNext(2);
    s.advanceTimeTo(501, TimeUnit.MILLISECONDS);
    // skip
    o.onNext(3);
    s.advanceTimeTo(600, TimeUnit.MILLISECONDS);
    // skip
    o.onNext(4);
    s.advanceTimeTo(700, TimeUnit.MILLISECONDS);
    // skip
    o.onNext(5);
    // deliver at 1300 after 500ms has passed since onNext(5)
    o.onNext(6);
    s.advanceTimeTo(1300, TimeUnit.MILLISECONDS);
    // deliver
    o.onNext(7);
    s.advanceTimeTo(1800, TimeUnit.MILLISECONDS);
    o.onComplete();
    InOrder inOrder = inOrder(observer);
    inOrder.verify(observer).onNext(2);
    inOrder.verify(observer).onNext(6);
    inOrder.verify(observer).onNext(7);
    inOrder.verify(observer).onComplete();
    inOrder.verifyNoMoreInteractions();
}
Also used : InOrder(org.mockito.InOrder) TestScheduler(io.reactivex.schedulers.TestScheduler) Test(org.junit.Test)

Example 27 with TestScheduler

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

the class ReplayProcessorTest method reentrantDrainBackpressured.

@Test
public void reentrantDrainBackpressured() {
    TestScheduler scheduler = new TestScheduler();
    final ReplayProcessor<Integer> rp = ReplayProcessor.createWithTimeAndSize(1, TimeUnit.SECONDS, scheduler, 2);
    TestSubscriber<Integer> ts = new TestSubscriber<Integer>(1L) {

        @Override
        public void onNext(Integer t) {
            if (t == 1) {
                rp.onNext(2);
            }
            super.onNext(t);
        }
    };
    rp.subscribe(ts);
    rp.onNext(1);
    rp.onComplete();
    ts.request(1);
    ts.assertResult(1, 2);
}
Also used : TestSubscriber(io.reactivex.subscribers.TestSubscriber) TestScheduler(io.reactivex.schedulers.TestScheduler) Test(org.junit.Test)

Example 28 with TestScheduler

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

the class ReplayProcessorTest method testReplayTimestampedAfterTermination.

@Test
public void testReplayTimestampedAfterTermination() {
    TestScheduler scheduler = new TestScheduler();
    ReplayProcessor<Integer> source = ReplayProcessor.createWithTime(1, TimeUnit.SECONDS, scheduler);
    source.onNext(1);
    scheduler.advanceTimeBy(1, TimeUnit.SECONDS);
    source.onNext(2);
    scheduler.advanceTimeBy(1, TimeUnit.SECONDS);
    source.onNext(3);
    source.onComplete();
    scheduler.advanceTimeBy(1, TimeUnit.SECONDS);
    Subscriber<Integer> o = TestHelper.mockSubscriber();
    source.subscribe(o);
    verify(o, never()).onNext(1);
    verify(o, never()).onNext(2);
    verify(o, never()).onNext(3);
    verify(o).onComplete();
    verify(o, never()).onError(any(Throwable.class));
}
Also used : TestScheduler(io.reactivex.schedulers.TestScheduler) Test(org.junit.Test)

Example 29 with TestScheduler

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

the class ReplayProcessorTest method testSizeAndHasAnyValueTimeBounded.

@Test
public void testSizeAndHasAnyValueTimeBounded() {
    TestScheduler ts = new TestScheduler();
    ReplayProcessor<Object> rs = ReplayProcessor.createWithTime(1, TimeUnit.SECONDS, ts);
    assertEquals(0, rs.size());
    assertFalse(rs.hasValue());
    for (int i = 0; i < 1000; i++) {
        rs.onNext(i);
        assertEquals(1, rs.size());
        assertTrue(rs.hasValue());
        ts.advanceTimeBy(2, TimeUnit.SECONDS);
        assertEquals(0, rs.size());
        assertFalse(rs.hasValue());
    }
    rs.onComplete();
    assertEquals(0, rs.size());
    assertFalse(rs.hasValue());
}
Also used : TestScheduler(io.reactivex.schedulers.TestScheduler) Test(org.junit.Test)

Example 30 with TestScheduler

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

the class ReplayProcessorTest method testReplayTimestampedDirectly.

@Test
public void testReplayTimestampedDirectly() {
    TestScheduler scheduler = new TestScheduler();
    ReplayProcessor<Integer> source = ReplayProcessor.createWithTime(1, TimeUnit.SECONDS, scheduler);
    source.onNext(1);
    scheduler.advanceTimeBy(1, TimeUnit.SECONDS);
    Subscriber<Integer> o = TestHelper.mockSubscriber();
    source.subscribe(o);
    source.onNext(2);
    scheduler.advanceTimeBy(1, TimeUnit.SECONDS);
    source.onNext(3);
    scheduler.advanceTimeBy(1, TimeUnit.SECONDS);
    source.onComplete();
    scheduler.advanceTimeBy(1, TimeUnit.SECONDS);
    verify(o, never()).onError(any(Throwable.class));
    verify(o, never()).onNext(1);
    verify(o).onNext(2);
    verify(o).onNext(3);
    verify(o).onComplete();
}
Also used : 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