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.from(empty) to emit onComplete even when 0 requested #2899

Merged
merged 1 commit into from
Apr 21, 2015
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 @@ -44,7 +44,10 @@ public OnSubscribeFromIterable(Iterable<? extends T> iterable) {
@Override
public void call(final Subscriber<? super T> o) {
final Iterator<? extends T> it = is.iterator();
o.setProducer(new IterableProducer<T>(o, it));
if (!it.hasNext() && !o.isUnsubscribed())
o.onCompleted();
else
o.setProducer(new IterableProducer<T>(o, it));
}

private static final class IterableProducer<T> implements Producer {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@
import java.util.Iterator;
import java.util.concurrent.CountDownLatch;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.atomic.AtomicBoolean;

import org.junit.Test;
import org.mockito.Mockito;
Expand Down Expand Up @@ -74,12 +75,12 @@ public Iterator<String> iterator() {

@Override
public boolean hasNext() {
return i++ < 3;
return i < 3;
}

@Override
public String next() {
return String.valueOf(i);
return String.valueOf(++i);
}

@Override
Expand Down Expand Up @@ -193,5 +194,31 @@ public void onNext(Integer t) {
assertTrue(latch.await(10, TimeUnit.SECONDS));
}

@Test
public void testFromEmptyIterableWhenZeroRequestedShouldStillEmitOnCompletedEagerly() {
final AtomicBoolean completed = new AtomicBoolean(false);
Observable.from(Collections.emptyList()).subscribe(new Subscriber<Object>() {

@Override
public void onStart() {
request(0);
}

@Override
public void onCompleted() {
completed.set(true);
}

@Override
public void onError(Throwable e) {

}

@Override
public void onNext(Object t) {

}});
assertTrue(completed.get());
}

}