Skip to content
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 11 additions & 0 deletions server/src/main/java/org/elasticsearch/action/ActionListener.java
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@
import java.util.function.Consumer;
import java.util.function.Function;

import static org.elasticsearch.action.ActionListenerImplementations.checkedRunnableFromReleasable;
import static org.elasticsearch.action.ActionListenerImplementations.runnableFromReleasable;
import static org.elasticsearch.action.ActionListenerImplementations.safeAcceptException;
import static org.elasticsearch.action.ActionListenerImplementations.safeOnFailure;
Expand Down Expand Up @@ -335,6 +336,16 @@ static <Response> ActionListener<Response> runBefore(ActionListener<Response> de
return assertOnce(new ActionListenerImplementations.RunBeforeActionListener<>(delegate, runBefore));
}

/**
* Wraps a given listener and returns a new listener which releases the provided {@code releaseBefore}
* resource before the listener is notified via either {@code #onResponse} or {@code #onFailure}.
*/
static <Response> ActionListener<Response> releaseBefore(ActionListener<Response> delegate, Releasable releaseBefore) {
Copy link
Contributor

Choose a reason for hiding this comment

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

It's always irked me that these params are in the opposite order from their executioin. I guess it's consistent with runBefore(). But do contemplate whether you'd prefer this to be more readable the other way round.

return assertOnce(
new ActionListenerImplementations.RunBeforeActionListener<>(delegate, checkedRunnableFromReleasable(releaseBefore))
);
}

/**
* Wraps a given listener and returns a new listener which makes sure {@link #onResponse(Object)}
* and {@link #onFailure(Exception)} of the provided listener will be called at most once.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,20 @@ public String toString() {
};
}

static CheckedRunnable<Exception> checkedRunnableFromReleasable(Releasable releasable) {
return new CheckedRunnable<>() {
@Override
public void run() {
Releasables.closeExpectNoException(releasable);
}

@Override
public String toString() {
return "release[" + releasable + "]";
}
};
}

static void safeAcceptException(Consumer<Exception> consumer, Exception e) {
assert e != null;
try {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@
import java.util.concurrent.atomic.AtomicBoolean;
import java.util.concurrent.atomic.AtomicInteger;
import java.util.concurrent.atomic.AtomicReference;
import java.util.function.BiFunction;
import java.util.function.Consumer;

import static org.hamcrest.Matchers.containsString;
Expand Down Expand Up @@ -610,16 +611,26 @@ public String toString() {
);
}

public void testReleaseBefore() {
runReleaseListenerTest(true, false, ActionListener::releaseBefore);
runReleaseListenerTest(true, true, ActionListener::releaseBefore);
runReleaseListenerTest(false, false, ActionListener::releaseBefore);
}

public void testReleaseAfter() {
runReleaseAfterTest(true, false);
runReleaseAfterTest(true, true);
runReleaseAfterTest(false, false);
runReleaseListenerTest(true, false, ActionListener::releaseAfter);
runReleaseListenerTest(true, true, ActionListener::releaseAfter);
runReleaseListenerTest(false, false, ActionListener::releaseAfter);
}

private static void runReleaseAfterTest(boolean successResponse, final boolean throwFromOnResponse) {
private static void runReleaseListenerTest(
boolean successResponse,
final boolean throwFromOnResponse,
BiFunction<ActionListener<Void>, Releasable, ActionListener<Void>> releaseListenerProvider
) {
final AtomicBoolean released = new AtomicBoolean();
final String description = randomAlphaOfLength(10);
final ActionListener<Void> l = ActionListener.releaseAfter(new ActionListener<>() {
final ActionListener<Void> l = releaseListenerProvider.apply(new ActionListener<>() {
@Override
public void onResponse(Void unused) {
if (throwFromOnResponse) {
Expand Down