Skip to content

Commit

Permalink
add testcase for Netty SelectorProviderUtil
Browse files Browse the repository at this point in the history
  • Loading branch information
czp3009 committed Jan 21, 2024
1 parent ec8be7f commit cd65e1f
Showing 1 changed file with 38 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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;

Expand All @@ -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();
Expand Down

0 comments on commit cd65e1f

Please sign in to comment.