Search in sources :

Example 26 with TestSubscriber

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

the class TestObserverTest method testTerminalOneKind.

@Test
public void testTerminalOneKind() {
    TestSubscriber<Integer> to = new TestSubscriber<Integer>();
    to.onError(new TestException());
    to.onComplete();
    try {
        to.assertTerminated();
    } catch (AssertionError ex) {
        // this is expected
        return;
    }
    fail("Failed to report multiple kinds of events!");
}
Also used : TestException(io.reactivex.exceptions.TestException) TestSubscriber(io.reactivex.subscribers.TestSubscriber)

Example 27 with TestSubscriber

use of io.reactivex.subscribers.TestSubscriber 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 TestSubscriber

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

the class TrampolineSchedulerTest method testTrampolineWorkerHandlesConcurrentScheduling.

/**
     * This is a regression test for #1702. Concurrent work scheduling that is improperly synchronized can cause an
     * action to be added or removed onto the priority queue during a poll, which can result in NPEs during queue
     * sifting. While it is difficult to isolate the issue directly, we can easily trigger the behavior by spamming the
     * trampoline with enqueue requests from multiple threads concurrently.
     */
@Test
public void testTrampolineWorkerHandlesConcurrentScheduling() {
    final Worker trampolineWorker = Schedulers.trampoline().createWorker();
    final Subscriber<Object> observer = TestHelper.mockSubscriber();
    final TestSubscriber<Disposable> ts = new TestSubscriber<Disposable>(observer);
    // Spam the trampoline with actions.
    Flowable.range(0, 50).flatMap(new Function<Integer, Publisher<Disposable>>() {

        @Override
        public Publisher<Disposable> apply(Integer count) {
            return Flowable.interval(1, TimeUnit.MICROSECONDS).map(new Function<Long, Disposable>() {

                @Override
                public Disposable apply(Long ount1) {
                    return trampolineWorker.schedule(Functions.EMPTY_RUNNABLE);
                }
            }).take(100);
        }
    }).subscribeOn(Schedulers.computation()).subscribe(ts);
    ts.awaitTerminalEvent();
    ts.assertNoErrors();
}
Also used : CompositeDisposable(io.reactivex.disposables.CompositeDisposable) Disposable(io.reactivex.disposables.Disposable) Worker(io.reactivex.Scheduler.Worker) TestSubscriber(io.reactivex.subscribers.TestSubscriber) Publisher(org.reactivestreams.Publisher) Test(org.junit.Test)

Example 29 with TestSubscriber

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

the class SingleCacheTest method crossCancelOnError.

@Test
public void crossCancelOnError() {
    PublishSubject<Integer> ps = PublishSubject.create();
    Single<Integer> cache = ps.single(-99).cache();
    final TestSubscriber<Integer> ts1 = new TestSubscriber<Integer>();
    TestSubscriber<Integer> ts2 = new TestSubscriber<Integer>() {

        @Override
        public void onError(Throwable t) {
            super.onError(t);
            ts1.cancel();
        }
    };
    cache.toFlowable().subscribe(ts2);
    cache.toFlowable().subscribe(ts1);
    ps.onError(new TestException());
    ts1.assertNoValues().assertNoErrors().assertNotComplete();
    ts2.assertFailure(TestException.class);
}
Also used : TestException(io.reactivex.exceptions.TestException) TestSubscriber(io.reactivex.subscribers.TestSubscriber) Test(org.junit.Test)

Example 30 with TestSubscriber

use of io.reactivex.subscribers.TestSubscriber in project rxjava2-jdbc by davidmoten.

the class PoolTest method testConnectionPoolRecylesFirst.

@Test
public void testConnectionPoolRecylesFirst() {
    Database db = DatabaseCreator.create(2);
    TestSubscriber<Connection> ts = //
    db.connections().doOnNext(//
    System.out::println).doOnNext(c -> {
        c.close();
    }).test(//
    4);
    //
    ts.assertValueCount(4).assertNotTerminated();
    List<Object> list = ts.getEvents().get(0);
    // all 4 connections released were the same
    assertTrue(list.get(0) == list.get(1));
    assertTrue(list.get(0) == list.get(2));
    assertTrue(list.get(0) == list.get(3));
}
Also used : NonBlockingPool(org.davidmoten.rx.pool.NonBlockingPool) Arrays(java.util.Arrays) Connection(java.sql.Connection) Database(org.davidmoten.rx.jdbc.Database) Pool(org.davidmoten.rx.pool.Pool) Assert.assertTrue(org.junit.Assert.assertTrue) Test(org.junit.Test) MemberFactory(org.davidmoten.rx.pool.MemberFactory) ArrayList(java.util.ArrayList) SQLException(java.sql.SQLException) List(java.util.List) NonBlockingMember(org.davidmoten.rx.pool.NonBlockingMember) Flowable(io.reactivex.Flowable) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) Schedulers(io.reactivex.schedulers.Schedulers) TestSubscriber(io.reactivex.subscribers.TestSubscriber) Database(org.davidmoten.rx.jdbc.Database) Connection(java.sql.Connection) Test(org.junit.Test)

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