Skip to content

1.x: Prevent Single.zip() of zero Singles #3789

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

Merged
merged 1 commit into from
Mar 23, 2016
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
3 changes: 2 additions & 1 deletion src/main/java/rx/Single.java
Original file line number Diff line number Diff line change
Expand Up @@ -1235,7 +1235,8 @@ public R call(Object... args) {
* </dl>
*
* @param singles
* an Iterable of source Singles
* an Iterable of source Singles. Should not be empty because {@link Single} either emits result or error.
* {@link java.util.NoSuchElementException} will be emit as error if Iterable will be empty.
* @param zipFunction
* a function that, when applied to an item emitted by each of the source Singles, results in
* an item that will be emitted by the resulting Single
Expand Down
6 changes: 6 additions & 0 deletions src/main/java/rx/internal/operators/SingleOperatorZip.java
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
import rx.plugins.RxJavaPlugins;
import rx.subscriptions.CompositeSubscription;

import java.util.NoSuchElementException;
import java.util.concurrent.atomic.AtomicBoolean;
import java.util.concurrent.atomic.AtomicInteger;

Expand All @@ -16,6 +17,11 @@ public static <T, R> Single<R> zip(final Single<? extends T>[] singles, final Fu
return Single.create(new Single.OnSubscribe<R>() {
@Override
public void call(final SingleSubscriber<? super R> subscriber) {
if (singles.length == 0) {
subscriber.onError(new NoSuchElementException("Can't zip 0 Singles."));
return;
}

final AtomicInteger wip = new AtomicInteger(singles.length);
final AtomicBoolean once = new AtomicBoolean();
final Object[] values = new Object[singles.length];
Expand Down
20 changes: 20 additions & 0 deletions src/test/java/rx/SingleTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -340,6 +340,26 @@ public String call(Object... args) {
ts.assertCompleted();
}

@Test
public void zipEmptyIterableShouldThrow() {
TestSubscriber<Object> testSubscriber = new TestSubscriber<Object>();
Iterable<Single<Object>> singles = Collections.emptyList();

Single
.zip(singles, new FuncN<Object>() {
@Override
public Object call(Object... args) {
throw new IllegalStateException("Should not be called");
}
})
.subscribe(testSubscriber);

testSubscriber.assertNoValues();
testSubscriber.assertNotCompleted();
testSubscriber.assertError(NoSuchElementException.class);
assertEquals("Can't zip 0 Singles.", testSubscriber.getOnErrorEvents().get(0).getMessage());
}

@Test
public void testZipWith() {
TestSubscriber<String> ts = new TestSubscriber<String>();
Expand Down