-
Notifications
You must be signed in to change notification settings - Fork 7.6k
OperatorReduce - prevent multiple terminal events #4246
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
akarnokd
merged 1 commit into
ReactiveX:1.x
from
davidmoten:operator-reduce-prevent-multiple-terminal-events
Jul 28, 2016
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
277 changes: 277 additions & 0 deletions
277
src/test/java/rx/internal/operators/OnSubscribeReduceTest.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,277 @@ | ||
/** | ||
* Copyright 2014 Netflix, Inc. | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
|
||
package rx.internal.operators; | ||
|
||
import static org.junit.Assert.assertEquals; | ||
import static org.mockito.Matchers.any; | ||
import static org.mockito.Mockito.never; | ||
import static org.mockito.Mockito.times; | ||
import static org.mockito.Mockito.verify; | ||
|
||
import java.util.Arrays; | ||
import java.util.List; | ||
import java.util.NoSuchElementException; | ||
import java.util.concurrent.CopyOnWriteArrayList; | ||
|
||
import org.junit.Before; | ||
import org.junit.Test; | ||
import org.mockito.Mock; | ||
import org.mockito.MockitoAnnotations; | ||
|
||
import rx.*; | ||
import rx.Observable.OnSubscribe; | ||
import rx.Observer; | ||
import rx.exceptions.TestException; | ||
import rx.functions.Action1; | ||
import rx.functions.Func1; | ||
import rx.functions.Func2; | ||
import rx.internal.util.UtilityFunctions; | ||
import rx.observers.TestSubscriber; | ||
import rx.plugins.RxJavaHooks; | ||
|
||
public class OnSubscribeReduceTest { | ||
@Mock | ||
Observer<Object> observer; | ||
|
||
@Before | ||
public void before() { | ||
MockitoAnnotations.initMocks(this); | ||
} | ||
|
||
Func2<Integer, Integer, Integer> sum = new Func2<Integer, Integer, Integer>() { | ||
@Override | ||
public Integer call(Integer t1, Integer t2) { | ||
return t1 + t2; | ||
} | ||
}; | ||
|
||
@Test | ||
public void testAggregateAsIntSum() { | ||
|
||
Observable<Integer> result = Observable.just(1, 2, 3, 4, 5).reduce(0, sum).map(UtilityFunctions.<Integer> identity()); | ||
|
||
result.subscribe(observer); | ||
|
||
verify(observer).onNext(1 + 2 + 3 + 4 + 5); | ||
verify(observer).onCompleted(); | ||
verify(observer, never()).onError(any(Throwable.class)); | ||
} | ||
|
||
@Test | ||
public void testAggregateAsIntSumSourceThrows() { | ||
Observable<Integer> result = Observable.concat(Observable.just(1, 2, 3, 4, 5), | ||
Observable.<Integer> error(new TestException())) | ||
.reduce(0, sum).map(UtilityFunctions.<Integer> identity()); | ||
|
||
result.subscribe(observer); | ||
|
||
verify(observer, never()).onNext(any()); | ||
verify(observer, never()).onCompleted(); | ||
verify(observer, times(1)).onError(any(TestException.class)); | ||
} | ||
|
||
@Test | ||
public void testAggregateAsIntSumAccumulatorThrows() { | ||
Func2<Integer, Integer, Integer> sumErr = new Func2<Integer, Integer, Integer>() { | ||
@Override | ||
public Integer call(Integer t1, Integer t2) { | ||
throw new TestException(); | ||
} | ||
}; | ||
|
||
Observable<Integer> result = Observable.just(1, 2, 3, 4, 5) | ||
.reduce(0, sumErr).map(UtilityFunctions.<Integer> identity()); | ||
|
||
result.subscribe(observer); | ||
|
||
verify(observer, never()).onNext(any()); | ||
verify(observer, never()).onCompleted(); | ||
verify(observer, times(1)).onError(any(TestException.class)); | ||
} | ||
|
||
@Test | ||
public void testAggregateAsIntSumResultSelectorThrows() { | ||
|
||
Func1<Integer, Integer> error = new Func1<Integer, Integer>() { | ||
|
||
@Override | ||
public Integer call(Integer t1) { | ||
throw new TestException(); | ||
} | ||
}; | ||
|
||
Observable<Integer> result = Observable.just(1, 2, 3, 4, 5) | ||
.reduce(0, sum).map(error); | ||
|
||
result.subscribe(observer); | ||
|
||
verify(observer, never()).onNext(any()); | ||
verify(observer, never()).onCompleted(); | ||
verify(observer, times(1)).onError(any(TestException.class)); | ||
} | ||
|
||
@Test | ||
public void testBackpressureWithNoInitialValue() throws InterruptedException { | ||
Observable<Integer> source = Observable.just(1, 2, 3, 4, 5, 6); | ||
Observable<Integer> reduced = source.reduce(sum); | ||
|
||
Integer r = reduced.toBlocking().first(); | ||
assertEquals(21, r.intValue()); | ||
} | ||
|
||
@Test | ||
public void testBackpressureWithInitialValue() throws InterruptedException { | ||
Observable<Integer> source = Observable.just(1, 2, 3, 4, 5, 6); | ||
Observable<Integer> reduced = source.reduce(0, sum); | ||
|
||
Integer r = reduced.toBlocking().first(); | ||
assertEquals(21, r.intValue()); | ||
} | ||
|
||
@Test | ||
public void testNoInitialValueDoesNotEmitMultipleTerminalEvents() { | ||
TestSubscriber<Integer> ts = TestSubscriber.create(); | ||
Observable.create(new OnSubscribe<Integer>() { | ||
|
||
@Override | ||
public void call(final Subscriber<? super Integer> sub) { | ||
sub.setProducer(new Producer() { | ||
|
||
@Override | ||
public void request(long n) { | ||
if (n > 0) { | ||
sub.onNext(1); | ||
sub.onNext(2); | ||
sub.onCompleted(); | ||
} | ||
} | ||
}); | ||
} | ||
}) | ||
.reduce(new Func2<Integer, Integer, Integer>() { | ||
|
||
@Override | ||
public Integer call(Integer a, Integer b) { | ||
throw new RuntimeException("boo"); | ||
}}) | ||
.unsafeSubscribe(ts); | ||
ts.assertError(RuntimeException.class); | ||
ts.assertNotCompleted(); | ||
} | ||
|
||
@Test | ||
public void testNoInitialValueUpstreamEmitsMoreOnNextDespiteUnsubscription() { | ||
TestSubscriber<Integer> ts = TestSubscriber.create(); | ||
Observable.create(new OnSubscribe<Integer>() { | ||
|
||
@Override | ||
public void call(final Subscriber<? super Integer> sub) { | ||
sub.setProducer(new Producer() { | ||
|
||
@Override | ||
public void request(long n) { | ||
if (n > 2) { | ||
sub.onNext(1); | ||
sub.onNext(2); | ||
sub.onNext(3); | ||
sub.onCompleted(); | ||
} | ||
} | ||
}); | ||
} | ||
}) | ||
.reduce(new Func2<Integer, Integer, Integer>() { | ||
boolean once = true; | ||
|
||
@Override | ||
public Integer call(Integer a, Integer b) { | ||
if (once) { | ||
throw new RuntimeException("boo"); | ||
} else { | ||
once = false; | ||
return a + b; | ||
} | ||
}}) | ||
.unsafeSubscribe(ts); | ||
ts.assertNoValues(); | ||
ts.assertError(RuntimeException.class); | ||
ts.assertNotCompleted(); | ||
} | ||
|
||
@Test | ||
public void testNoInitialValueDoesNotEmitMultipleErrorEventsAndReportsSecondErrorToHooks() { | ||
try { | ||
final List<Throwable> list = new CopyOnWriteArrayList<Throwable>(); | ||
RxJavaHooks.setOnError(new Action1<Throwable>() { | ||
|
||
@Override | ||
public void call(Throwable t) { | ||
list.add(t); | ||
} | ||
}); | ||
TestSubscriber<Integer> ts = TestSubscriber.create(); | ||
final RuntimeException e1 = new RuntimeException("e1"); | ||
final Throwable e2 = new RuntimeException("e2"); | ||
Observable.create(new OnSubscribe<Integer>() { | ||
|
||
@Override | ||
public void call(final Subscriber<? super Integer> sub) { | ||
sub.setProducer(new Producer() { | ||
|
||
@Override | ||
public void request(long n) { | ||
if (n > 1) { | ||
sub.onNext(1); | ||
sub.onNext(2); | ||
sub.onError(e2); | ||
} | ||
} | ||
}); | ||
} | ||
}) | ||
.reduce(new Func2<Integer, Integer, Integer>() { | ||
|
||
@Override | ||
public Integer call(Integer a, Integer b) { | ||
throw e1; | ||
}}) | ||
.unsafeSubscribe(ts); | ||
ts.assertNotCompleted(); | ||
System.out.println(ts.getOnErrorEvents()); | ||
assertEquals(Arrays.asList(e1), ts.getOnErrorEvents()); | ||
assertEquals(Arrays.asList(e2), list); | ||
} finally { | ||
RxJavaHooks.setOnError(null); | ||
} | ||
} | ||
|
||
|
||
@Test | ||
public void testNoInitialValueEmitsNoSuchElementExceptionIfEmptyStream() { | ||
TestSubscriber<Integer> ts = TestSubscriber.create(); | ||
Observable.<Integer>empty().reduce(new Func2<Integer, Integer, Integer>() { | ||
|
||
@Override | ||
public Integer call(Integer a, Integer b) { | ||
return a + b; | ||
} | ||
}).subscribe(ts); | ||
ts.assertError(NoSuchElementException.class); | ||
} | ||
|
||
|
||
} |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Isn't here
RxJavaHooks.onError
missing?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
yep, fixing