@Override public void onError(Throwable e) { long p = produced; if (p != 0L) { arbiter.produced(p); } Observable<T> o; try { o = onError.call(e); } catch (Throwable ex) { Exceptions.throwIfFatal(ex); actual.onError(new CompositeException(e, ex)); return; } if (o == null) { actual.onError( new NullPointerException("The onError function returned a null Observable.")); } else { o.unsafeSubscribe(new ResumeSubscriber<T>(actual, arbiter)); } }
@Test public void testAssert() { Observable<Integer> oi = Observable.from(Arrays.asList(1, 2)); TestSubscriber<Integer> o = new TestSubscriber<Integer>(); oi.subscribe(o); o.assertReceivedOnNext(Arrays.asList(1, 2)); assertEquals(2, o.getOnNextEvents().size()); o.assertTerminalEvent(); }
@Test public void testWrappingMock() { Observable<Integer> oi = Observable.from(Arrays.asList(1, 2)); @SuppressWarnings("unchecked") Observer<Integer> mockObserver = mock(Observer.class); oi.subscribe(new TestSubscriber<Integer>(mockObserver)); InOrder inOrder = inOrder(mockObserver); inOrder.verify(mockObserver, times(1)).onNext(1); inOrder.verify(mockObserver, times(1)).onNext(2); inOrder.verify(mockObserver, times(1)).onCompleted(); inOrder.verifyNoMoreInteractions(); }
@Test public void testAssertNotMatchValue() { Observable<Integer> oi = Observable.from(Arrays.asList(1, 2)); TestSubscriber<Integer> o = new TestSubscriber<Integer>(); oi.subscribe(o); thrown.expect(AssertionError.class); thrown.expectMessage("Value at index: 1 expected to be [3] (Integer) but was: [2] (Integer)"); o.assertReceivedOnNext(Arrays.asList(1, 3)); assertEquals(2, o.getOnNextEvents().size()); o.assertTerminalEvent(); }
@Test public void testAssertNotMatchCount() { Observable<Integer> oi = Observable.from(Arrays.asList(1, 2)); TestSubscriber<Integer> o = new TestSubscriber<Integer>(); oi.subscribe(o); thrown.expect(AssertionError.class); thrown.expectMessage("Number of items does not match. Provided: 1 Actual: 2"); o.assertReceivedOnNext(Arrays.asList(1)); assertEquals(2, o.getOnNextEvents().size()); o.assertTerminalEvent(); }
@Test public void testAwaitTerminalEventWithDuration() { TestSubscriber<Object> ts = new TestSubscriber<Object>(); Observable.just(1).subscribe(ts); ts.awaitTerminalEvent(1, TimeUnit.SECONDS); ts.assertTerminalEvent(); }
@Test public void testAssertError() { RuntimeException e = new RuntimeException("Oops"); TestSubscriber<Object> subscriber = new TestSubscriber<Object>(); Observable.error(e).subscribe(subscriber); subscriber.assertError(e); }
@Override public void onCompleted() { long p = produced; if (p != 0L) { arbiter.produced(p); } onCompleted.unsafeSubscribe(new ResumeSubscriber<T>(actual, arbiter)); }
@Override public void call(final Subscriber<? super T> subscriber) { try { // create the resource final Resource resource = resourceFactory.call(); // create an action/subscription that disposes only once final DisposeAction<Resource> disposeOnceOnly = new DisposeAction<Resource>(dispose, resource); // dispose on unsubscription subscriber.add(disposeOnceOnly); // create the observable final Observable<? extends T> source = observableFactory // create the observable .call(resource); final Observable<? extends T> observable; // supplement with on termination disposal if requested if (disposeEagerly) observable = source // dispose on completion or error .doOnTerminate(disposeOnceOnly); else observable = source; try { // start observable.unsafeSubscribe(Subscribers.wrap(subscriber)); } catch (Throwable e) { Throwable disposeError = disposeEagerlyIfRequested(disposeOnceOnly); Exceptions.throwIfFatal(e); Exceptions.throwIfFatal(disposeError); if (disposeError != null) subscriber.onError(new CompositeException(Arrays.asList(e, disposeError))); else // propagate error subscriber.onError(e); } } catch (Throwable e) { // then propagate error Exceptions.throwOrReport(e, subscriber); } }
@Override public Observable<T> call(final Observable<T> o) { return Observable.create( new OnSubscribe<T>() { @Override public void call(Subscriber<? super T> t) { OnTerminateResumeSubscriber<T> parent = new OnTerminateResumeSubscriber<T>(t, onError, onCompleted); t.add(parent); t.setProducer(parent.arbiter); o.unsafeSubscribe(parent); } }); }
@Test public void testAwaitTerminalEventWithDurationAndUnsubscribeOnTimeout() { TestSubscriber<Object> ts = new TestSubscriber<Object>(); final AtomicBoolean unsub = new AtomicBoolean(false); Observable.just(1) // .doOnUnsubscribe( new Action0() { @Override public void call() { unsub.set(true); } }) // .delay(1000, TimeUnit.MILLISECONDS) .subscribe(ts); ts.awaitTerminalEventAndUnsubscribeOnTimeout(100, TimeUnit.MILLISECONDS); assertTrue(unsub.get()); }