@Override public void run() { ms.onError(ex2); } };
@Override public void run() { ms.onError(ex2); } };
@Override public void run() { ms.onError(ex); } };
@Override public void run() { subj.onError(ex); } };
@Override public void run() { ms.onError(ex); } };
@Test public void nullThrowable() { SingleSubject<Integer> ss = SingleSubject.create(); try { ss.onError(null); fail("No NullPointerException thrown"); } catch (NullPointerException ex) { assertEquals("onError called with null. Null values are generally not allowed in 2.x operators and sources.", ex.getMessage()); } ss.test().assertEmpty().cancel(); }
@Test public void once() { SingleSubject<Integer> ss = SingleSubject.create(); TestObserver<Integer> to = ss.test(); ss.onSuccess(1); ss.onSuccess(2); List<Throwable> errors = TestHelper.trackPluginErrors(); try { ss.onError(new IOException()); TestHelper.assertUndeliverable(errors, 0, IOException.class); } finally { RxJavaPlugins.reset(); } to.assertResult(1); }
@Test public void untilPublisherMainError() { SingleSubject<Integer> main = SingleSubject.create(); PublishProcessor<Integer> other = PublishProcessor.create(); TestObserver<Integer> to = main.takeUntil(other).test(); assertTrue("Main no observers?", main.hasObservers()); assertTrue("Other no observers?", other.hasSubscribers()); main.onError(new TestException()); assertFalse("Main has observers?", main.hasObservers()); assertFalse("Other has observers?", other.hasSubscribers()); to.assertFailure(TestException.class); }
@Test public void untilSingleMainError() { SingleSubject<Integer> main = SingleSubject.create(); SingleSubject<Integer> other = SingleSubject.create(); TestObserver<Integer> to = main.takeUntil(other).test(); assertTrue("Main no observers?", main.hasObservers()); assertTrue("Other no observers?", other.hasObservers()); main.onError(new TestException()); assertFalse("Main has observers?", main.hasObservers()); assertFalse("Other has observers?", other.hasObservers()); to.assertFailure(TestException.class); }
@Test public void untilCompletableMainError() { SingleSubject<Integer> main = SingleSubject.create(); CompletableSubject other = CompletableSubject.create(); TestObserver<Integer> to = main.takeUntil(other).test(); assertTrue("Main no observers?", main.hasObservers()); assertTrue("Other no observers?", other.hasObservers()); main.onError(new TestException()); assertFalse("Main has observers?", main.hasObservers()); assertFalse("Other has observers?", other.hasObservers()); to.assertFailure(TestException.class); }
@Test public void untilSingleOtherError() { SingleSubject<Integer> main = SingleSubject.create(); SingleSubject<Integer> other = SingleSubject.create(); TestObserver<Integer> to = main.takeUntil(other).test(); assertTrue("Main no observers?", main.hasObservers()); assertTrue("Other no observers?", other.hasObservers()); other.onError(new TestException()); assertFalse("Main has observers?", main.hasObservers()); assertFalse("Other has observers?", other.hasObservers()); to.assertFailure(TestException.class); }
@Test public void errorMain() { SingleSubject<Integer> ss = SingleSubject.create(); PublishSubject<Integer> ps = PublishSubject.create(); TestObserver<Integer> to = ss.flatMapObservable(Functions.justFunction(ps)) .test(); assertTrue(ss.hasObservers()); assertFalse(ps.hasObservers()); ss.onError(new TestException()); assertFalse(ss.hasObservers()); assertFalse(ps.hasObservers()); to.assertFailure(TestException.class); }
@Test public void switchOver() { PublishProcessor<Integer> pp = PublishProcessor.create(); final SingleSubject<Integer> ms1 = SingleSubject.create(); final SingleSubject<Integer> ms2 = SingleSubject.create(); TestSubscriber<Integer> ts = pp.switchMapSingle(new Function<Integer, SingleSource<Integer>>() { @Override public SingleSource<Integer> apply(Integer v) throws Exception { if (v == 1) { return ms1; } return ms2; } }).test(); ts.assertEmpty(); pp.onNext(1); ts.assertEmpty(); assertTrue(ms1.hasObservers()); pp.onNext(2); assertFalse(ms1.hasObservers()); assertTrue(ms2.hasObservers()); ms2.onError(new TestException()); assertFalse(pp.hasSubscribers()); ts.assertFailure(TestException.class); }
@Test public void switchOver() { PublishSubject<Integer> ps = PublishSubject.create(); final SingleSubject<Integer> ms1 = SingleSubject.create(); final SingleSubject<Integer> ms2 = SingleSubject.create(); TestObserver<Integer> to = ps.switchMapSingle(new Function<Integer, SingleSource<Integer>>() { @Override public SingleSource<Integer> apply(Integer v) throws Exception { if (v == 1) { return ms1; } return ms2; } }).test(); to.assertEmpty(); ps.onNext(1); to.assertEmpty(); assertTrue(ms1.hasObservers()); ps.onNext(2); assertFalse(ms1.hasObservers()); assertTrue(ms2.hasObservers()); ms2.onError(new TestException()); assertFalse(ps.hasObservers()); to.assertFailure(TestException.class); }
@Test public void error() { SingleSubject<Integer> ss = SingleSubject.create(); assertFalse(ss.hasValue()); assertNull(ss.getValue()); assertFalse(ss.hasThrowable()); assertNull(ss.getThrowable()); assertFalse(ss.hasObservers()); assertEquals(0, ss.observerCount()); TestObserver<Integer> to = ss.test(); to.assertEmpty(); assertTrue(ss.hasObservers()); assertEquals(1, ss.observerCount()); ss.onError(new IOException()); assertFalse(ss.hasValue()); assertNull(ss.getValue()); assertTrue(ss.hasThrowable()); assertTrue(ss.getThrowable().toString(), ss.getThrowable() instanceof IOException); assertFalse(ss.hasObservers()); assertEquals(0, ss.observerCount()); to.assertFailure(IOException.class); ss.test().assertFailure(IOException.class); assertFalse(ss.hasValue()); assertNull(ss.getValue()); assertTrue(ss.hasThrowable()); assertTrue(ss.getThrowable().toString(), ss.getThrowable() instanceof IOException); assertFalse(ss.hasObservers()); assertEquals(0, ss.observerCount()); }
@Test public void switchOverDelayError() { PublishProcessor<Integer> pp = PublishProcessor.create(); final SingleSubject<Integer> ms1 = SingleSubject.create(); final SingleSubject<Integer> ms2 = SingleSubject.create(); TestSubscriber<Integer> ts = pp.switchMapSingleDelayError(new Function<Integer, SingleSource<Integer>>() { @Override public SingleSource<Integer> apply(Integer v) throws Exception { if (v == 1) { return ms1; } return ms2; } }).test(); ts.assertEmpty(); pp.onNext(1); ts.assertEmpty(); assertTrue(ms1.hasObservers()); pp.onNext(2); assertFalse(ms1.hasObservers()); assertTrue(ms2.hasObservers()); ms2.onError(new TestException()); ts.assertEmpty(); assertTrue(pp.hasSubscribers()); pp.onComplete(); ts.assertFailure(TestException.class); }
@Test public void switchOverDelayError() { PublishSubject<Integer> ps = PublishSubject.create(); final SingleSubject<Integer> ms1 = SingleSubject.create(); final SingleSubject<Integer> ms2 = SingleSubject.create(); TestObserver<Integer> to = ps.switchMapSingleDelayError(new Function<Integer, SingleSource<Integer>>() { @Override public SingleSource<Integer> apply(Integer v) throws Exception { if (v == 1) { return ms1; } return ms2; } }).test(); to.assertEmpty(); ps.onNext(1); to.assertEmpty(); assertTrue(ms1.hasObservers()); ps.onNext(2); assertFalse(ms1.hasObservers()); assertTrue(ms2.hasObservers()); ms2.onError(new TestException()); to.assertEmpty(); assertTrue(ps.hasObservers()); ps.onComplete(); to.assertFailure(TestException.class); }
/** * Returns a Single that emits the value of the CompletionStage, its error or * NoSuchElementException if it signals null. * @param <T> the value type * @param future the source CompletionStage instance * @return the new Completable instance */ public static <T> Single<T> fromFuture(CompletionStage<T> future) { SingleSubject<T> cs = SingleSubject.create(); future.whenComplete((v, e) -> { if (e != null) { cs.onError(e); } else if (v != null) { cs.onSuccess(v); } else { cs.onError(new NoSuchElementException()); } }); return cs; }
@Test public void onErrorError() { Disposable d = SingleConsumers.subscribeAutoDispose(processor, composite, this, this); assertTrue(d.getClass().toString(), ((LambdaConsumerIntrospection)d).hasCustomOnError()); assertTrue(composite.size() > 0); assertTrue(events.toString(), events.isEmpty()); processor.onError(new IOException()); assertTrue(events.toString(), events.get(0) instanceof IOException); assertEquals(0, composite.size()); }
@Test public void onErrorCrash() { List<Throwable> errors = TestHelper.trackPluginErrors(); try { SingleConsumers.subscribeAutoDispose(processor, composite, this, new Consumer<Throwable>() { @Override public void accept(Throwable t) throws Exception { throw new IOException(t); } }); processor.onError(new IllegalArgumentException()); assertTrue(events.toString(), events.isEmpty()); TestHelper.assertError(errors, 0, CompositeException.class); List<Throwable> inners = TestHelper.compositeList(errors.get(0)); TestHelper.assertError(inners, 0, IllegalArgumentException.class); TestHelper.assertError(inners, 1, IOException.class); } finally { RxJavaPlugins.reset(); } }