From cd65e1f2a99694e31582a389a6ec47a4ae96383f Mon Sep 17 00:00:00 2001 From: czp3009 Date: Sun, 21 Jan 2024 16:46:33 +0800 Subject: [PATCH] add testcase for Netty SelectorProviderUtil --- .../src/test/java/netty/NettyTests.java | 38 +++++++++++++++++++ 1 file changed, 38 insertions(+) diff --git a/tests/src/io.netty/netty-common/4.1.80.Final/src/test/java/netty/NettyTests.java b/tests/src/io.netty/netty-common/4.1.80.Final/src/test/java/netty/NettyTests.java index 5df7cc2b9..a38c1ba22 100644 --- a/tests/src/io.netty/netty-common/4.1.80.Final/src/test/java/netty/NettyTests.java +++ b/tests/src/io.netty/netty-common/4.1.80.Final/src/test/java/netty/NettyTests.java @@ -7,6 +7,9 @@ package netty; import java.io.InputStream; +import java.net.Inet6Address; +import java.nio.channels.UnsupportedAddressTypeException; +import java.nio.channels.spi.SelectorProvider; import java.time.Duration; import java.util.Objects; import java.util.concurrent.atomic.AtomicReference; @@ -56,8 +59,13 @@ import io.netty.util.CharsetUtil; import org.awaitility.Awaitility; import org.hamcrest.CoreMatchers; +import org.junit.jupiter.api.Assertions; +import org.junit.jupiter.api.Assumptions; import org.junit.jupiter.api.Test; +import static io.netty.channel.socket.InternetProtocolFamily.IPv4; +import static io.netty.util.NetUtil.LOCALHOST; + public class NettyTests { private static final int PORT = 8080; @@ -71,6 +79,36 @@ public void noSsl() throws Exception { test(false); } + @Test + void testNioSocketChannel() { + Assumptions.assumeTrue(LOCALHOST instanceof Inet6Address); + + EventLoopGroup group = new NioEventLoopGroup(); + try { + Bootstrap b = new Bootstrap().group(group) + .channelFactory(() -> new NioSocketChannel(SelectorProvider.provider(), IPv4)) + .handler(new LoggingHandler()); + Assertions.assertThrows(UnsupportedAddressTypeException.class, () -> b.bind(LOCALHOST, 7777).sync().channel()); + } finally { + group.shutdownGracefully(); + } + } + + @Test + void testNioServerSocketChannel() { + Assumptions.assumeTrue(LOCALHOST instanceof Inet6Address); + + EventLoopGroup group = new NioEventLoopGroup(); + try { + Bootstrap b = new Bootstrap().group(group) + .channelFactory(() -> new NioServerSocketChannel(SelectorProvider.provider(), IPv4)) + .handler(new LoggingHandler()); + Assertions.assertThrows(UnsupportedAddressTypeException.class, () -> b.bind(LOCALHOST, 7777).sync().channel()); + } finally { + group.shutdownGracefully(); + } + } + private void test(boolean ssl) throws Exception { EventLoopGroup bossGroup = new NioEventLoopGroup(1); EventLoopGroup workerGroup = new NioEventLoopGroup();