/**是否含有观察者*/ public boolean hasObservers() { return _bus.hasObservers(); } }
@Override public boolean hasObservers() { return source.hasObservers(); }
public boolean hasObservers() { return _bus.hasObservers(); } }
@Override public boolean hasSubscribers() { return source.hasObservers(); }
/** * 判断是否有订阅者 */ public boolean hasObservers() { return _bus.hasObservers(); }
@Override public boolean hasObservers() { return delegate.hasObservers(); }
@Test public void sj2ToSj1Error() { io.reactivex.subjects.PublishSubject<Integer> ps2 = io.reactivex.subjects.PublishSubject.create(); rx.subjects.Subject<Integer, Integer> sj1 = toV1Subject(ps2); rx.observers.AssertableSubscriber<Integer> to = sj1.test(); assertTrue(sj1.hasObservers()); assertTrue(ps2.hasObservers()); sj1.onError(new IOException()); assertFalse(sj1.hasObservers()); assertFalse(ps2.hasObservers()); to.assertFailure(IOException.class); }
@Test public void sj2ToSj1Normal() { io.reactivex.subjects.PublishSubject<Integer> ps2 = io.reactivex.subjects.PublishSubject.create(); rx.subjects.Subject<Integer, Integer> sj1 = toV1Subject(ps2); rx.observers.AssertableSubscriber<Integer> to = sj1.test(); assertTrue(sj1.hasObservers()); assertTrue(ps2.hasObservers()); sj1.onNext(1); sj1.onNext(2); sj1.onCompleted(); assertFalse(sj1.hasObservers()); assertFalse(ps2.hasObservers()); to.assertResult(1, 2); }
@Test public void sj1ToFp2Lifecycle() { rx.subjects.Subject<Integer, Integer> sj1 = rx.subjects.PublishSubject.create(); io.reactivex.processors.FlowableProcessor<Integer> pp2 = toV2Processor(sj1); io.reactivex.subscribers.TestSubscriber<Integer> to = pp2.test(0L); assertTrue(sj1.hasObservers()); assertTrue(pp2.hasSubscribers()); pp2.onNext(1); pp2.onError(new IOException()); assertFalse(sj1.hasObservers()); assertFalse(pp2.hasSubscribers()); assertFalse(pp2.hasComplete()); assertTrue(pp2.hasThrowable()); assertNotNull(pp2.getThrowable()); to.assertFailure(rx.exceptions.MissingBackpressureException.class); }
@Test public void sj2ToSj1Lifecycle() { io.reactivex.subjects.PublishSubject<Integer> pp2 = io.reactivex.subjects.PublishSubject.create(); rx.subjects.Subject<Integer, Integer> sj1 = toV1Subject(pp2); rx.observers.AssertableSubscriber<Integer> to = sj1.test(0L); assertTrue(sj1.hasObservers()); assertTrue(pp2.hasObservers()); sj1.onNext(1); sj1.onError(new IOException()); assertFalse(sj1.hasObservers()); assertFalse(pp2.hasObservers()); assertFalse(pp2.hasComplete()); assertTrue(pp2.hasThrowable()); assertNotNull(pp2.getThrowable()); to.assertFailure(rx.exceptions.MissingBackpressureException.class); }
@Test public void sj1ToFp2NullValue() { rx.subjects.Subject<Integer, Integer> sj1 = rx.subjects.PublishSubject.create(); io.reactivex.processors.FlowableProcessor<Integer> pp2 = toV2Processor(sj1); io.reactivex.subscribers.TestSubscriber<Integer> to = pp2.test(); pp2.onNext(null); assertFalse(pp2.hasSubscribers()); assertFalse(sj1.hasObservers()); to.assertFailure(NullPointerException.class); }
@Test public void sj1ToFp2NullException() { rx.subjects.Subject<Integer, Integer> sj1 = rx.subjects.PublishSubject.create(); io.reactivex.processors.FlowableProcessor<Integer> pp2 = toV2Processor(sj1); io.reactivex.subscribers.TestSubscriber<Integer> to = pp2.test(); pp2.onError(null); assertFalse(pp2.hasSubscribers()); assertFalse(sj1.hasObservers()); to.assertFailure(NullPointerException.class); }
@Test public void fp2ToSj1Error() { io.reactivex.processors.FlowableProcessor<Integer> pp2 = io.reactivex.processors.PublishProcessor.create(); rx.subjects.Subject<Integer, Integer> sj1 = toV1Subject(pp2); rx.observers.AssertableSubscriber<Integer> to = sj1.test(); assertTrue(sj1.hasObservers()); assertTrue(pp2.hasSubscribers()); sj1.onError(new IOException()); assertFalse(sj1.hasObservers()); assertFalse(pp2.hasSubscribers()); assertFalse(pp2.hasComplete()); assertTrue(pp2.hasThrowable()); assertNotNull(pp2.getThrowable()); assertTrue(pp2.getThrowable() instanceof IOException); to.assertFailure(IOException.class); }
@Test public void sj1ToFp2Error() { rx.subjects.Subject<Integer, Integer> sj1 = rx.subjects.PublishSubject.create(); io.reactivex.processors.FlowableProcessor<Integer> pp2 = toV2Processor(sj1); io.reactivex.subscribers.TestSubscriber<Integer> to = pp2.test(); assertTrue(sj1.hasObservers()); assertTrue(pp2.hasSubscribers()); pp2.onError(new IOException()); assertFalse(sj1.hasObservers()); assertFalse(pp2.hasSubscribers()); assertFalse(pp2.hasComplete()); assertTrue(pp2.hasThrowable()); assertNotNull(pp2.getThrowable()); assertTrue(pp2.getThrowable() instanceof IOException); to.assertFailure(IOException.class); }
@Test public void sj2ToSj1Backpressured() { io.reactivex.subjects.PublishSubject<Integer> pp2 = io.reactivex.subjects.PublishSubject.create(); rx.subjects.Subject<Integer, Integer> sj1 = toV1Subject(pp2); rx.observers.AssertableSubscriber<Integer> to = sj1.test(0L); assertTrue(sj1.hasObservers()); assertTrue(pp2.hasObservers()); sj1.onNext(1); assertFalse(sj1.hasObservers()); assertFalse(pp2.hasObservers()); assertFalse(pp2.hasComplete()); assertFalse(pp2.hasThrowable()); assertNull(pp2.getThrowable()); to.assertFailure(rx.exceptions.MissingBackpressureException.class); }
@Test public void fp2ToSj1Normal() { io.reactivex.processors.FlowableProcessor<Integer> pp2 = io.reactivex.processors.PublishProcessor.create(); rx.subjects.Subject<Integer, Integer> sj1 = toV1Subject(pp2); rx.observers.AssertableSubscriber<Integer> to = sj1.test(); assertTrue(sj1.hasObservers()); assertTrue(pp2.hasSubscribers()); sj1.onNext(1); sj1.onNext(2); sj1.onCompleted(); assertFalse(sj1.hasObservers()); assertFalse(pp2.hasSubscribers()); assertTrue(pp2.hasComplete()); assertFalse(pp2.hasThrowable()); assertNull(pp2.getThrowable()); to.assertResult(1, 2); }
@Test public void sj1ToFp2Normal() { rx.subjects.Subject<Integer, Integer> sj1 = rx.subjects.PublishSubject.create(); io.reactivex.processors.FlowableProcessor<Integer> pp2 = toV2Processor(sj1); io.reactivex.subscribers.TestSubscriber<Integer> to = pp2.test(); assertTrue(sj1.hasObservers()); assertTrue(pp2.hasSubscribers()); pp2.onNext(1); pp2.onNext(2); pp2.onComplete(); assertFalse(sj1.hasObservers()); assertFalse(pp2.hasSubscribers()); assertTrue(pp2.hasComplete()); assertFalse(pp2.hasThrowable()); assertNull(pp2.getThrowable()); to.assertResult(1, 2); }
@Test public void sj1ToFp2Lifecycle2() { rx.subjects.Subject<Integer, Integer> sj1 = rx.subjects.PublishSubject.create(); io.reactivex.processors.FlowableProcessor<Integer> pp2 = toV2Processor(sj1); io.reactivex.subscribers.TestSubscriber<Integer> to = pp2.test(); assertTrue(pp2.hasSubscribers()); assertTrue(sj1.hasObservers()); assertFalse(pp2.hasComplete()); assertFalse(pp2.hasThrowable()); assertNull(pp2.getThrowable()); BooleanSubscription d1 = new BooleanSubscription(); pp2.onSubscribe(d1); assertFalse(d1.isCancelled()); pp2.onNext(1); pp2.onNext(2); pp2.onComplete(); pp2.onComplete(); pp2.onError(new IOException()); pp2.onNext(3); BooleanSubscription d2 = new BooleanSubscription(); pp2.onSubscribe(d2); assertFalse(d1.isCancelled()); assertTrue(d2.isCancelled()); assertFalse(pp2.hasSubscribers()); assertFalse(sj1.hasObservers()); assertTrue(pp2.hasComplete()); assertFalse(pp2.hasThrowable()); assertNull(pp2.getThrowable()); to.assertResult(1, 2); }
@Test public void fp2ToSj1Backpressured() { io.reactivex.processors.FlowableProcessor<Integer> pp2 = io.reactivex.processors.ReplayProcessor.create(); rx.subjects.Subject<Integer, Integer> sj1 = toV1Subject(pp2); rx.observers.AssertableSubscriber<Integer> to = sj1.test(0L); assertTrue(sj1.hasObservers()); assertTrue(pp2.hasSubscribers()); sj1.onNext(1); sj1.onNext(2); sj1.onNext(3); sj1.onNext(4); to.assertNoValues().assertNoErrors().assertNotCompleted(); to.requestMore(1).assertValue(1).assertNoErrors().assertNotCompleted(); to.requestMore(2).assertValues(1, 2, 3).assertNoErrors().assertNotCompleted(); sj1.onCompleted(); to.assertValues(1, 2, 3).assertNoErrors().assertNotCompleted(); assertFalse(sj1.hasObservers()); assertFalse(pp2.hasSubscribers()); assertTrue(pp2.hasComplete()); assertFalse(pp2.hasThrowable()); assertNull(pp2.getThrowable()); to .requestMore(1) .assertResult(1, 2, 3, 4); }
@Test public void sj1ToFp2Backpressured() { rx.subjects.Subject<Integer, Integer> sj1 = rx.subjects.ReplaySubject.create(); io.reactivex.processors.FlowableProcessor<Integer> pp2 = toV2Processor(sj1); io.reactivex.subscribers.TestSubscriber<Integer> to = pp2.test(0L); assertTrue(sj1.hasObservers()); assertTrue(pp2.hasSubscribers()); pp2.onNext(1); pp2.onNext(2); pp2.onNext(3); pp2.onNext(4); to.assertNoValues().assertNoErrors().assertNotComplete(); to.requestMore(1).assertValue(1).assertNoErrors().assertNotComplete(); to.requestMore(2).assertValues(1, 2, 3).assertNoErrors().assertNotComplete(); pp2.onComplete(); to.assertValues(1, 2, 3).assertNoErrors().assertNotComplete(); assertFalse(sj1.hasObservers()); assertFalse(pp2.hasSubscribers()); assertTrue(pp2.hasComplete()); assertFalse(pp2.hasThrowable()); assertNull(pp2.getThrowable()); to .requestMore(1) .assertResult(1, 2, 3, 4); }