Search in sources :

Example 61 with TestSubscriber

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

the class ReplayProcessorTest method testCompletedStopsEmittingData.

@Test
public void testCompletedStopsEmittingData() {
    ReplayProcessor<Integer> channel = ReplayProcessor.create();
    Subscriber<Object> observerA = TestHelper.mockSubscriber();
    Subscriber<Object> observerB = TestHelper.mockSubscriber();
    Subscriber<Object> observerC = TestHelper.mockSubscriber();
    Subscriber<Object> observerD = TestHelper.mockSubscriber();
    TestSubscriber<Object> ts = new TestSubscriber<Object>(observerA);
    channel.subscribe(ts);
    channel.subscribe(observerB);
    InOrder inOrderA = inOrder(observerA);
    InOrder inOrderB = inOrder(observerB);
    InOrder inOrderC = inOrder(observerC);
    InOrder inOrderD = inOrder(observerD);
    channel.onNext(42);
    // both A and B should have received 42 from before subscription
    inOrderA.verify(observerA).onNext(42);
    inOrderB.verify(observerB).onNext(42);
    ts.dispose();
    // a should receive no more
    inOrderA.verifyNoMoreInteractions();
    channel.onNext(4711);
    // only be should receive 4711 at this point
    inOrderB.verify(observerB).onNext(4711);
    channel.onComplete();
    // B is subscribed so should receive onComplete
    inOrderB.verify(observerB).onComplete();
    channel.subscribe(observerC);
    // when C subscribes it should receive 42, 4711, onComplete
    inOrderC.verify(observerC).onNext(42);
    inOrderC.verify(observerC).onNext(4711);
    inOrderC.verify(observerC).onComplete();
    // if further events are propagated they should be ignored
    channel.onNext(13);
    channel.onNext(14);
    channel.onNext(15);
    channel.onError(new RuntimeException());
    // a new subscription should only receive what was emitted prior to terminal state onComplete
    channel.subscribe(observerD);
    inOrderD.verify(observerD).onNext(42);
    inOrderD.verify(observerD).onNext(4711);
    inOrderD.verify(observerD).onComplete();
    verify(observerA).onSubscribe((Subscription) notNull());
    verify(observerB).onSubscribe((Subscription) notNull());
    verify(observerC).onSubscribe((Subscription) notNull());
    verify(observerD).onSubscribe((Subscription) notNull());
    Mockito.verifyNoMoreInteractions(observerA);
    Mockito.verifyNoMoreInteractions(observerB);
    Mockito.verifyNoMoreInteractions(observerC);
    Mockito.verifyNoMoreInteractions(observerD);
}
Also used : InOrder(org.mockito.InOrder) TestSubscriber(io.reactivex.subscribers.TestSubscriber) Test(org.junit.Test)

Example 62 with TestSubscriber

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

the class ReplayProcessorTest method reentrantDrain.

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

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

Example 63 with TestSubscriber

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

the class TestObserverTest method testTerminalErrorOnce.

@Test
public void testTerminalErrorOnce() {
    TestSubscriber<Integer> to = new TestSubscriber<Integer>();
    to.onError(new TestException());
    to.onError(new TestException());
    try {
        to.assertTerminated();
    } catch (AssertionError ex) {
        // this is expected
        return;
    }
    fail("Failed to report multiple onError terminal events!");
}
Also used : TestException(io.reactivex.exceptions.TestException) 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