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());
}
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();
}
}
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();
}
}
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);
}
}
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();
}
Aggregations