Skip to content

Commit

Permalink
Ensure Uni.eventually() also handles cancellation signals
Browse files Browse the repository at this point in the history
  • Loading branch information
jponge committed Jan 5, 2023
1 parent 1f8a8e3 commit daa580e
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 5 deletions.
10 changes: 5 additions & 5 deletions implementation/src/main/java/io/smallrye/mutiny/Uni.java
Original file line number Diff line number Diff line change
Expand Up @@ -524,8 +524,8 @@ default <O> Uni<O> chain(Supplier<Uni<? extends O>> 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.
*
* <pre>
* {@code
Expand All @@ -538,16 +538,16 @@ default <O> Uni<O> chain(Supplier<Uni<? extends O>> supplier) {
* }
* </pre>
* <p>
* 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.
* @see #onItemOrFailure()
*/
@CheckReturnValue
default Uni<T> eventually(Runnable action) {
return onItemOrFailure().invoke((item, err) -> nonNull(action, "action").run());
return onTermination().invoke(nonNull(action, "action"));
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -346,6 +346,23 @@ public void testEventuallyActionOnFailure() {
assertThat(eventuallyCalled).isTrue();
}

@Test
public void testEventuallyOnCancellation() {
AtomicReference<Object> item = new AtomicReference<>();
AtomicBoolean eventuallyCalled = new AtomicBoolean();
AtomicBoolean onCancellationCalled = new AtomicBoolean();

UniAssertSubscriber<Integer> 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<Object> item = new AtomicReference<>();
Expand Down

0 comments on commit daa580e

Please sign in to comment.