diff --git a/implementation/src/main/java/io/smallrye/mutiny/Uni.java b/implementation/src/main/java/io/smallrye/mutiny/Uni.java index 06cd2d175..1deb84c69 100644 --- a/implementation/src/main/java/io/smallrye/mutiny/Uni.java +++ b/implementation/src/main/java/io/smallrye/mutiny/Uni.java @@ -524,8 +524,8 @@ default Uni chain(Supplier> supplier) { } /** - * Execute an action after an item or a failure has been emitted. - * This is equivalent to a {@code finally} block in Java. + * Execute an action after an item, a failure or a cancellation has been received. + * This is semantically equivalent to a {@code finally} block in Java. * *
      * {@code
@@ -538,8 +538,8 @@ default  Uni chain(Supplier> supplier) {
      * }
      * 
*

- * This method is a shortcut for {@link UniOnItemOrFailure#invoke(BiConsumer)}: - * {@code onItemOrFailure().invoke((item, err) -> action.run())} + * This method is a shortcut for {@link UniOnTerminate#invoke(Runnable)}: + * {@code onTermination().invoke(action)} * * @param action an action to perform, must not be {@code null}. * @return a new {@link Uni} that emits events once the action has completed. @@ -547,7 +547,7 @@ default Uni chain(Supplier> supplier) { */ @CheckReturnValue default Uni eventually(Runnable action) { - return onItemOrFailure().invoke((item, err) -> nonNull(action, "action").run()); + return onTermination().invoke(nonNull(action, "action")); } /** diff --git a/implementation/src/test/java/io/smallrye/mutiny/operators/UniOnEventTest.java b/implementation/src/test/java/io/smallrye/mutiny/operators/UniOnEventTest.java index c333476e6..b896c5a77 100644 --- a/implementation/src/test/java/io/smallrye/mutiny/operators/UniOnEventTest.java +++ b/implementation/src/test/java/io/smallrye/mutiny/operators/UniOnEventTest.java @@ -346,6 +346,23 @@ public void testEventuallyActionOnFailure() { assertThat(eventuallyCalled).isTrue(); } + @Test + public void testEventuallyOnCancellation() { + AtomicReference item = new AtomicReference<>(); + AtomicBoolean eventuallyCalled = new AtomicBoolean(); + AtomicBoolean onCancellationCalled = new AtomicBoolean(); + + UniAssertSubscriber subscriber = Uni.createFrom().item(63) + .invoke(item::set) + .eventually(() -> eventuallyCalled.set(true)) + .onCancellation().invoke(() -> onCancellationCalled.set(true)) + .subscribe().withSubscriber(new UniAssertSubscriber<>(true)); + + assertThat(item.get()).isNull(); + assertThat(eventuallyCalled).isTrue(); + assertThat(onCancellationCalled).isTrue(); + } + @Test public void testEventuallyActionThrowingException() { AtomicReference item = new AtomicReference<>();