Don't cause ClassCastException if registration fails during constructing DnsNameResolver. (#8280)

Motivation:

We should not try to cast the Channel to a DatagramChannel as this will cause a ClassCastException.

Modifications:

- Do not cast
- rethrow from constructor if we detect the registration failed.
- Add unit test.

Result:

Propagate correct exception.
This commit is contained in:
Norman Maurer 2018-09-11 20:34:37 +02:00 committed by GitHub
parent 1dff107de1
commit 9eb124bb62
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 29 additions and 2 deletions

View File

@ -152,7 +152,7 @@ public class DnsNameResolver extends InetNameResolver {
private static final DatagramDnsQueryEncoder ENCODER = new DatagramDnsQueryEncoder();
final Future<Channel> channelFuture;
final DatagramChannel ch;
final Channel ch;
// Comparator that ensures we will try first to use the nameservers that use our preferred address type.
private final Comparator<InetSocketAddress> nameServerComparator;
@ -356,7 +356,18 @@ public class DnsNameResolver extends InetNameResolver {
});
channelFuture = responseHandler.channelActivePromise;
ch = (DatagramChannel) b.register().channel();
ChannelFuture future = b.register();
Throwable cause = future.cause();
if (cause != null) {
if (cause instanceof RuntimeException) {
throw (RuntimeException) cause;
}
if (cause instanceof Error) {
throw (Error) cause;
}
throw new IllegalStateException("Unable to create / register Channel", cause);
}
ch = future.channel();
ch.config().setRecvByteBufAllocator(new FixedRecvByteBufAllocator(maxPayloadSize));
ch.closeFuture().addListener(new ChannelFutureListener() {

View File

@ -2270,4 +2270,20 @@ public class DnsNameResolverTest {
server.stop();
}
}
@Test
public void testChannelFactoryException() {
final IllegalStateException exception = new IllegalStateException();
try {
newResolver().channelFactory(new ChannelFactory<DatagramChannel>() {
@Override
public DatagramChannel newChannel() {
throw exception;
}
}).build();
fail();
} catch (Exception e) {
assertSame(exception, e);
}
}
}