Skip to content

Commit 9918c43

Browse files
committed
Make RemoteClusterConnectionTests more robust against cancelable threads aborts
Today we assert hart if failure listeners are invoked more than once. Yet, this can happen if we cancel the execution since the caller and the handler will get the exception on the cancelable threads and will notify the listener concurrently if timinig allows. This commit relaxes the assertion towards handling multiple invocations with `ExecutionCancelledException` Closes #24010 Closes #24179 Closes vagnerclementino/elasticsearch/#98
1 parent 16acd4e commit 9918c43

File tree

1 file changed

+14
-3
lines changed

1 file changed

+14
-3
lines changed

core/src/test/java/org/elasticsearch/action/search/RemoteClusterConnectionTests.java

Lines changed: 14 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -476,7 +476,7 @@ public void run() {
476476
barrier.await();
477477
CountDownLatch latch = new CountDownLatch(numConnectionAttempts);
478478
for (int i = 0; i < numConnectionAttempts; i++) {
479-
AtomicReference<RuntimeException> executed = new AtomicReference<>();
479+
AtomicReference<Exception> executed = new AtomicReference<>();
480480
ActionListener<Void> listener = ActionListener.wrap(
481481
x -> {
482482
if (executed.compareAndSet(null, new RuntimeException())) {
@@ -486,10 +486,21 @@ public void run() {
486486
}
487487
},
488488
x -> {
489-
if (executed.compareAndSet(null, new RuntimeException())) {
489+
if (executed.compareAndSet(null, x)) {
490490
latch.countDown();
491491
} else {
492-
throw new AssertionError("shit's been called twice", executed.get());
492+
final String message = x.getMessage();
493+
if ((executed.get().getClass() == x.getClass()
494+
&& "operation was cancelled reason [connect handler is closed]".equals(message)
495+
&& message.equals(executed.get().getMessage())) == false) {
496+
// we do cancel the operation and that means that if timing allows it, the caller
497+
// of a blocking call as well as the handler will get the exception from the
498+
// ExecutionCancelledException concurrently. unless that is the case we fail
499+
// if we get called more than once!
500+
AssertionError assertionError = new AssertionError("shit's been called twice", x);
501+
assertionError.addSuppressed(executed.get());
502+
throw assertionError;
503+
}
493504
}
494505
if (x instanceof RejectedExecutionException || x instanceof AlreadyClosedException
495506
|| x instanceof CancellableThreads.ExecutionCancelledException) {

0 commit comments

Comments
 (0)