Skip to content
Merged
Show file tree
Hide file tree
Changes from all 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
6 changes: 6 additions & 0 deletions src/main/java/org/xbill/DNS/Resolver.java
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
import java.util.Collections;
import java.util.List;
import java.util.concurrent.CompletableFuture;
import java.util.concurrent.CompletionException;
import java.util.concurrent.CompletionStage;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.Executor;
Expand Down Expand Up @@ -239,11 +240,16 @@ default Object sendAsync(Message query, ResolverListener listener) {
(result, throwable) -> {
if (throwable != null) {
Exception exception;
if (throwable instanceof CompletionException && throwable.getCause() != null) {
throwable = throwable.getCause();
}

if (throwable instanceof Exception) {
exception = (Exception) throwable;
} else {
exception = new Exception(throwable);
}

listener.handleException(id, exception);
return null;
}
Expand Down
47 changes: 47 additions & 0 deletions src/test/java/org/xbill/DNS/ResolverTest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
// SPDX-License-Identifier: BSD-3-Clause
package org.xbill.DNS;

import static org.assertj.core.api.Assertions.assertThat;
import static org.junit.jupiter.api.Assertions.fail;

import java.net.UnknownHostException;
import java.time.Duration;
import java.util.concurrent.CompletionException;
import java.util.concurrent.CountDownLatch;
import java.util.concurrent.TimeUnit;
import org.junit.jupiter.api.Test;

class ResolverTest {
@Test
@SuppressWarnings("deprecation")
void resolverListenerExceptionUnwrap() throws InterruptedException, UnknownHostException {
// 1. Point to a blackhole address from RFC 5737 TEST-NET-1 to ensure a timeout
SimpleResolver resolver = new SimpleResolver("192.0.2.1");
resolver.setTimeout(Duration.ofSeconds(2));

Message query =
Message.newQuery(
Record.newRecord(Name.fromConstantString("example.com."), Type.A, DClass.IN));
CountDownLatch latch = new CountDownLatch(1);

// 2. Use the async method with a listener
resolver.sendAsync(
query,
new ResolverListener() {
@Override
public void receiveMessage(Object id, Message m) {
fail("Received message (should not happen)");
latch.countDown();
}

@Override
public void handleException(Object id, Exception ex) {
// 3. Observe the exception type
assertThat(ex).isNotInstanceOf(CompletionException.class);
latch.countDown();
}
});

latch.await(5, TimeUnit.SECONDS);
}
}
Loading