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

Created the initial implementation of BehaviorSubject #256

Merged
merged 2 commits into from
May 7, 2013
Merged
Changes from 1 commit
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
257 changes: 257 additions & 0 deletions rxjava-core/src/main/java/rx/subjects/BehaviorSubject.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,257 @@
/**
* Copyright 2013 Netflix, Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package rx.subjects;

import static org.mockito.Matchers.any;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.times;
import static org.mockito.Mockito.verify;

import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.atomic.AtomicReference;

import org.junit.Test;
import org.mockito.Mockito;

import rx.Observer;
import rx.Subscription;
import rx.util.AtomicObservableSubscription;
import rx.util.SynchronizedObserver;
import rx.util.functions.Action1;
import rx.util.functions.Func0;
import rx.util.functions.Func1;

/**
* Subject that publishes the previous and all subsequent events to each {@link Observer} that subscribes.
Copy link
Member

Choose a reason for hiding this comment

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

Perhaps replacing "previous" with "last" is clearer that this is not all previous events but only the most recent (last) event?

* <p>
* Example usage:
* <p>
* <pre> {@code

// observer will receive all events.
BehaviorSubject<Object> subject = BehaviorSubject.createWithDefaultValue("default");
subject.subscribe(observer);
subject.onNext("one");
subject.onNext("two");
subject.onNext("three");

// observer will receive the "one", "two" and "three" events.
BehaviorSubject<Object> subject = BehaviorSubject.createWithDefaultValue("default");
subject.onNext("one");
subject.subscribe(observer);
subject.onNext("two");
subject.onNext("three");

} </pre>
*
* @param <T>
*/
public class BehaviorSubject<T> extends Subject<T, T> {

public static <T> BehaviorSubject<T> createWithDefaultValue(T defaultValue) {
final ConcurrentHashMap<Subscription, Observer<T>> observers = new ConcurrentHashMap<Subscription, Observer<T>>();

final AtomicReference<T> currentValue = new AtomicReference<T>(defaultValue);

Func1<Observer<T>, Subscription> onSubscribe = new Func1<Observer<T>, Subscription>() {
@Override
public Subscription call(Observer<T> observer) {
final AtomicObservableSubscription subscription = new AtomicObservableSubscription();

subscription.wrap(new Subscription() {
@Override
public void unsubscribe() {
// on unsubscribe remove it from the map of outbound observers to notify
observers.remove(subscription);
}
});

SynchronizedObserver<T> synchronizedObserver = new SynchronizedObserver<T>(observer, subscription);
Copy link
Member

Choose a reason for hiding this comment

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

I don't think we need a SynchronizedObserver here. There is nothing about this operator that is injecting concurrency in so we don't need to synchronize onNext calls.

By default implementations assume onNext/onCompleted/onError calls are not concurrent and trust that the source abides by the Rx contract.

Operators like merge and zip which are inherently concurrent must synchronize but most should not need to.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Hmmm, I see your point. But in that case we might need to revisit AsyncSubject and PublishSubject as well, since I based AsyncSubject and BehaviorSubject on PublishSubject.

Copy link
Member

Choose a reason for hiding this comment

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

Yes, if they are using SynchronizedObserver I don't believe they should be. It is up to the producer to comply with the contract not the Observer to protect itself (though much of RxJava code is defensive and uses concurrent data structures we don't want that to extend to the more expensive defense of serializing everything).

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Alright I'll fix this for the subjects. But I will be abroad for the rest of this week so it might take a while.

Copy link
Member

Choose a reason for hiding this comment

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

I can do that then when I merge this. Thank you @michaeldejong

synchronizedObserver.onNext(currentValue.get());

// on subscribe add it to the map of outbound observers to notify
observers.put(subscription, synchronizedObserver);
return subscription;
}
};

return new BehaviorSubject<T>(currentValue, onSubscribe, observers);
}

private final ConcurrentHashMap<Subscription, Observer<T>> observers;
private final AtomicReference<T> currentValue;

protected BehaviorSubject(AtomicReference<T> currentValue, Func1<Observer<T>, Subscription> onSubscribe, ConcurrentHashMap<Subscription, Observer<T>> observers) {
super(onSubscribe);
this.currentValue = currentValue;
this.observers = observers;
}

@Override
public void onCompleted() {
for (Observer<T> observer : observers.values()) {
observer.onCompleted();
}
}

@Override
public void onError(Exception e) {
for (Observer<T> observer : observers.values()) {
observer.onError(e);
}
}

@Override
public void onNext(T args) {
currentValue.set(args);
for (Observer<T> observer : observers.values()) {
observer.onNext(args);
}
}

public static class UnitTest {

private final Exception testException = new Exception();

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

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

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

assertReceivedAllEvents(aObserver);
}

private void assertReceivedAllEvents(Observer<String> aObserver) {
verify(aObserver, times(1)).onNext("default");
verify(aObserver, times(1)).onNext("one");
verify(aObserver, times(1)).onNext("two");
verify(aObserver, times(1)).onNext("three");
verify(aObserver, Mockito.never()).onError(testException);
verify(aObserver, Mockito.never()).onCompleted();
}

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

subject.onNext("one");

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

subject.onNext("two");
subject.onNext("three");

assertDidNotReceiveTheDefaultValue(aObserver);
}

private void assertDidNotReceiveTheDefaultValue(Observer<String> aObserver) {
verify(aObserver, Mockito.never()).onNext("default");
verify(aObserver, times(1)).onNext("one");
verify(aObserver, times(1)).onNext("two");
verify(aObserver, times(1)).onNext("three");
verify(aObserver, Mockito.never()).onError(testException);
verify(aObserver, Mockito.never()).onCompleted();
}

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

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

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

assertCompletedObserver(aObserver);
}

private void assertCompletedObserver(Observer<String> aObserver)
{
verify(aObserver, times(1)).onNext("default");
verify(aObserver, times(1)).onNext("one");
verify(aObserver, Mockito.never()).onError(any(Exception.class));
verify(aObserver, times(1)).onCompleted();
}

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

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

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

assertErrorObserver(aObserver);
}

private void assertErrorObserver(Observer<String> aObserver)
{
verify(aObserver, times(1)).onNext("default");
verify(aObserver, times(1)).onNext("one");
verify(aObserver, times(1)).onError(testException);
}

@Test
public void testUnsubscribe()
{
UnsubscribeTester.test(new Func0<BehaviorSubject<String>>()
{
@Override
public BehaviorSubject<String> call()
{
return BehaviorSubject.createWithDefaultValue("default");
}
}, new Action1<BehaviorSubject<String>>()
{
@Override
public void call(BehaviorSubject<String> DefaultSubject)
{
DefaultSubject.onCompleted();
}
}, new Action1<BehaviorSubject<String>>()
{
@Override
public void call(BehaviorSubject<String> DefaultSubject)
{
DefaultSubject.onError(new Exception());
}
}, new Action1<BehaviorSubject<String>>()
{
@Override
public void call(BehaviorSubject<String> DefaultSubject)
{
DefaultSubject.onNext("one");
}
});
}
}
}