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

Missing fixes from the subject rewrite #659

Merged
merged 1 commit into from
Dec 23, 2013
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
13 changes: 7 additions & 6 deletions rxjava-core/src/main/java/rx/subjects/BehaviorSubject.java
Original file line number Diff line number Diff line change
Expand Up @@ -169,12 +169,13 @@ public void call(Collection<SubjectObserver<? super T>> observers) {

@Override
public void onNext(T v) {
/**
* Store the latest value but do not send it. It only gets sent when 'onCompleted' occurs.
*/
lastNotification.set(new Notification<T>(v));
for (Observer<? super T> o : subscriptionManager.rawSnapshot()) {
o.onNext(v);
// do not overwrite a terminal notification
// so new subscribers can get them
if (lastNotification.get().isOnNext()) {
lastNotification.set(new Notification<T>(v));
for (Observer<? super T> o : subscriptionManager.rawSnapshot()) {
o.onNext(v);
}
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,7 @@ public Subscription onSubscribe(Observer<? super T> actualObserver) {
try {
current.terminationLatch.await();
} catch (InterruptedException e) {
Thread.currentThread().interrupt();
throw new RuntimeException("Interrupted waiting for termination.", e);
}
break;
Expand Down
50 changes: 50 additions & 0 deletions rxjava-core/src/test/java/rx/subjects/BehaviorSubjectTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -185,5 +185,55 @@ public void testCompletedAfterErrorIsNotSent() {
verify(aObserver, never()).onNext("two");
verify(aObserver, never()).onCompleted();
}
@Test
public void testCompletedAfterErrorIsNotSent2() {
BehaviorSubject<String> subject = BehaviorSubject.create("default");

@SuppressWarnings("unchecked")
Observer<String> aObserver = mock(Observer.class);
subject.subscribe(aObserver);

subject.onNext("one");
subject.onError(testException);
subject.onNext("two");
subject.onCompleted();

verify(aObserver, times(1)).onNext("default");
verify(aObserver, times(1)).onNext("one");
verify(aObserver, times(1)).onError(testException);
verify(aObserver, never()).onNext("two");
verify(aObserver, never()).onCompleted();

Observer<Object> o2 = mock(Observer.class);
subject.subscribe(o2);
verify(o2, times(1)).onError(testException);
verify(o2, never()).onNext(any());
verify(o2, never()).onCompleted();
}

@Test
public void testCompletedAfterErrorIsNotSent3() {
BehaviorSubject<String> subject = BehaviorSubject.create("default");

@SuppressWarnings("unchecked")
Observer<String> aObserver = mock(Observer.class);
subject.subscribe(aObserver);

subject.onNext("one");
subject.onCompleted();
subject.onNext("two");
subject.onCompleted();

verify(aObserver, times(1)).onNext("default");
verify(aObserver, times(1)).onNext("one");
verify(aObserver, times(1)).onCompleted();
verify(aObserver, never()).onError(any(Throwable.class));
verify(aObserver, never()).onNext("two");

Observer<Object> o2 = mock(Observer.class);
subject.subscribe(o2);
verify(o2, times(1)).onCompleted();
verify(o2, never()).onNext(any());
verify(aObserver, never()).onError(any(Throwable.class));
}
}