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

Conversation

Projects
None yet
3 participants
@skryvets
Copy link
Contributor

skryvets commented Jan 26, 2019

This PR adds doOnTerminate operator to Single and Maybe.

Resolves: #6379.

@codecov

This comment has been minimized.

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

This comment has been minimized.

@akarnokd

This comment has been minimized.

@skryvets

skryvets Jan 27, 2019

Author Contributor

Updated

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

@Test(expected = NullPointerException.class)

This comment has been minimized.

@akarnokd

akarnokd Jan 26, 2019

Member

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

This comment has been minimized.

@skryvets

skryvets Jan 27, 2019

Author Contributor

Implemented

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

This comment has been minimized.

@akarnokd

akarnokd Jan 26, 2019

Member

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

This comment has been minimized.

@skryvets

skryvets Jan 27, 2019

Author Contributor

Implemented

@skryvets skryvets force-pushed the skryvets:add-on-terminate-to-single-maybe branch 2 times, most recently from 03a2dc2 to c714e85 Jan 27, 2019

*/
@CheckReturnValue
@NonNull
@SchedulerSupport(SchedulerSupport.NONE)

This comment has been minimized.

@akarnokd

akarnokd Jan 27, 2019

Member

Please add @Experimental.

This comment has been minimized.

@skryvets

skryvets Jan 27, 2019

Author Contributor

Added

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

This comment has been minimized.

@akarnokd

akarnokd Jan 27, 2019

Member

Please add @since 2.2.7 - experimental.

This comment has been minimized.

@skryvets

skryvets Jan 27, 2019

Author Contributor

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

This comment has been minimized.

@akarnokd

akarnokd Jan 27, 2019

Member

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

This comment has been minimized.

@skryvets

skryvets Jan 27, 2019

Author Contributor

Changed

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

This comment has been minimized.

@akarnokd

akarnokd Jan 27, 2019

Member

Please add @Experimental.

This comment has been minimized.

@skryvets

skryvets Jan 29, 2019

Author Contributor

Added

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

This comment has been minimized.

@akarnokd

akarnokd Jan 27, 2019

Member

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

This comment has been minimized.

@skryvets

skryvets Jan 27, 2019

Author Contributor

Fixed

atomicBoolean.set(true);
}
})
.subscribe();

This comment has been minimized.

@akarnokd

akarnokd Jan 27, 2019

Member

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

This comment has been minimized.

@skryvets

skryvets Jan 27, 2019

Author Contributor

Changed

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

This comment has been minimized.

@akarnokd

akarnokd Jan 27, 2019

Member

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

This comment has been minimized.

@skryvets

skryvets Jan 27, 2019

Author Contributor

Changed

}
});

try {

This comment has been minimized.

@akarnokd

akarnokd Jan 27, 2019

Member

Why the testing style change?

This comment has been minimized.

@skryvets

skryvets Jan 27, 2019

Author Contributor

Refactored to match the rest of the testing style

@skryvets skryvets force-pushed the skryvets:add-on-terminate-to-single-maybe branch from c714e85 to 302c52b Jan 27, 2019

}
})
.test()
.assertResult();

This comment has been minimized.

@skryvets

skryvets Jan 28, 2019

Author Contributor

@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.

This comment has been minimized.

@akarnokd

akarnokd Jan 28, 2019

Member

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

@akarnokd akarnokd added this to the 2.2 backlog milestone Jan 28, 2019

}
})
.test()
.assertFailure(CompositeException.class);

This comment has been minimized.

@akarnokd

This comment has been minimized.

@skryvets

skryvets Jan 29, 2019

Author Contributor

Fixed

}
})
.test()
.assertFailure(CompositeException.class);

This comment has been minimized.

@akarnokd

This comment has been minimized.

@skryvets

skryvets Jan 29, 2019

Author Contributor

Fixed

@skryvets skryvets force-pushed the skryvets:add-on-terminate-to-single-maybe branch from 302c52b to 2bc2f16 Jan 29, 2019

}
})
.test()
.assertNoValues();

This comment has been minimized.

@skryvets

skryvets Jan 29, 2019

Author Contributor

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.

This comment has been minimized.

@akarnokd

akarnokd Jan 29, 2019

Member

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

This comment has been minimized.

@skryvets

skryvets Jan 29, 2019

Author Contributor

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.

This comment has been minimized.

@akarnokd

akarnokd Jan 29, 2019

Member

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.

This comment has been minimized.

@skryvets

skryvets Jan 29, 2019

Author Contributor

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

@skryvets skryvets force-pushed the skryvets:add-on-terminate-to-single-maybe branch from 2bc2f16 to 3e8284b Jan 29, 2019

@skryvets

This comment has been minimized.

Copy link
Contributor Author

skryvets commented Jan 29, 2019

All improvements and fixes have been implemented

@akarnokd akarnokd merged commit 6e266af into ReactiveX:2.x Jan 29, 2019

3 checks passed

codecov/patch 100% of diff hit (target 95%)
Details
codecov/project 98.28% (target 95%)
Details
continuous-integration/travis-ci/pr The Travis CI build passed
Details
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment