Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

EmptyObserver and TestObserver #1742

Merged
merged 3 commits into from
Oct 10, 2014
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,7 @@
import rx.Observer;
import rx.Subscriber;
import rx.functions.Action0;
import rx.observers.EmptyObserver;
import rx.observers.Subscribers;
import rx.observers.Observers;
import rx.subjects.Subject;
import rx.subscriptions.Subscriptions;

Expand Down Expand Up @@ -51,9 +50,6 @@
*/
public class BufferUntilSubscriber<T> extends Subject<T, T> {

@SuppressWarnings("rawtypes")
private final static Observer EMPTY_OBSERVER = new EmptyObserver();

/**
* @warn create() undescribed
* @return
Expand Down Expand Up @@ -96,7 +92,7 @@ public void call(final Subscriber<? super T> s) {
s.add(Subscriptions.create(new Action0() {
@Override
public void call() {
state.observerRef = EMPTY_OBSERVER;
state.observerRef = Observers.empty();
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should not use Observers.empty() here. Once it's unsubscribed, onError should be ignored, but Observers.empty() will throw an Exception for onError. Why not put EmptyObserver into an internal package?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We could, or just create a static inner class here as it's rare this is needed.

Thanks for catching this. I'll fix this shortly.

}
}));
boolean win = false;
Expand Down
41 changes: 0 additions & 41 deletions src/main/java/rx/observers/EmptyObserver.java

This file was deleted.

8 changes: 3 additions & 5 deletions src/main/java/rx/observers/Observers.java
Original file line number Diff line number Diff line change
Expand Up @@ -46,9 +46,7 @@ public final void onNext(Object args) {

/**
* Returns an inert {@link Observer} that does nothing in response to the emissions or notifications from
* any {@code Observable} it subscribes to. This is different, however, from an {@link EmptyObserver}, in
* that it will throw an exception if its {@link Observer#onError onError} method is called (whereas
* {@code EmptyObserver} will swallow the error in such a case).
* any {@code Observable} it subscribes to but will throw an exception if its {@link Observer#onError onError} method is called.
*
* @return an inert {@code Observer}
*/
Expand All @@ -59,8 +57,8 @@ public static <T> Observer<T> empty() {

/**
* Creates an {@link Observer} that receives the emissions of any {@code Observable} it subscribes to via
* {@link Observer#onNext onNext} but ignores {@link Observer#onError onError} and
* {@link Observer#onCompleted onCompleted} notifications.
* {@link Observer#onNext onNext} but ignores {@link Observer#onCompleted onCompleted} notifications.
* It will throws an {@link OnErrorNotImplementedException} if {@link Observer#onError onError} is invoked.
*
* @param onNext
* a function that handles each item emitted by an {@code Observable}
Expand Down
22 changes: 21 additions & 1 deletion src/main/java/rx/observers/TestObserver.java
Original file line number Diff line number Diff line change
Expand Up @@ -36,8 +36,9 @@ public TestObserver(Observer<T> delegate) {
this.delegate = delegate;
}

@SuppressWarnings("unchecked")
public TestObserver() {
this.delegate = Observers.empty();
this.delegate = (Observer<T>) INERT;
}

@Override
Expand Down Expand Up @@ -153,4 +154,23 @@ public void assertTerminalEvent() {
}
}

// do nothing ... including swallowing errors
private static Observer<Object> INERT = new Observer<Object>() {

@Override
public void onCompleted() {

}

@Override
public void onError(Throwable e) {

}

@Override
public void onNext(Object t) {

}

};
}
5 changes: 5 additions & 0 deletions src/test/java/rx/observers/TestObserverTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -119,5 +119,10 @@ public void testWrappingMockWhenUnsubscribeInvolved() {
inOrder.verify(mockObserver, times(1)).onCompleted();
inOrder.verifyNoMoreInteractions();
}

@Test
public void testErrorSwallowed() {
Observable.error(new RuntimeException()).subscribe(new TestObserver<Object>());
}

}