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

2.x: add Observable.switchMapSingle and switchMapSingleDelayError #5161

Merged
merged 1 commit into from
Mar 9, 2017

Conversation

davidmoten
Copy link
Collaborator

This is a new operator discussed in #4853. The issue refers to a goodly number of new operators which I'll do bit by bit as my time allows and to ensure review is not too daunting.

@akarnokd akarnokd added this to the 2.1 milestone Mar 8, 2017
Copy link
Member

@akarnokd akarnokd left a comment

Choose a reason for hiding this comment

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

Looks good but some changes are required.

} else {
single = Single.unsafeCreate(source);
}
return (Observable<R>) single.toObservable();
Copy link
Member

Choose a reason for hiding this comment

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

You could just use:

return RxJavaPlugins.onAssembly(new SingleToObservable(mapper.apply(t)));

@Experimental
@CheckReturnValue
@SchedulerSupport(SchedulerSupport.NONE)
public final <R> Observable<R> switchMapSingle(Function<? super T, ? extends SingleSource<? extends R>> mapper) {
Copy link
Member

Choose a reason for hiding this comment

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

@NonNull?

@Experimental
@CheckReturnValue
@SchedulerSupport(SchedulerSupport.NONE)
public final <R> Observable<R> switchMapSingleDelayError(Function<? super T, ? extends SingleSource<? extends R>> mapper) {
Copy link
Member

Choose a reason for hiding this comment

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

@NonNull Function?


private static <T, R> Function<T, Observable<R>> convertSingleMapperToObservableMapper(
final Function<? super T, ? extends SingleSource<? extends R>> mapper) {
return new Function<T, Observable<R>>() {
Copy link
Member

Choose a reason for hiding this comment

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

ObjectHelper.requireNonNull(mapper, "mapper is null");

Copy link
Member

Choose a reason for hiding this comment

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

Please avoid anonymous inner classes.

@davidmoten
Copy link
Collaborator Author

updated with suggested changes

@davidmoten
Copy link
Collaborator Author

unrelated ci failure

io.reactivex.parallel.ParallelFlowableTest > parallelismAndPrefetchAsync FAILED
    java.lang.AssertionError: Value counts differ; Expected: 1048576, Actual: 814189 (latch = 1, values = 814189, errors = 0, completions = 0, timeout!, disposed!)
        at io.reactivex.observers.BaseTestConsumer.fail(BaseTestConsumer.java:163)
        at io.reactivex.observers.BaseTestConsumer.assertValueCount(BaseTestConsumer.java:462)
        at io.reactivex.parallel.ParallelFlowableTest.parallelismAndPrefetchAsync(ParallelFlowableTest.java:741)

@akarnokd
Copy link
Member

akarnokd commented Mar 8, 2017

unrelated ci failure

Tracking via #5154. Could be due to low timeout settings and Travis overload. Just rerun the build next time; I did it just now.

@Test
public void switchMapSingleFunctionDoesntReturnSingle() {
Observable.just(0)
.switchMapSingle(new Function<Object, SingleSource<Integer>>() {
Copy link
Collaborator

Choose a reason for hiding this comment

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

I remember that with flatMapSingle there was in the initial draft a few errors that errors weren't propagated correctly. Should not those cases also be tested that in the future once the operators receive custom implementations everything still works correctly?

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

I don't think so. At the moment those cases have full coverage. If we make custom implementations then a whole suite of tests need to be added just get coverage back up again and it will be an obvious timesaver to duplicate the tests you refer to.


@Override
public Observable<R> apply(T t) throws Exception {
return RxJavaPlugins.onAssembly(new SingleToObservable<R>(mapper.apply(t)));
Copy link
Collaborator

Choose a reason for hiding this comment

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

check that mapper.apply(t) does not return null?

Copy link
Member

Choose a reason for hiding this comment

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

Indeed, this should do it:

return RxJavaPlugins.onAssembly(new SingleToObservable<R>(
    ObjectHelper.requireNonNull(mapper.apply(t), "The mapper returned a null value")));

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

thanks will do

@codecov
Copy link

codecov bot commented Mar 8, 2017

Codecov Report

Merging #5161 into 2.x will increase coverage by 0.02%.
The diff coverage is 100%.

@@             Coverage Diff              @@
##                2.x    #5161      +/-   ##
============================================
+ Coverage     95.86%   95.88%   +0.02%     
- Complexity     5648     5652       +4     
============================================
  Files           621      621              
  Lines         39962    39972      +10     
  Branches       5610     5610              
============================================
+ Hits          38309    38329      +20     
+ Misses          665      657       -8     
+ Partials        988      986       -2
Impacted Files Coverage Δ Complexity Δ
...operators/observable/ObservableInternalHelper.java 86.31% <100%> (+1.25%) 18 <3> (+3)
src/main/java/io/reactivex/Observable.java 100% <100%> (ø) 506 <2> (+2)
...vex/internal/operators/single/SingleTakeUntil.java 86.88% <0%> (-8.2%) 2% <0%> (ø)
.../operators/completable/CompletableConcatArray.java 93.75% <0%> (-6.25%) 2% <0%> (ø)
...rnal/subscriptions/DeferredScalarSubscription.java 93.84% <0%> (-4.62%) 27% <0%> (-1%)
...io/reactivex/internal/util/BackpressureHelper.java 95.91% <0%> (-4.09%) 21% <0%> (-1%)
...ernal/operators/maybe/MaybeTakeUntilPublisher.java 96% <0%> (-4%) 2% <0%> (ø)
...main/java/io/reactivex/subjects/SingleSubject.java 95.23% <0%> (-2.39%) 38% <0%> (-1%)
...ternal/operators/completable/CompletableUsing.java 95.23% <0%> (-2.39%) 4% <0%> (ø)
.../main/java/io/reactivex/subjects/MaybeSubject.java 95.65% <0%> (-2.18%) 46% <0%> (-1%)
... and 35 more

Continue to review full report at Codecov.

Legend - Click here to learn more
Δ = absolute <relative> (impact), ø = not affected, ? = missing data
Powered by Codecov. Last update 7861679...315fb80. Read the comment docs.

@davidmoten
Copy link
Collaborator Author

updated with null check on mapper call and added unit tests for null mapper and null mapper call result. Not proposing to duplicate all tests. @akarnokd what would you like?

@akarnokd
Copy link
Member

akarnokd commented Mar 8, 2017

Don't duplicate tests. When the specialized implementation happens, that will ask for proper coverage by itself.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants