@Test public void testScanIntegersWithoutInitialValue() { Subscriber<Integer> observer = TestHelper.mockSubscriber(); Observable<Integer> observable = Observable.just(1, 2, 3); Observable<Integer> m = observable.scan( new BiFunction<Integer, Integer, Integer>() { @Override public Integer apply(Integer t1, Integer t2) { return t1 + t2; } }); m.subscribe(observer); verify(observer, never()).onError(any(Throwable.class)); verify(observer, never()).onNext(0); verify(observer, times(1)).onNext(1); verify(observer, times(1)).onNext(3); verify(observer, times(1)).onNext(6); verify(observer, times(3)).onNext(anyInt()); verify(observer, times(1)).onComplete(); verify(observer, never()).onError(any(Throwable.class)); }
@Test public void testScanIntegersWithInitialValue() { Subscriber<String> observer = TestHelper.mockSubscriber(); Observable<Integer> observable = Observable.just(1, 2, 3); Observable<String> m = observable.scan( "", new BiFunction<String, Integer, String>() { @Override public String apply(String s, Integer n) { return s + n.toString(); } }); m.subscribe(observer); verify(observer, never()).onError(any(Throwable.class)); verify(observer, times(1)).onNext(""); verify(observer, times(1)).onNext("1"); verify(observer, times(1)).onNext("12"); verify(observer, times(1)).onNext("123"); verify(observer, times(4)).onNext(anyString()); verify(observer, times(1)).onComplete(); verify(observer, never()).onError(any(Throwable.class)); }
@Test public void testErrorInParentObservableDelayed() throws Exception { for (int i = 0; i < 50; i++) { final TestASynchronous1sDelayedObservable o1 = new TestASynchronous1sDelayedObservable(); final TestASynchronous1sDelayedObservable o2 = new TestASynchronous1sDelayedObservable(); Observable<Observable<String>> parentObservable = Observable.create( new Publisher<Observable<String>>() { @Override public void subscribe(Subscriber<? super Observable<String>> op) { op.onSubscribe(EmptySubscription.INSTANCE); op.onNext(Observable.create(o1)); op.onNext(Observable.create(o2)); op.onError(new NullPointerException("throwing exception in parent")); } }); Subscriber<String> stringObserver = TestHelper.mockSubscriber(); TestSubscriber<String> ts = new TestSubscriber<>(stringObserver); Observable<String> m = Observable.mergeDelayError(parentObservable); m.subscribe(ts); System.out.println("testErrorInParentObservableDelayed | " + i); ts.awaitTerminalEvent(2000, TimeUnit.MILLISECONDS); ts.assertTerminated(); verify(stringObserver, times(2)).onNext("hello"); verify(stringObserver, times(1)).onError(any(NullPointerException.class)); verify(stringObserver, never()).onComplete(); } }
@Test @Ignore("Subscribers should not throw") public void testMergeSourceWhichDoesntPropagateExceptionBack() { Observable<Integer> source = Observable.create( new Publisher<Integer>() { @Override public void subscribe(Subscriber<? super Integer> t1) { t1.onSubscribe(EmptySubscription.INSTANCE); try { t1.onNext(0); } catch (Throwable swallow) { } t1.onNext(1); t1.onComplete(); } }); Observable<Integer> result = Observable.mergeDelayError(source, Observable.just(2)); final Subscriber<Integer> o = TestHelper.mockSubscriber(); InOrder inOrder = inOrder(o); result.unsafeSubscribe( new Observer<Integer>() { int calls; @Override public void onNext(Integer t) { if (calls++ == 0) { throw new TestException(); } o.onNext(t); } @Override public void onError(Throwable e) { o.onError(e); } @Override public void onComplete() { o.onComplete(); } }); /* * If the child onNext throws, why would we keep accepting values from * other sources? */ inOrder.verify(o).onNext(2); inOrder.verify(o, never()).onNext(0); inOrder.verify(o, never()).onNext(1); inOrder.verify(o, never()).onNext(anyInt()); inOrder.verify(o).onError(any(TestException.class)); verify(o, never()).onComplete(); }
@Before public void before() { observer = TestHelper.mockSubscriber(); }