Search in sources :

Example 11 with TestSubscriber

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

the class SingleSwitchOnNextTest method delaySwitch.

@Test
public void delaySwitch() {
    PublishProcessor<Single<Integer>> pp = PublishProcessor.create();
    TestSubscriber<Integer> ts = Single.switchOnNextDelayError(pp).test();
    assertTrue(pp.hasSubscribers());
    ts.assertEmpty();
    SingleSubject<Integer> ss1 = SingleSubject.create();
    SingleSubject<Integer> ss2 = SingleSubject.create();
    pp.onNext(ss1);
    assertTrue(ss1.hasObservers());
    pp.onNext(ss2);
    assertFalse(ss1.hasObservers());
    assertTrue(ss2.hasObservers());
    assertTrue(ss2.hasObservers());
    ss2.onError(new TestException());
    assertTrue(pp.hasSubscribers());
    ts.assertEmpty();
    pp.onComplete();
    ts.assertFailure(TestException.class);
}
Also used : TestException(io.reactivex.rxjava3.exceptions.TestException) Test(org.junit.Test)

Example 12 with TestSubscriber

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

the class TrampolineSchedulerTest method trampolineWorkerHandlesConcurrentScheduling.

/**
 * 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 trampolineWorkerHandlesConcurrentScheduling() {
    final Worker trampolineWorker = Schedulers.trampoline().createWorker();
    final Subscriber<Object> subscriber = TestHelper.mockSubscriber();
    final TestSubscriber<Disposable> ts = new TestSubscriber<>(subscriber);
    // 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.awaitDone(5, TimeUnit.SECONDS);
    ts.assertNoErrors();
}
Also used : Worker(io.reactivex.rxjava3.core.Scheduler.Worker) TestSubscriber(io.reactivex.rxjava3.subscribers.TestSubscriber) Test(org.junit.Test)

Example 13 with TestSubscriber

use of io.reactivex.rxjava3.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<>();
    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.rxjava3.exceptions.TestException) TestSubscriber(io.reactivex.rxjava3.subscribers.TestSubscriber) Test(org.junit.Test)

Example 14 with TestSubscriber

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

the class SafeSubscriberTest method dispose.

@Test
public void dispose() {
    TestSubscriber<Integer> ts = new TestSubscriber<>();
    SafeSubscriber<Integer> so = new SafeSubscriber<>(ts);
    BooleanSubscription bs = new BooleanSubscription();
    so.onSubscribe(bs);
    ts.dispose();
    assertTrue(bs.isCancelled());
// assertTrue(so.isDisposed());
}
Also used : BooleanSubscription(io.reactivex.rxjava3.internal.subscriptions.BooleanSubscription) Test(org.junit.Test)

Example 15 with TestSubscriber

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

the class TestSubscriberTest method interruptTerminalEventAwaitAndUnsubscribe.

@Test
public void interruptTerminalEventAwaitAndUnsubscribe() {
    TestSubscriber<Integer> ts = new TestSubscriber<>();
    final Thread t0 = Thread.currentThread();
    Worker w = Schedulers.computation().createWorker();
    try {
        w.schedule(new Runnable() {

            @Override
            public void run() {
                t0.interrupt();
            }
        }, 200, TimeUnit.MILLISECONDS);
        try {
            ts.awaitDone(5, TimeUnit.SECONDS);
        } catch (RuntimeException allowed) {
            assertTrue(allowed.toString(), allowed.getCause() instanceof InterruptedException);
        }
        ts.dispose();
        if (!ts.isCancelled()) {
            fail("Did not unsubscribe!");
        }
    } finally {
        w.dispose();
    }
}
Also used : ThrowingRunnable(org.junit.function.ThrowingRunnable) Worker(io.reactivex.rxjava3.core.Scheduler.Worker) Test(org.junit.Test)

Aggregations

Test (org.junit.Test)169 TestSubscriber (io.reactivex.rxjava3.subscribers.TestSubscriber)116 BooleanSubscription (io.reactivex.rxjava3.internal.subscriptions.BooleanSubscription)106 TestException (io.reactivex.rxjava3.exceptions.TestException)56 RxJavaTest (io.reactivex.rxjava3.core.RxJavaTest)27 TimeUnit (java.util.concurrent.TimeUnit)19 FlowableRxInvokerProvider (org.apache.cxf.jaxrs.rx3.client.FlowableRxInvokerProvider)18 FlowableRxInvoker (org.apache.cxf.jaxrs.rx3.client.FlowableRxInvoker)17 JacksonJsonProvider (com.fasterxml.jackson.jaxrs.json.JacksonJsonProvider)15 IOException (java.io.IOException)15 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)15 Flowable (io.reactivex.rxjava3.core.Flowable)13 InternalServerErrorException (javax.ws.rs.InternalServerErrorException)13 ClientBuilder (javax.ws.rs.client.ClientBuilder)13 MediaType (javax.ws.rs.core.MediaType)13 AbstractResourceInfo (org.apache.cxf.jaxrs.model.AbstractResourceInfo)13 AbstractBusClientServerTestBase (org.apache.cxf.testutil.common.AbstractBusClientServerTestBase)13 Assert.assertTrue (org.junit.Assert.assertTrue)13 BeforeClass (org.junit.BeforeClass)13 TestScheduler (io.reactivex.rxjava3.schedulers.TestScheduler)12