Skip to content

Commit 65a01fe

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 b5849a5 commit 65a01fe

File tree

1 file changed

+14
-3
lines changed

1 file changed

+14
-3
lines changed

core/src/test/java/org/elasticsearch/transport/RemoteClusterConnectionTests.java

Lines changed: 14 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -499,7 +499,7 @@ public void run() {
499499
barrier.await();
500500
CountDownLatch latch = new CountDownLatch(numConnectionAttempts);
501501
for (int i = 0; i < numConnectionAttempts; i++) {
502-
AtomicReference<RuntimeException> executed = new AtomicReference<>();
502+
AtomicReference<Exception> executed = new AtomicReference<>();
503503
ActionListener<Void> listener = ActionListener.wrap(
504504
x -> {
505505
if (executed.compareAndSet(null, new RuntimeException())) {
@@ -509,10 +509,21 @@ public void run() {
509509
}
510510
},
511511
x -> {
512-
if (executed.compareAndSet(null, new RuntimeException())) {
512+
if (executed.compareAndSet(null, x)) {
513513
latch.countDown();
514514
} else {
515-
throw new AssertionError("shit's been called twice", executed.get());
515+
final String message = x.getMessage();
516+
if ((executed.get().getClass() == x.getClass()
517+
&& "operation was cancelled reason [connect handler is closed]".equals(message)
518+
&& message.equals(executed.get().getMessage())) == false) {
519+
// we do cancel the operation and that means that if timing allows it, the caller
520+
// of a blocking call as well as the handler will get the exception from the
521+
// ExecutionCancelledException concurrently. unless that is the case we fail
522+
// if we get called more than once!
523+
AssertionError assertionError = new AssertionError("shit's been called twice", x);
524+
assertionError.addSuppressed(executed.get());
525+
throw assertionError;
526+
}
516527
}
517528
if (x instanceof RejectedExecutionException || x instanceof AlreadyClosedException
518529
|| x instanceof CancellableThreads.ExecutionCancelledException) {

0 commit comments

Comments
 (0)