Home / Class/ NioIoHandlerTest Class — netty Architecture

NioIoHandlerTest Class — netty Architecture

Architecture documentation for the NioIoHandlerTest class in NioIoHandlerTest.java from the netty codebase.

Entity Profile

Dependency Diagram

graph TD
  77992452_9860_5bb3_1005_1a8be4c6ad5f["NioIoHandlerTest"]
  05810aa2_1725_50bc_e7fb_0b196e4d883a["NioIoHandlerTest.java"]
  77992452_9860_5bb3_1005_1a8be4c6ad5f -->|defined in| 05810aa2_1725_50bc_e7fb_0b196e4d883a
  995658cf_0c94_91f6_3e39_2f3df800cae4["testNioHandlerCanBeDrivenByMainThread()"]
  77992452_9860_5bb3_1005_1a8be4c6ad5f -->|method| 995658cf_0c94_91f6_3e39_2f3df800cae4

Relationship Graph

Source Code

transport/src/test/java/io/netty/channel/nio/NioIoHandlerTest.java lines 37–135

public class NioIoHandlerTest {

    @Test
    void testNioHandlerCanBeDrivenByMainThread() throws Exception {
        IoHandlerFactory factory = NioIoHandler.newFactory();
        final Thread current = Thread.currentThread();
        final Queue<Runnable> tasks = new ConcurrentLinkedQueue<>();
        // Create our own IoExecutor that is driven by the main thread
        final ThreadAwareExecutor executor = new ThreadAwareExecutor() {
            @Override
            public boolean isExecutorThread(Thread thread) {
                return current == thread;
            }

            @Override
            public void execute(Runnable command) {
                tasks.add(command);
            }
        };
        IoHandlerContext context = new IoHandlerContext() {
            @Override
            public boolean canBlock() {
                // Just busy spin.
                return false;
            }

            @Override
            public long delayNanos(long currentTimeNanos) {
                return 0;
            }

            @Override
            public long deadlineNanos() {
                return -1;
            }
        };
        IoHandler handler = factory.newHandler(executor);

        // Open a ServerSocketChannel that we can connect to.
        ServerSocketChannel channel = SelectorProvider.provider().openServerSocketChannel();
        channel.configureBlocking(false);
        channel.bind(new InetSocketAddress(0));
        // Do the connect in another thread so we don't block our io execution loop.
        Thread t = new Thread(new Runnable() {
            @Override
            public void run() {
                Socket socket = new Socket();
                try {
                    socket.connect(channel.getLocalAddress());
                } catch (IOException e) {
                    throw new RuntimeException(e);
                }
            }
        });
        AtomicBoolean acceptedConnection = new AtomicBoolean();
        IoRegistration registration = handler.register(new NioSelectableChannelIoHandle<ServerSocketChannel>(channel) {
            @Override
            protected void handle(ServerSocketChannel channel, SelectionKey key) {
                if (key.isAcceptable()) {
                    try {
                        // Just accept the connection and close it.
                        java.nio.channels.SocketChannel accepted = channel.accept();
                        accepted.close();
                    } catch (Exception e) {
                        // ignore
                    } finally {
                        acceptedConnection.set(true);
                    }
                }
            }
        });
        registration.submit(NioIoOps.ACCEPT);
        t.start();

        // Let's loop until our registration was cancelled.
        while (registration.isValid()) {
            handler.run(context);
            for (;;) {
                // Execute all tasks that were dispatched.
                Runnable r = tasks.poll();
                if (r == null) {

Frequently Asked Questions

What is the NioIoHandlerTest class?
NioIoHandlerTest is a class in the netty codebase, defined in transport/src/test/java/io/netty/channel/nio/NioIoHandlerTest.java.
Where is NioIoHandlerTest defined?
NioIoHandlerTest is defined in transport/src/test/java/io/netty/channel/nio/NioIoHandlerTest.java at line 37.

Analyze Your Own Codebase

Get architecture documentation, dependency graphs, and domain analysis for your codebase in minutes.

Try Supermodel Free