Skip to content

1.x: negative argument check for skip's count and merge's maxConcurrent #3677

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
Feb 8, 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: 3 additions & 0 deletions src/main/java/rx/internal/operators/OperatorMerge.java
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,9 @@ public static <T> OperatorMerge<T> instance(boolean delayErrors) {
* @return
*/
public static <T> OperatorMerge<T> instance(boolean delayErrors, int maxConcurrent) {
if (maxConcurrent <= 0) {
throw new IllegalArgumentException("maxConcurrent > 0 required but it was " + maxConcurrent);
}
if (maxConcurrent == Integer.MAX_VALUE) {
return instance(delayErrors);
}
Expand Down
3 changes: 3 additions & 0 deletions src/main/java/rx/internal/operators/OperatorSkip.java
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,9 @@ public final class OperatorSkip<T> implements Observable.Operator<T, T> {
final int toSkip;

public OperatorSkip(int n) {
if (n < 0) {
throw new IllegalArgumentException("n >= 0 required but it was " + n);
}
this.toSkip = n;
}

Expand Down
20 changes: 20 additions & 0 deletions src/test/java/rx/internal/operators/OperatorMergeTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -1333,4 +1333,24 @@ public void testConcurrencyLimit() {
ts.assertValue(0);
ts.assertCompleted();
}

@Test
public void negativeMaxConcurrent() {
try {
Observable.merge(Arrays.asList(Observable.just(1), Observable.just(2)), -1);
fail("Expected IllegalArgumentException");
} catch (IllegalArgumentException e) {
assertEquals("maxConcurrent > 0 required but it was -1", e.getMessage());
}
}

@Test
public void zeroMaxConcurrent() {
try {
Observable.merge(Arrays.asList(Observable.just(1), Observable.just(2)), 0);
fail("Expected IllegalArgumentException");
} catch (IllegalArgumentException e) {
assertEquals("maxConcurrent > 0 required but it was 0", e.getMessage());
}
}
}
18 changes: 7 additions & 11 deletions src/test/java/rx/internal/operators/OperatorSkipTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
package rx.internal.operators;

import static org.junit.Assert.assertEquals;
import static org.junit.Assert.fail;
import static org.mockito.Matchers.any;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.never;
Expand All @@ -37,17 +38,12 @@ public class OperatorSkipTest {

@Test
public void testSkipNegativeElements() {

Observable<String> skip = Observable.just("one", "two", "three").lift(new OperatorSkip<String>(-99));

@SuppressWarnings("unchecked")
Observer<String> observer = mock(Observer.class);
skip.subscribe(observer);
verify(observer, times(1)).onNext("one");
verify(observer, times(1)).onNext("two");
verify(observer, times(1)).onNext("three");
verify(observer, never()).onError(any(Throwable.class));
verify(observer, times(1)).onCompleted();
try {
Observable.just("one", "two", "three").skip(-99);
fail("Expected IllegalArgumentException");
} catch (IllegalArgumentException e) {
assertEquals("n >= 0 required but it was -99", e.getMessage());
}
}

@Test
Expand Down