From c1827114e90cd89864578b00abe392db43a26a43 Mon Sep 17 00:00:00 2001 From: Norman Maurer Date: Thu, 12 May 2016 14:10:04 +0200 Subject: [PATCH] Use ConnectException when connection failed for LocalChannel Motivation: To be more consistent we should use ConnectException when we fail the connect attempt because no LocalServerChannel exists with the given address. Modifications: Use correct exception. Result: More consistent handling of connection refused between different transports. --- .../main/java/io/netty/channel/local/LocalChannel.java | 3 ++- .../java/io/netty/channel/local/LocalChannelTest.java | 10 ++++++++++ 2 files changed, 12 insertions(+), 1 deletion(-) diff --git a/transport/src/main/java/io/netty/channel/local/LocalChannel.java b/transport/src/main/java/io/netty/channel/local/LocalChannel.java index f37b37a337..518783925a 100644 --- a/transport/src/main/java/io/netty/channel/local/LocalChannel.java +++ b/transport/src/main/java/io/netty/channel/local/LocalChannel.java @@ -34,6 +34,7 @@ import io.netty.util.internal.InternalThreadLocalMap; import io.netty.util.internal.OneTimeTask; import io.netty.util.internal.PlatformDependent; +import java.net.ConnectException; import java.net.SocketAddress; import java.nio.channels.AlreadyConnectedException; import java.nio.channels.ClosedChannelException; @@ -481,7 +482,7 @@ public class LocalChannel extends AbstractChannel { Channel boundChannel = LocalChannelRegistry.get(remoteAddress); if (!(boundChannel instanceof LocalServerChannel)) { - Exception cause = new ChannelException("connection refused"); + Exception cause = new ConnectException("connection refused: " + remoteAddress); safeSetFailure(promise, cause); close(voidPromise()); return; diff --git a/transport/src/test/java/io/netty/channel/local/LocalChannelTest.java b/transport/src/test/java/io/netty/channel/local/LocalChannelTest.java index 72985d5731..e5b08dd539 100644 --- a/transport/src/test/java/io/netty/channel/local/LocalChannelTest.java +++ b/transport/src/test/java/io/netty/channel/local/LocalChannelTest.java @@ -42,6 +42,7 @@ import org.junit.AfterClass; import org.junit.BeforeClass; import org.junit.Test; +import java.net.ConnectException; import java.nio.channels.ClosedChannelException; import java.util.concurrent.CountDownLatch; import java.util.concurrent.Executor; @@ -863,6 +864,15 @@ public class LocalChannelTest { } } + @Test(expected = ConnectException.class) + public void testConnectionRefused() { + Bootstrap sb = new Bootstrap(); + sb.group(group1) + .channel(LocalChannel.class) + .handler(new TestHandler()) + .connect(LocalAddress.ANY).syncUninterruptibly(); + } + private static final class LatchChannelFutureListener extends CountDownLatch implements ChannelFutureListener { public LatchChannelFutureListener(int count) { super(count);