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

Fix the bug that BlockingObservable.singleOrDefault doesn't call unsubscribe #1260

Merged
merged 1 commit into from
May 27, 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
13 changes: 2 additions & 11 deletions rxjava-core/src/main/java/rx/observables/BlockingObservable.java
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
import rx.Subscriber;
import rx.functions.Action1;
import rx.functions.Func1;
import rx.functions.Functions;
import rx.operators.BlockingOperatorLatest;
import rx.operators.BlockingOperatorMostRecent;
import rx.operators.BlockingOperatorNext;
Expand Down Expand Up @@ -381,17 +382,7 @@ public T single(Func1<? super T, Boolean> predicate) {
* @see <a href="http://msdn.microsoft.com/en-us/library/system.reactive.linq.observable.singleordefault.aspx">MSDN: Observable.SingleOrDefault</a>
*/
public T singleOrDefault(T defaultValue) {
Iterator<? extends T> it = this.toIterable().iterator();

if (!it.hasNext()) {
return defaultValue;
}

T result = it.next();
if (it.hasNext()) {
throw new IllegalArgumentException("Sequence contains too many elements");
}
return result;
return from(o.map(Functions.<T>identity()).singleOrDefault(defaultValue)).single();
Copy link
Member

Choose a reason for hiding this comment

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

Why do you need to map with the identity function here?

Copy link
Member Author

Choose a reason for hiding this comment

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

Because o is Observable<? extends T> and I cannot call singleOrDefault(T) on it directly.

Copy link
Contributor

Choose a reason for hiding this comment

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

Does that mean that XXXOrDefault have to be covariant as well :-(

public T singleOrDefault(T defaultValue)

Copy link
Member Author

Choose a reason for hiding this comment

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

Observable<T> singleOrDefault(T defaultValue) also need to

However, I cannot write <U super T> Observable<U> singleOrDefault(U defaultValue) in Java :(

Copy link
Member

Choose a reason for hiding this comment

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

This covariance stuff is insane.

Copy link
Contributor

Choose a reason for hiding this comment

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

Copy link
Member

Choose a reason for hiding this comment

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

:-)

}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,10 +16,13 @@
package rx.observables;

import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertTrue;
import static org.junit.Assert.fail;

import java.util.Iterator;
import java.util.NoSuchElementException;
import java.util.concurrent.CountDownLatch;
import java.util.concurrent.TimeUnit;

import org.junit.Assert;
import org.junit.Before;
Expand All @@ -28,10 +31,14 @@
import org.mockito.MockitoAnnotations;

import rx.Observable;
import rx.Observable.OnSubscribe;
import rx.Subscriber;
import rx.exceptions.TestException;
import rx.functions.Action0;
import rx.functions.Action1;
import rx.functions.Func1;
import rx.schedulers.Schedulers;
import rx.subscriptions.Subscriptions;

public class BlockingObservableTest {

Expand Down Expand Up @@ -377,4 +384,30 @@ public Boolean call(String args) {
});
assertEquals("default", first);
}

@Test
public void testSingleOrDefaultUnsubscribe() throws InterruptedException {
final CountDownLatch unsubscribe = new CountDownLatch(1);
Observable<Integer> o = Observable.create(new OnSubscribe<Integer>() {
@Override
public void call(Subscriber<? super Integer> subscriber) {
subscriber.add(Subscriptions.create(new Action0() {
@Override
public void call() {
unsubscribe.countDown();
}
}));
subscriber.onNext(1);
subscriber.onNext(2);
// Don't call `onCompleted` to emulate an infinite stream
}
}).subscribeOn(Schedulers.newThread());
try {
o.toBlocking().singleOrDefault(-1);
fail("Expected IllegalArgumentException because there are 2 elements");
} catch (IllegalArgumentException e) {
// Expected
}
assertTrue("Timeout means `unsubscribe` is not called", unsubscribe.await(30, TimeUnit.SECONDS));
}
}