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

Add doOnTerminate to Single/Maybe for consistency #6386

Merged
merged 1 commit into from Jan 29, 2019
Merged

Add doOnTerminate to Single/Maybe for consistency #6386

merged 1 commit into from Jan 29, 2019

Conversation

skryvets
Copy link
Contributor

This PR adds doOnTerminate operator to Single and Maybe.

Resolves: #6379.

@codecov
Copy link

codecov bot commented Jan 26, 2019

Codecov Report

Merging #6386 into 2.x will increase coverage by 0.05%.
The diff coverage is 100%.

Impacted file tree graph

@@             Coverage Diff              @@
##                2.x    #6386      +/-   ##
============================================
+ Coverage     98.23%   98.28%   +0.05%     
- Complexity     6287     6299      +12     
============================================
  Files           673      675       +2     
  Lines         45092    45156      +64     
  Branches       6239     6239              
============================================
+ Hits          44297    44383      +86     
+ Misses          256      238      -18     
+ Partials        539      535       -4
Impacted Files Coverage Δ Complexity Δ
...x/internal/operators/maybe/MaybeDoOnTerminate.java 100% <100%> (ø) 2 <2> (?)
src/main/java/io/reactivex/Maybe.java 100% <100%> (ø) 173 <1> (+1) ⬆️
...internal/operators/single/SingleDoOnTerminate.java 100% <100%> (ø) 2 <2> (?)
src/main/java/io/reactivex/Single.java 100% <100%> (ø) 149 <1> (+1) ⬆️
.../operators/observable/ObservableFlatMapSingle.java 89.55% <0%> (-5.98%) 2% <0%> (ø)
...nternal/operators/parallel/ParallelReduceFull.java 91.08% <0%> (-3.97%) 2% <0%> (ø)
...tivex/internal/observers/FutureSingleObserver.java 94.33% <0%> (-3.78%) 24% <0%> (-1%)
.../operators/mixed/FlowableConcatMapCompletable.java 97.43% <0%> (-2.57%) 2% <0%> (ø)
...perators/mixed/ObservableSwitchMapCompletable.java 97.89% <0%> (-2.11%) 3% <0%> (ø)
...nternal/operators/observable/ObservableWindow.java 98% <0%> (-2%) 3% <0%> (ø)
... and 28 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 3fbfcc9...3e8284b. Read the comment docs.

@@ -2892,6 +2892,31 @@ public final T blockingGet(T defaultValue) {
));
}

/**
* Calls the shared consumer with the given onTerminate callback for each
Copy link
Member

Choose a reason for hiding this comment

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

This sentence makes no sense. Please word it like the other similar operators: http://reactivex.io/RxJava/2.x/javadoc/io/reactivex/Completable.html#doOnTerminate-io.reactivex.functions.Action-

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Updated

@@ -826,6 +827,53 @@ public void accept(Disposable v) throws Exception {
.assertFailure(TestException.class);
}

@Test(expected = NullPointerException.class)
Copy link
Member

Choose a reason for hiding this comment

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

Please put these unit test into their own files and also verify the cases when the onTerminate callback fail.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Implemented

onTerminate.run();
} catch (Throwable ex) {
Exceptions.throwIfFatal(ex);
downstream.onError(ex);
Copy link
Member

Choose a reason for hiding this comment

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

This should be a composite error with the original error followed by the handler crash. Example: https://github.com/ReactiveX/RxJava/blob/2.x/src/main/java/io/reactivex/internal/operators/completable/CompletablePeek.java#L92

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Implemented

*/
@CheckReturnValue
@NonNull
@SchedulerSupport(SchedulerSupport.NONE)
Copy link
Member

Choose a reason for hiding this comment

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

Please add @Experimental.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Added

* @return the new Maybe instance
* @see <a href="http://reactivex.io/documentation/operators/do.html">ReactiveX operators documentation: Do</a>
* @see #doOnTerminate(Action)
*/
Copy link
Member

Choose a reason for hiding this comment

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

Please add @since 2.2.7 - experimental.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Added

* @return the new Single instance
* @see <a href="http://reactivex.io/documentation/operators/do.html">ReactiveX operators documentation: Do</a>
* @see #doOnTerminate(Action)
* @since 2.0
Copy link
Member

Choose a reason for hiding this comment

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

Please use the next minor version: @since 2.2.7 - experimental.

Copy link
Contributor Author

@skryvets skryvets Jan 27, 2019

Choose a reason for hiding this comment

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

Changed

@CheckReturnValue
@NonNull
@SchedulerSupport(SchedulerSupport.NONE)
public final Single<T> doOnTerminate(final Action onTerminate) {
Copy link
Member

Choose a reason for hiding this comment

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

Please add @Experimental.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Added

public void run() {
atomicBoolean.set(true);
}
}).subscribe();
Copy link
Member

Choose a reason for hiding this comment

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

You should use .test().assertResult(1); instead of just subscribing blindly.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Fixed

atomicBoolean.set(true);
}
})
.subscribe();
Copy link
Member

Choose a reason for hiding this comment

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

You should use .test().assertResult(1); instead.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Changed

public void run() throws Exception {
atomicBoolean.set(true);
}
}).subscribe();
Copy link
Member

Choose a reason for hiding this comment

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

You should use .test().assertResult(1); instead.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Changed

}
});

try {
Copy link
Member

Choose a reason for hiding this comment

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

Why the testing style change?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Refactored to match the rest of the testing style

}
})
.test()
.assertResult();
Copy link
Contributor Author

Choose a reason for hiding this comment

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

@akarnokd, as you suggested I've tried to use .assertResult(1) which results in the test failure. I found similar pattern here, where assertResult() has no params since Maybe doesn't emit a value. Please correct me if I'm wrong.

Copy link
Member

Choose a reason for hiding this comment

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

Think about it a bit. You have an empty source and you want to verify it is still empty at the end.

}
})
.test()
.assertFailure(CompositeException.class);
Copy link
Member

Choose a reason for hiding this comment

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

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Fixed

}
})
.test()
.assertFailure(CompositeException.class);
Copy link
Member

Choose a reason for hiding this comment

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

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Fixed

}
})
.test()
.assertNoValues();
Copy link
Contributor Author

Choose a reason for hiding this comment

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

I came to a conclusion that assertNoValues() would be the right choice. I also considered assertEmpty() method, but this one will not work as onComplete event is triggered.

Thanks for pointing me in a right direction.

Copy link
Member

Choose a reason for hiding this comment

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

AssertNoValues doesn't verify the lack of errors. Please put assertResult back.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I thought your previous comment mentioned that it was a wrong approach. Maybe I misunderstood. Is it still correct to use assertResult()?

I put it back anyways according to your last review.

Copy link
Member

Choose a reason for hiding this comment

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

You misunderstood me. If a source has no values, there is no reason to put any value into the parameter of assertResult(). A no-arg assertResult verifies exactly that the outcome is no items and no errors.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Got it. Sorry for the for the back and forth from my side.

@skryvets
Copy link
Contributor Author

All improvements and fixes have been implemented

@akarnokd akarnokd merged commit 6e266af into ReactiveX:2.x Jan 29, 2019
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.

None yet

3 participants