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

Observable.toList breaks with multiple subscribers #206

Merged
merged 2 commits into from
Mar 27, 2013
Merged
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 @@ -40,7 +40,6 @@ public static <T> Func1<Observer<List<T>>, Subscription> toObservableList(Observ
private static class ToObservableList<T> implements Func1<Observer<List<T>>, Subscription> {

private final Observable<T> that;
final ConcurrentLinkedQueue<T> list = new ConcurrentLinkedQueue<T>();

public ToObservableList(Observable<T> that) {
this.that = that;
Expand All @@ -49,6 +48,7 @@ public ToObservableList(Observable<T> that) {
public Subscription call(final Observer<List<T>> observer) {

return that.subscribe(new Observer<T>() {
final ConcurrentLinkedQueue<T> list = new ConcurrentLinkedQueue<T>();
public void onNext(T value) {
// onNext can be concurrently executed so list must be thread-safe
list.add(value);
Expand Down Expand Up @@ -94,5 +94,28 @@ public void testList() {
verify(aObserver, Mockito.never()).onError(any(Exception.class));
verify(aObserver, times(1)).onCompleted();
}

@Test
public void testListMultipleObservers() {
Observable<String> w = Observable.toObservable("one", "two", "three");
Observable<List<String>> observable = Observable.create(toObservableList(w));

@SuppressWarnings("unchecked")
Observer<List<String>> o1 = mock(Observer.class);
observable.subscribe(o1);

Observer<List<String>> o2 = mock(Observer.class);
observable.subscribe(o2);

List<String> expected = Arrays.asList("one", "two", "three");

verify(o1, times(1)).onNext(expected);
verify(o1, Mockito.never()).onError(any(Exception.class));
verify(o1, times(1)).onCompleted();

verify(o2, times(1)).onNext(expected);
verify(o2, Mockito.never()).onError(any(Exception.class));
verify(o2, times(1)).onCompleted();
}
}
}