Search in sources :

Example 1 with TestSubscriber

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

the class FlowableCacheTest method unsafeChildThrows.

@Test
@Ignore("RS subscribers should not throw")
public void unsafeChildThrows() {
    final AtomicInteger count = new AtomicInteger();
    Flowable<Integer> source = Flowable.range(1, 100).doOnNext(new Consumer<Integer>() {

        @Override
        public void accept(Integer t) {
            count.getAndIncrement();
        }
    }).cache();
    TestSubscriber<Integer> ts = new TestSubscriber<Integer>() {

        @Override
        public void onNext(Integer t) {
            throw new TestException();
        }
    };
    source.subscribe(ts);
    Assert.assertEquals(100, count.get());
    ts.assertNoValues();
    ts.assertNotComplete();
    ts.assertError(TestException.class);
}
Also used : AtomicInteger(java.util.concurrent.atomic.AtomicInteger) TestException(io.reactivex.exceptions.TestException) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) TestSubscriber(io.reactivex.subscribers.TestSubscriber)

Example 2 with TestSubscriber

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

the class FlowableDelaySubscriptionOtherTest method testNoSubscriptionIfOtherErrors.

@Test
public void testNoSubscriptionIfOtherErrors() {
    PublishProcessor<Object> other = PublishProcessor.create();
    TestSubscriber<Integer> ts = new TestSubscriber<Integer>();
    final AtomicInteger subscribed = new AtomicInteger();
    Flowable.<Integer>error(new TestException()).doOnSubscribe(new Consumer<Subscription>() {

        @Override
        public void accept(Subscription s) {
            subscribed.getAndIncrement();
        }
    }).delaySubscription(other).subscribe(ts);
    ts.assertNotComplete();
    ts.assertNoErrors();
    ts.assertNoValues();
    Assert.assertEquals("Premature subscription", 0, subscribed.get());
    other.onError(new TestException());
    Assert.assertEquals("Premature subscription", 0, subscribed.get());
    ts.assertNoValues();
    ts.assertNotComplete();
    ts.assertError(TestException.class);
}
Also used : TestException(io.reactivex.exceptions.TestException) TestSubscriber(io.reactivex.subscribers.TestSubscriber) Subscription(org.reactivestreams.Subscription)

Example 3 with TestSubscriber

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

the class FlowableDoAfterTerminateTest method ifFinallyActionThrowsExceptionShouldNotBeSwallowedAndActionShouldBeCalledOnce.

@Test
public void ifFinallyActionThrowsExceptionShouldNotBeSwallowedAndActionShouldBeCalledOnce() throws Exception {
    Action finallyAction = Mockito.mock(Action.class);
    doThrow(new IllegalStateException()).when(finallyAction).run();
    TestSubscriber<String> testSubscriber = new TestSubscriber<String>();
    Flowable.just("value").doAfterTerminate(finallyAction).subscribe(testSubscriber);
    testSubscriber.assertValue("value");
    verify(finallyAction).run();
// Actual result:
// Not only IllegalStateException was swallowed
// But finallyAction was called twice!
}
Also used : Action(io.reactivex.functions.Action) TestSubscriber(io.reactivex.subscribers.TestSubscriber)

Example 4 with TestSubscriber

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

the class FlowableOnBackpressureLatestTest method testAsynchronousDrop.

@Test
public void testAsynchronousDrop() throws InterruptedException {
    TestSubscriber<Integer> ts = new TestSubscriber<Integer>(1L) {

        final Random rnd = new Random();

        @Override
        public void onNext(Integer t) {
            super.onNext(t);
            if (rnd.nextDouble() < 0.001) {
                try {
                    Thread.sleep(1);
                } catch (InterruptedException ex) {
                    ex.printStackTrace();
                }
            }
            request(1);
        }
    };
    int m = 100000;
    Flowable.range(1, m).subscribeOn(Schedulers.computation()).onBackpressureLatest().observeOn(Schedulers.io()).subscribe(ts);
    ts.awaitTerminalEvent(2, TimeUnit.SECONDS);
    ts.assertTerminated();
    int n = ts.values().size();
    System.out.println("testAsynchronousDrop -> " + n);
    Assert.assertTrue("All events received?", n < m);
}
Also used : Random(java.util.Random) TestSubscriber(io.reactivex.subscribers.TestSubscriber)

Example 5 with TestSubscriber

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

the class FlowablePublishFunctionTest method oneStartOnly.

@Test
public void oneStartOnly() {
    final AtomicInteger startCount = new AtomicInteger();
    TestSubscriber<Integer> ts = new TestSubscriber<Integer>() {

        @Override
        public void onStart() {
            startCount.incrementAndGet();
        }
    };
    PublishProcessor<Integer> ps = PublishProcessor.create();
    ps.publish(new Function<Flowable<Integer>, Flowable<Integer>>() {

        @Override
        public Flowable<Integer> apply(Flowable<Integer> o) {
            return o.take(1);
        }
    }).subscribe(ts);
    Assert.assertEquals(1, startCount.get());
}
Also used : AtomicInteger(java.util.concurrent.atomic.AtomicInteger) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) 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