Skip to content

Commit

Permalink
Replace use of old EventLoopGroup construction with new way of const…
Browse files Browse the repository at this point in the history
…ructing it (#14020)

Motiviation:

#13991 introduced a new way of how
EventLoopGroups should be constructed. We should update our code to use
the new way in examples and tests

Modifications:

Rewrite all tests / example to use new way of constructing

Result:
Cleanup
  • Loading branch information
normanmaurer committed May 2, 2024
1 parent 062e9da commit c390810
Show file tree
Hide file tree
Showing 124 changed files with 595 additions and 455 deletions.
Expand Up @@ -25,9 +25,10 @@
import io.netty.channel.ChannelHandlerContext;
import io.netty.channel.ChannelInitializer;
import io.netty.channel.ChannelOption;
import io.netty.channel.MultiThreadIoEventLoopGroup;
import io.netty.channel.SimpleChannelInboundHandler;
import io.netty.channel.embedded.EmbeddedChannel;
import io.netty.channel.nio.NioEventLoopGroup;
import io.netty.channel.nio.NioIoHandler;
import io.netty.channel.socket.SocketChannel;
import io.netty.channel.socket.nio.NioServerSocketChannel;
import io.netty.channel.socket.nio.NioSocketChannel;
Expand Down Expand Up @@ -129,7 +130,7 @@ public void testServerCloseSocketInputProvidesData() throws InterruptedException
final CountDownLatch serverChannelLatch = new CountDownLatch(1);
final CountDownLatch responseReceivedLatch = new CountDownLatch(1);
try {
sb.group(new NioEventLoopGroup(2));
sb.group(new MultiThreadIoEventLoopGroup(2, NioIoHandler.newFactory()));
sb.channel(NioServerSocketChannel.class);
sb.childHandler(new ChannelInitializer<Channel>() {
@Override
Expand Down Expand Up @@ -173,7 +174,7 @@ public void operationComplete(ChannelFuture future) throws Exception {
}
});

cb.group(new NioEventLoopGroup(1));
cb.group(new MultiThreadIoEventLoopGroup(1, NioIoHandler.newFactory()));
cb.channel(NioSocketChannel.class);
cb.option(ChannelOption.ALLOW_HALF_CLOSURE, true);
cb.handler(new ChannelInitializer<Channel>() {
Expand Down
Expand Up @@ -25,7 +25,8 @@
import io.netty.channel.ChannelInitializer;
import io.netty.channel.ChannelPipeline;
import io.netty.channel.ChannelPromise;
import io.netty.channel.nio.NioEventLoopGroup;
import io.netty.channel.MultiThreadIoEventLoopGroup;
import io.netty.channel.nio.NioIoHandler;
import io.netty.channel.socket.nio.NioServerSocketChannel;
import io.netty.channel.socket.nio.NioSocketChannel;
import io.netty.handler.codec.compression.Brotli;
Expand Down Expand Up @@ -449,7 +450,8 @@ public Integer answer(InvocationOnMock in) throws Throwable {
any(ByteBuf.class), anyInt(), anyBoolean());

final CountDownLatch serverChannelLatch = new CountDownLatch(1);
sb.group(new NioEventLoopGroup(), new NioEventLoopGroup());
sb.group(new MultiThreadIoEventLoopGroup(NioIoHandler.newFactory()),
new MultiThreadIoEventLoopGroup(NioIoHandler.newFactory()));
sb.channel(NioServerSocketChannel.class);
sb.childHandler(new ChannelInitializer<Channel>() {
@Override
Expand All @@ -473,7 +475,7 @@ protected void initChannel(Channel ch) throws Exception {
}
});

cb.group(new NioEventLoopGroup());
cb.group(new MultiThreadIoEventLoopGroup(NioIoHandler.newFactory()));
cb.channel(NioSocketChannel.class);
cb.handler(new ChannelInitializer<Channel>() {
@Override
Expand Down
Expand Up @@ -17,7 +17,9 @@

import io.netty.buffer.ByteBuf;
import io.netty.buffer.Unpooled;
import io.netty.channel.DefaultEventLoopGroup;
import io.netty.channel.EventLoopGroup;
import io.netty.channel.MultiThreadIoEventLoopGroup;
import io.netty.channel.local.LocalIoHandler;
import io.netty.handler.codec.http2.Http2Connection.Endpoint;
import io.netty.handler.codec.http2.Http2Stream.State;
import io.netty.util.concurrent.Future;
Expand Down Expand Up @@ -59,7 +61,7 @@ public class DefaultHttp2ConnectionTest {

private DefaultHttp2Connection server;
private DefaultHttp2Connection client;
private static DefaultEventLoopGroup group;
private static EventLoopGroup group;

@Mock
private Http2Connection.Listener clientListener;
Expand All @@ -69,7 +71,7 @@ public class DefaultHttp2ConnectionTest {

@BeforeAll
public static void beforeClass() {
group = new DefaultEventLoopGroup(2);
group = new MultiThreadIoEventLoopGroup(2, LocalIoHandler.newFactory());
}

@AfterAll
Expand Down
Expand Up @@ -24,7 +24,8 @@
import io.netty.channel.ChannelInitializer;
import io.netty.channel.ChannelPipeline;
import io.netty.channel.EventLoopGroup;
import io.netty.channel.nio.NioEventLoopGroup;
import io.netty.channel.MultiThreadIoEventLoopGroup;
import io.netty.channel.nio.NioIoHandler;
import io.netty.channel.socket.SocketChannel;
import io.netty.channel.socket.nio.NioServerSocketChannel;
import io.netty.channel.socket.nio.NioSocketChannel;
Expand All @@ -42,7 +43,7 @@

public class DefaultHttp2PushPromiseFrameTest {

private final EventLoopGroup eventLoopGroup = new NioEventLoopGroup(2);
private final EventLoopGroup eventLoopGroup = new MultiThreadIoEventLoopGroup(2, NioIoHandler.newFactory());
private final ClientHandler clientHandler = new ClientHandler();
private final Map<Integer, String> contentMap = new ConcurrentHashMap<Integer, String>();

Expand Down
Expand Up @@ -30,11 +30,12 @@
import io.netty.channel.ChannelPipeline;
import io.netty.channel.DefaultEventLoop;
import io.netty.channel.EventLoopGroup;
import io.netty.channel.MultiThreadIoEventLoopGroup;
import io.netty.channel.SimpleChannelInboundHandler;
import io.netty.channel.local.LocalAddress;
import io.netty.channel.local.LocalChannel;
import io.netty.channel.local.LocalServerChannel;
import io.netty.channel.nio.NioEventLoopGroup;
import io.netty.channel.nio.NioIoHandler;
import io.netty.channel.socket.nio.NioServerSocketChannel;
import io.netty.channel.socket.nio.NioSocketChannel;
import io.netty.handler.codec.http.DefaultFullHttpRequest;
Expand Down Expand Up @@ -163,7 +164,7 @@ private static String generateLargeString(int sizeInBytes) {

@BeforeEach
public void setup() {
eventLoopGroup = new NioEventLoopGroup();
eventLoopGroup = new MultiThreadIoEventLoopGroup(NioIoHandler.newFactory());
}

@AfterEach
Expand Down Expand Up @@ -668,19 +669,19 @@ public void streamHandlerInactivatedResponseFlushed() throws InterruptedExceptio
EventLoopGroup clientEventLoopGroup = null;

try {
serverEventLoopGroup = new NioEventLoopGroup(1, new ThreadFactory() {
serverEventLoopGroup = new MultiThreadIoEventLoopGroup(1, new ThreadFactory() {
@Override
public Thread newThread(Runnable r) {
return new Thread(r, "serverloop");
}
});
}, NioIoHandler.newFactory());

clientEventLoopGroup = new NioEventLoopGroup(1, new ThreadFactory() {
clientEventLoopGroup = new MultiThreadIoEventLoopGroup(1, new ThreadFactory() {
@Override
public Thread newThread(Runnable r) {
return new Thread(r, "clientloop");
}
});
}, NioIoHandler.newFactory());

final int streams = 10;
final CountDownLatch latchClientResponses = new CountDownLatch(streams);
Expand Down
Expand Up @@ -27,8 +27,9 @@
import io.netty.channel.ChannelHandlerContext;
import io.netty.channel.ChannelInitializer;
import io.netty.channel.EventLoopGroup;
import io.netty.channel.MultiThreadIoEventLoopGroup;
import io.netty.channel.embedded.EmbeddedChannel;
import io.netty.channel.nio.NioEventLoopGroup;
import io.netty.channel.nio.NioIoHandler;
import io.netty.channel.socket.nio.NioServerSocketChannel;
import io.netty.channel.socket.nio.NioSocketChannel;
import io.netty.handler.codec.EncoderException;
Expand Down Expand Up @@ -255,7 +256,7 @@ public void testAllocatingAroundBlockSize() {
@Test
@Timeout(value = 3000, unit = TimeUnit.MILLISECONDS)
public void writingAfterClosedChannelDoesNotNPE() throws InterruptedException {
EventLoopGroup group = new NioEventLoopGroup(2);
EventLoopGroup group = new MultiThreadIoEventLoopGroup(2, NioIoHandler.newFactory());
Channel serverChannel = null;
Channel clientChannel = null;
final CountDownLatch latch = new CountDownLatch(1);
Expand Down
Expand Up @@ -20,7 +20,8 @@
import io.netty.channel.ChannelInitializer;
import io.netty.channel.ChannelPipeline;
import io.netty.channel.EventLoopGroup;
import io.netty.channel.nio.NioEventLoopGroup;
import io.netty.channel.MultiThreadIoEventLoopGroup;
import io.netty.channel.nio.NioIoHandler;
import io.netty.channel.socket.SocketChannel;
import io.netty.channel.socket.nio.NioSocketChannel;
import io.netty.example.util.ServerUtil;
Expand All @@ -39,7 +40,7 @@ public static void main(String[] args) throws Exception {
// Configure SSL.
final SslContext sslCtx = ServerUtil.buildSslContext();

EventLoopGroup group = new NioEventLoopGroup();
EventLoopGroup group = new MultiThreadIoEventLoopGroup(NioIoHandler.newFactory());
try {
Bootstrap b = new Bootstrap();
b.group(group)
Expand Down
Expand Up @@ -20,7 +20,8 @@
import io.netty.channel.ChannelInitializer;
import io.netty.channel.ChannelPipeline;
import io.netty.channel.EventLoopGroup;
import io.netty.channel.nio.NioEventLoopGroup;
import io.netty.channel.MultiThreadIoEventLoopGroup;
import io.netty.channel.nio.NioIoHandler;
import io.netty.channel.socket.SocketChannel;
import io.netty.channel.socket.nio.NioServerSocketChannel;
import io.netty.example.util.ServerUtil;
Expand All @@ -39,8 +40,8 @@ public static void main(String[] args) throws Exception {
// Configure SSL.
final SslContext sslCtx = ServerUtil.buildSslContext();

EventLoopGroup bossGroup = new NioEventLoopGroup(1);
EventLoopGroup workerGroup = new NioEventLoopGroup();
EventLoopGroup bossGroup = new MultiThreadIoEventLoopGroup(1, NioIoHandler.newFactory());
EventLoopGroup workerGroup = new MultiThreadIoEventLoopGroup(NioIoHandler.newFactory());
try {
ServerBootstrap b = new ServerBootstrap();
b.group(bossGroup, workerGroup)
Expand Down
5 changes: 3 additions & 2 deletions example/src/main/java/io/netty/example/dns/dot/DoTClient.java
Expand Up @@ -23,7 +23,8 @@
import io.netty.channel.ChannelHandlerContext;
import io.netty.channel.ChannelPipeline;
import io.netty.channel.EventLoopGroup;
import io.netty.channel.nio.NioEventLoopGroup;
import io.netty.channel.MultiThreadIoEventLoopGroup;
import io.netty.channel.nio.NioIoHandler;
import io.netty.channel.socket.SocketChannel;
import io.netty.channel.socket.nio.NioSocketChannel;
import io.netty.channel.SimpleChannelInboundHandler;
Expand Down Expand Up @@ -71,7 +72,7 @@ private static void handleQueryResp(DefaultDnsResponse msg) {
}

public static void main(String[] args) throws Exception {
EventLoopGroup group = new NioEventLoopGroup();
EventLoopGroup group = new MultiThreadIoEventLoopGroup(NioIoHandler.newFactory());
try {
final SslContext sslContext = SslContextBuilder.forClient()
.protocols("TLSv1.3", "TLSv1.2")
Expand Down
Expand Up @@ -23,7 +23,8 @@
import io.netty.channel.ChannelHandlerContext;
import io.netty.channel.ChannelPipeline;
import io.netty.channel.EventLoopGroup;
import io.netty.channel.nio.NioEventLoopGroup;
import io.netty.channel.MultiThreadIoEventLoopGroup;
import io.netty.channel.nio.NioIoHandler;
import io.netty.channel.socket.SocketChannel;
import io.netty.channel.socket.nio.NioSocketChannel;
import io.netty.channel.SimpleChannelInboundHandler;
Expand Down Expand Up @@ -69,7 +70,7 @@ private static void handleQueryResp(DefaultDnsResponse msg) {
}

public static void main(String[] args) throws Exception {
EventLoopGroup group = new NioEventLoopGroup();
EventLoopGroup group = new MultiThreadIoEventLoopGroup(NioIoHandler.newFactory());
try {
Bootstrap b = new Bootstrap();
b.group(group)
Expand Down
11 changes: 7 additions & 4 deletions example/src/main/java/io/netty/example/dns/tcp/TcpDnsServer.java
Expand Up @@ -22,8 +22,10 @@
import io.netty.channel.Channel;
import io.netty.channel.ChannelHandlerContext;
import io.netty.channel.ChannelInitializer;
import io.netty.channel.EventLoopGroup;
import io.netty.channel.MultiThreadIoEventLoopGroup;
import io.netty.channel.SimpleChannelInboundHandler;
import io.netty.channel.nio.NioEventLoopGroup;
import io.netty.channel.nio.NioIoHandler;
import io.netty.channel.socket.SocketChannel;
import io.netty.channel.socket.nio.NioServerSocketChannel;
import io.netty.channel.socket.nio.NioSocketChannel;
Expand Down Expand Up @@ -57,8 +59,9 @@ public final class TcpDnsServer {
private static final byte[] QUERY_RESULT = new byte[]{(byte) 192, (byte) 168, 1, 1};

public static void main(String[] args) throws Exception {
ServerBootstrap bootstrap = new ServerBootstrap().group(new NioEventLoopGroup(1),
new NioEventLoopGroup())
ServerBootstrap bootstrap = new ServerBootstrap().group(
new MultiThreadIoEventLoopGroup(1, NioIoHandler.newFactory()),
new MultiThreadIoEventLoopGroup(NioIoHandler.newFactory()))
.channel(NioServerSocketChannel.class)
.handler(new LoggingHandler(LogLevel.INFO))
.childHandler(new ChannelInitializer<Channel>() {
Expand Down Expand Up @@ -110,7 +113,7 @@ public void run() {

// copy from TcpDnsClient.java
private static void clientQuery() throws Exception {
NioEventLoopGroup group = new NioEventLoopGroup();
EventLoopGroup group = new MultiThreadIoEventLoopGroup(NioIoHandler.newFactory());
try {
Bootstrap b = new Bootstrap();
b.group(group)
Expand Down
5 changes: 3 additions & 2 deletions example/src/main/java/io/netty/example/dns/udp/DnsClient.java
Expand Up @@ -25,8 +25,9 @@
import io.netty.channel.ChannelInitializer;
import io.netty.channel.ChannelPipeline;
import io.netty.channel.EventLoopGroup;
import io.netty.channel.MultiThreadIoEventLoopGroup;
import io.netty.channel.SimpleChannelInboundHandler;
import io.netty.channel.nio.NioEventLoopGroup;
import io.netty.channel.nio.NioIoHandler;
import io.netty.channel.socket.DatagramChannel;
import io.netty.channel.socket.nio.NioDatagramChannel;
import io.netty.handler.codec.dns.DatagramDnsQuery;
Expand Down Expand Up @@ -67,7 +68,7 @@ private static void handleQueryResp(DatagramDnsResponse msg) {

public static void main(String[] args) throws Exception {
InetSocketAddress addr = new InetSocketAddress(DNS_SERVER_HOST, DNS_SERVER_PORT);
EventLoopGroup group = new NioEventLoopGroup();
EventLoopGroup group = new MultiThreadIoEventLoopGroup(NioIoHandler.newFactory());
try {
Bootstrap b = new Bootstrap();
b.group(group)
Expand Down
5 changes: 3 additions & 2 deletions example/src/main/java/io/netty/example/echo/EchoClient.java
Expand Up @@ -21,7 +21,8 @@
import io.netty.channel.ChannelOption;
import io.netty.channel.ChannelPipeline;
import io.netty.channel.EventLoopGroup;
import io.netty.channel.nio.NioEventLoopGroup;
import io.netty.channel.MultiThreadIoEventLoopGroup;
import io.netty.channel.nio.NioIoHandler;
import io.netty.channel.socket.SocketChannel;
import io.netty.channel.socket.nio.NioSocketChannel;
import io.netty.example.util.ServerUtil;
Expand All @@ -44,7 +45,7 @@ public static void main(String[] args) throws Exception {
final SslContext sslCtx = ServerUtil.buildSslContext();

// Configure the client.
EventLoopGroup group = new NioEventLoopGroup();
EventLoopGroup group = new MultiThreadIoEventLoopGroup(NioIoHandler.newFactory());
try {
Bootstrap b = new Bootstrap();
b.group(group)
Expand Down
7 changes: 4 additions & 3 deletions example/src/main/java/io/netty/example/echo/EchoServer.java
Expand Up @@ -21,7 +21,8 @@
import io.netty.channel.ChannelOption;
import io.netty.channel.ChannelPipeline;
import io.netty.channel.EventLoopGroup;
import io.netty.channel.nio.NioEventLoopGroup;
import io.netty.channel.MultiThreadIoEventLoopGroup;
import io.netty.channel.nio.NioIoHandler;
import io.netty.channel.socket.SocketChannel;
import io.netty.channel.socket.nio.NioServerSocketChannel;
import io.netty.example.util.ServerUtil;
Expand All @@ -41,8 +42,8 @@ public static void main(String[] args) throws Exception {
final SslContext sslCtx = ServerUtil.buildSslContext();

// Configure the server.
EventLoopGroup bossGroup = new NioEventLoopGroup(1);
EventLoopGroup workerGroup = new NioEventLoopGroup();
EventLoopGroup bossGroup = new MultiThreadIoEventLoopGroup(1, NioIoHandler.newFactory());
EventLoopGroup workerGroup = new MultiThreadIoEventLoopGroup(NioIoHandler.newFactory());
final EchoServerHandler serverHandler = new EchoServerHandler();
try {
ServerBootstrap b = new ServerBootstrap();
Expand Down
Expand Up @@ -18,7 +18,8 @@
import io.netty.bootstrap.Bootstrap;
import io.netty.channel.ChannelFuture;
import io.netty.channel.EventLoopGroup;
import io.netty.channel.nio.NioEventLoopGroup;
import io.netty.channel.MultiThreadIoEventLoopGroup;
import io.netty.channel.nio.NioIoHandler;
import io.netty.channel.socket.nio.NioSocketChannel;
import io.netty.example.util.ServerUtil;
import io.netty.handler.ssl.SslContext;
Expand All @@ -37,7 +38,7 @@ public static void main(String[] args) throws Exception {
// Configure SSL.
final SslContext sslCtx = ServerUtil.buildSslContext();

EventLoopGroup group = new NioEventLoopGroup();
EventLoopGroup group = new MultiThreadIoEventLoopGroup(NioIoHandler.newFactory());
try {
Bootstrap b = new Bootstrap();
b.group(group)
Expand Down
Expand Up @@ -17,7 +17,8 @@

import io.netty.bootstrap.ServerBootstrap;
import io.netty.channel.EventLoopGroup;
import io.netty.channel.nio.NioEventLoopGroup;
import io.netty.channel.MultiThreadIoEventLoopGroup;
import io.netty.channel.nio.NioIoHandler;
import io.netty.channel.socket.nio.NioServerSocketChannel;
import io.netty.example.util.ServerUtil;
import io.netty.handler.logging.LogLevel;
Expand All @@ -36,8 +37,8 @@ public static void main(String[] args) throws Exception {
// Configure SSL.
final SslContext sslCtx = ServerUtil.buildSslContext();

EventLoopGroup bossGroup = new NioEventLoopGroup(1);
EventLoopGroup workerGroup = new NioEventLoopGroup();
EventLoopGroup bossGroup = new MultiThreadIoEventLoopGroup(1, NioIoHandler.newFactory());
EventLoopGroup workerGroup = new MultiThreadIoEventLoopGroup(NioIoHandler.newFactory());
try {
ServerBootstrap b = new ServerBootstrap();
b.group(bossGroup, workerGroup)
Expand Down

0 comments on commit c390810

Please sign in to comment.