Search in sources :

Example 31 with TestSubscriber

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

the class FlowableMergeWithMaybeTest method cancelMainOnOtherError.

@Test
public void cancelMainOnOtherError() {
    PublishProcessor<Integer> pp = PublishProcessor.create();
    MaybeSubject<Integer> ms = MaybeSubject.create();
    TestSubscriber<Integer> ts = pp.mergeWith(ms).test();
    assertTrue(pp.hasSubscribers());
    assertTrue(ms.hasObservers());
    ms.onError(new TestException());
    ts.assertFailure(TestException.class);
    assertFalse("main has observers!", pp.hasSubscribers());
    assertFalse("other has observers", ms.hasObservers());
}
Also used : TestException(io.reactivex.rxjava3.exceptions.TestException) Test(org.junit.Test)

Example 32 with TestSubscriber

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

the class SerializedSubscriberTest method startOnce.

@Test
public void startOnce() {
    List<Throwable> error = TestHelper.trackPluginErrors();
    try {
        TestSubscriber<Integer> ts = new TestSubscriber<>();
        final SerializedSubscriber<Integer> so = new SerializedSubscriber<>(ts);
        so.onSubscribe(new BooleanSubscription());
        BooleanSubscription bs = new BooleanSubscription();
        so.onSubscribe(bs);
        assertTrue(bs.isCancelled());
        TestHelper.assertError(error, 0, IllegalStateException.class, "Subscription already set!");
    } finally {
        RxJavaPlugins.reset();
    }
}
Also used : BooleanSubscription(io.reactivex.rxjava3.internal.subscriptions.BooleanSubscription)

Example 33 with TestSubscriber

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

the class SerializedSubscriberTest method onCompleteRace.

@Test
public void onCompleteRace() {
    for (int i = 0; i < TestHelper.RACE_DEFAULT_LOOPS; i++) {
        TestSubscriber<Integer> ts = new TestSubscriber<>();
        final SerializedSubscriber<Integer> so = new SerializedSubscriber<>(ts);
        BooleanSubscription bs = new BooleanSubscription();
        so.onSubscribe(bs);
        Runnable r = new Runnable() {

            @Override
            public void run() {
                so.onComplete();
            }
        };
        TestHelper.race(r, r);
        ts.awaitDone(5, TimeUnit.SECONDS).assertResult();
    }
}
Also used : BooleanSubscription(io.reactivex.rxjava3.internal.subscriptions.BooleanSubscription)

Example 34 with TestSubscriber

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

the class SerializedSubscriberTest method onNextOnCompleteRace.

@Test
public void onNextOnCompleteRace() {
    for (int i = 0; i < TestHelper.RACE_DEFAULT_LOOPS; i++) {
        TestSubscriber<Integer> ts = new TestSubscriber<>();
        final SerializedSubscriber<Integer> so = new SerializedSubscriber<>(ts);
        BooleanSubscription bs = new BooleanSubscription();
        so.onSubscribe(bs);
        Runnable r1 = new Runnable() {

            @Override
            public void run() {
                so.onComplete();
            }
        };
        Runnable r2 = new Runnable() {

            @Override
            public void run() {
                so.onNext(1);
            }
        };
        TestHelper.race(r1, r2);
        ts.awaitDone(5, TimeUnit.SECONDS).assertNoErrors().assertComplete();
        assertTrue(ts.values().size() <= 1);
    }
}
Also used : BooleanSubscription(io.reactivex.rxjava3.internal.subscriptions.BooleanSubscription)

Example 35 with TestSubscriber

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

the class SerializedSubscriberTest method threadStarvation.

/**
 * Demonstrates thread starvation problem.
 *
 * No solution on this for now. Trade-off in this direction as per https://github.com/ReactiveX/RxJava/issues/998#issuecomment-38959474
 * Probably need backpressure for this to work
 *
 * When using SynchronizedSubscriber we get this output:
 *
 * {@code p1: 18 p2: 68 =>} should be close to each other unless we have thread starvation
 *
 * When using SerializedSubscriber we get:
 *
 * {@code p1: 1 p2: 2445261 =>} should be close to each other unless we have thread starvation
 *
 * This demonstrates how SynchronizedSubscriber balances back and forth better, and blocks emission.
 * The real issue in this example is the async buffer-bloat, so we need backpressure.
 *
 * @throws InterruptedException if the await is interrupted
 */
@Ignore("Demonstrates thread starvation problem. Read JavaDoc")
@Test
public void threadStarvation() throws InterruptedException {
    TestSubscriber<String> ts = new TestSubscriber<>(new DefaultSubscriber<String>() {

        @Override
        public void onComplete() {
        }

        @Override
        public void onError(Throwable e) {
        }

        @Override
        public void onNext(String t) {
            // force it to take time when delivering
            try {
                Thread.sleep(1);
            } catch (InterruptedException e) {
            }
        }
    });
    final Subscriber<String> subscriber = serializedSubscriber(ts);
    AtomicInteger p1 = new AtomicInteger();
    AtomicInteger p2 = new AtomicInteger();
    subscriber.onSubscribe(new BooleanSubscription());
    ResourceSubscriber<String> as1 = new ResourceSubscriber<String>() {

        @Override
        public void onNext(String t) {
            subscriber.onNext(t);
        }

        @Override
        public void onError(Throwable t) {
            RxJavaPlugins.onError(t);
        }

        @Override
        public void onComplete() {
        }
    };
    ResourceSubscriber<String> as2 = new ResourceSubscriber<String>() {

        @Override
        public void onNext(String t) {
            subscriber.onNext(t);
        }

        @Override
        public void onError(Throwable t) {
            RxJavaPlugins.onError(t);
        }

        @Override
        public void onComplete() {
        }
    };
    infinite(p1).subscribe(as1);
    infinite(p2).subscribe(as2);
    Thread.sleep(100);
    System.out.println("p1: " + p1.get() + " p2: " + p2.get() + " => should be close to each other unless we have thread starvation");
    // fairly distributed within 10000 of each other
    assertEquals(p1.get(), p2.get(), 10000);
    as1.dispose();
    as2.dispose();
}
Also used : BooleanSubscription(io.reactivex.rxjava3.internal.subscriptions.BooleanSubscription)

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