Skip to content

Add a way to check if a IoEventLoopGroup / IoEventLoop is using a spe… - #14019

Merged
normanmaurer merged 3 commits into
4.2from
type_check
May 2, 2024
Merged

normanmaurer merged 3 commits into
4.2from
type_check

Conversation

@normanmaurer

Copy link
Copy Markdown
Member

…cific IoHandler

Motivation:

We need to provide a way for the user to check if an IoEventLoopGroup is supporting NIO / Epoll / KQueue etc. Before people often used group instanceof NioEventLoopGroup etc.

Modifications:

Add new method that people can use to check what kind of IO is used.

Result:

People can use group.isIoType(NioHandler.class) etc.

@normanmaurer

Copy link
Copy Markdown
Member Author

Noticed I need something like this when porting our testsuite to the new API.

* @param handlerType the type of the {@link IoHandler}.
* @return if used or not.
*/
default boolean isIoType(Class<? extends IoHandler> handlerType) {

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I am open for a better name

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@bryce-anderson bryce-anderson left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This seems good code wise, but I'm trying to figure out where this would be used. You mentioned tests: are there any good examples of tests that could be updated to make its purpose more obvious?

@normanmaurer

Copy link
Copy Markdown
Member Author

This seems good code wise, but I'm trying to figure out where this would be used. You mentioned tests: are there any good examples of tests that could be updated to make its purpose more obvious?

Yeah for example we used something like this:

public class EpollSocketMultipleConnectTest extends SocketMultipleConnectTest {

    @Override
    protected List<TestsuitePermutation.BootstrapComboFactory<ServerBootstrap, Bootstrap>> newFactories() {
        List<TestsuitePermutation.BootstrapComboFactory<ServerBootstrap, Bootstrap>> factories
                = new ArrayList<TestsuitePermutation.BootstrapComboFactory<ServerBootstrap, Bootstrap>>();
        for (TestsuitePermutation.BootstrapComboFactory<ServerBootstrap, Bootstrap> comboFactory
                : EpollSocketTestPermutation.INSTANCE.socketWithFastOpen()) {
            EventLoopGroup group = comboFactory.newClientInstance().config().group();
            if (group instanceof NioEventLoopGroup || group instanceof EpollEventLoopGroup) {
                factories.add(comboFactory);
            }
        }
        return factories;
    }
}

Beside this sometimes users did do instanceof checks to decide which SocketChannel implementation they used etc

normanmaurer and others added 3 commits May 1, 2024 00:58
…cific IoHandler

Motivation:

We need to provide a way for the user to check if an `IoEventLoopGroup` is supporting NIO / Epoll / KQueue etc.
Before people often used `group instanceof NioEventLoopGroup` etc.

Modifications:

Add new method that people can use to check what kind of IO is used.

Result:

People can use `group.isIoType(NioHandler.class)` etc.

@bryce-anderson bryce-anderson left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah for example we used something like this:

So the example could change to look like this?

diff --git a/transport-native-epoll/src/test/java/io/netty/channel/epoll/EpollSocketMultipleConnectTest.java b/transport-native-epoll/src/test/java/io/netty/channel/epoll/EpollSocketMultipleConnectTest.java
index b70313545e..219e6c9365 100644
--- a/transport-native-epoll/src/test/java/io/netty/channel/epoll/EpollSocketMultipleConnectTest.java
+++ b/transport-native-epoll/src/test/java/io/netty/channel/epoll/EpollSocketMultipleConnectTest.java
@@ -18,7 +18,8 @@ package io.netty.channel.epoll;
 import io.netty.bootstrap.Bootstrap;
 import io.netty.bootstrap.ServerBootstrap;
 import io.netty.channel.EventLoopGroup;
-import io.netty.channel.nio.NioEventLoopGroup;
+import io.netty.channel.IoEventLoopGroup;
+import io.netty.channel.nio.NioIoHandler;
 import io.netty.testsuite.transport.TestsuitePermutation;
 import io.netty.testsuite.transport.socket.SocketMultipleConnectTest;
 
@@ -34,8 +35,12 @@ public class EpollSocketMultipleConnectTest extends SocketMultipleConnectTest {
         for (TestsuitePermutation.BootstrapComboFactory<ServerBootstrap, Bootstrap> comboFactory
                 : EpollSocketTestPermutation.INSTANCE.socketWithFastOpen()) {
             EventLoopGroup group = comboFactory.newClientInstance().config().group();
-            if (group instanceof NioEventLoopGroup || group instanceof EpollEventLoopGroup) {
-                factories.add(comboFactory);
+
+            if (group instanceof IoEventLoopGroup) {
+                IoEventLoopGroup ioGroup = (IoEventLoopGroup) group;
+                if (ioGroup.isIoType(NioIoHandler.class) || ioGroup.isIoType(EpollIoHandler.class)) {
+                    factories.add(comboFactory);
+                }
             }
         }
         return factories;

It's more code but we can get rid of most of the sub-types which is good. If that's the motivation maybe we can adjust the test to show how to use it. After that :shipit:.

@normanmaurer

Copy link
Copy Markdown
Member Author

Yes ... I did this as a followup

#14020

@normanmaurer
normanmaurer merged commit 062e9da into 4.2 May 2, 2024
@normanmaurer
normanmaurer deleted the type_check branch May 2, 2024 06:09
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants