Skip to content

Commit 69ce93e

Browse files
committed
Add ActionListener.releaseBefore
This commit introduces a new helper function, ActionListener.releaseBefore, which ensures that a resource is released before the listener is notified.
1 parent 731a412 commit 69ce93e

File tree

2 files changed

+35
-5
lines changed

2 files changed

+35
-5
lines changed

server/src/main/java/org/elasticsearch/action/ActionListener.java

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -335,6 +335,25 @@ static <Response> ActionListener<Response> runBefore(ActionListener<Response> de
335335
return assertOnce(new ActionListenerImplementations.RunBeforeActionListener<>(delegate, runBefore));
336336
}
337337

338+
/**
339+
* Wraps a given listener and returns a new listener which releases the provided {@code releaseBefore}
340+
* resource before the listener is notified via either {@code #onResponse} or {@code #onFailure}.
341+
*/
342+
static <Response> ActionListener<Response> releaseBefore(ActionListener<Response> delegate, Releasable releaseBefore) {
343+
Runnable releasableRunnable = runnableFromReleasable(releaseBefore);
344+
return assertOnce(new ActionListenerImplementations.RunBeforeActionListener<>(delegate, new CheckedRunnable<>() {
345+
@Override
346+
public void run() {
347+
releasableRunnable.run();
348+
}
349+
350+
@Override
351+
public String toString() {
352+
return releasableRunnable.toString();
353+
}
354+
}));
355+
}
356+
338357
/**
339358
* Wraps a given listener and returns a new listener which makes sure {@link #onResponse(Object)}
340359
* and {@link #onFailure(Exception)} of the provided listener will be called at most once.

server/src/test/java/org/elasticsearch/action/ActionListenerTests.java

Lines changed: 16 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@
2828
import java.util.concurrent.atomic.AtomicBoolean;
2929
import java.util.concurrent.atomic.AtomicInteger;
3030
import java.util.concurrent.atomic.AtomicReference;
31+
import java.util.function.BiFunction;
3132
import java.util.function.Consumer;
3233

3334
import static org.hamcrest.Matchers.containsString;
@@ -610,16 +611,26 @@ public String toString() {
610611
);
611612
}
612613

614+
public void testReleaseBefore() {
615+
runReleaseListenerTest(true, false, ActionListener::releaseBefore);
616+
runReleaseListenerTest(true, true, ActionListener::releaseBefore);
617+
runReleaseListenerTest(false, false, ActionListener::releaseBefore);
618+
}
619+
613620
public void testReleaseAfter() {
614-
runReleaseAfterTest(true, false);
615-
runReleaseAfterTest(true, true);
616-
runReleaseAfterTest(false, false);
621+
runReleaseListenerTest(true, false, ActionListener::releaseAfter);
622+
runReleaseListenerTest(true, true, ActionListener::releaseAfter);
623+
runReleaseListenerTest(false, false, ActionListener::releaseAfter);
617624
}
618625

619-
private static void runReleaseAfterTest(boolean successResponse, final boolean throwFromOnResponse) {
626+
private static void runReleaseListenerTest(
627+
boolean successResponse,
628+
final boolean throwFromOnResponse,
629+
BiFunction<ActionListener<Void>, Releasable, ActionListener<Void>> releaseListenerProvider
630+
) {
620631
final AtomicBoolean released = new AtomicBoolean();
621632
final String description = randomAlphaOfLength(10);
622-
final ActionListener<Void> l = ActionListener.releaseAfter(new ActionListener<>() {
633+
final ActionListener<Void> l = releaseListenerProvider.apply(new ActionListener<>() {
623634
@Override
624635
public void onResponse(Void unused) {
625636
if (throwFromOnResponse) {

0 commit comments

Comments
 (0)