Home / Class/ SocketExceptionHandlingTest Class — netty Architecture

SocketExceptionHandlingTest Class — netty Architecture

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

Entity Profile

Dependency Diagram

graph TD
  fd3c0407_ba1e_31d2_2fbc_ec438cf54fab["SocketExceptionHandlingTest"]
  9fd334be_c250_b631_d5f7_d0a51c6c89cc["SocketExceptionHandlingTest.java"]
  fd3c0407_ba1e_31d2_2fbc_ec438cf54fab -->|defined in| 9fd334be_c250_b631_d5f7_d0a51c6c89cc
  b251ea2d_a5a1_c910_a496_14a5ae3af9f2["testReadPendingIsResetAfterEachRead()"]
  fd3c0407_ba1e_31d2_2fbc_ec438cf54fab -->|method| b251ea2d_a5a1_c910_a496_14a5ae3af9f2

Relationship Graph

Source Code

testsuite/src/main/java/io/netty/testsuite/transport/socket/SocketExceptionHandlingTest.java lines 38–119

public class SocketExceptionHandlingTest extends AbstractSocketTest {
    @Test
    public void testReadPendingIsResetAfterEachRead(TestInfo testInfo) throws Throwable {
        run(testInfo, new Runner<ServerBootstrap, Bootstrap>() {
            @Override
            public void run(ServerBootstrap serverBootstrap, Bootstrap bootstrap) throws Throwable {
                testReadPendingIsResetAfterEachRead(serverBootstrap, bootstrap);
            }
        });
    }

    public void testReadPendingIsResetAfterEachRead(ServerBootstrap sb, Bootstrap cb) throws Throwable {
        Channel serverChannel = null;
        Channel clientChannel = null;
        try {
            MyInitializer serverInitializer = new MyInitializer();
            sb.option(ChannelOption.SO_BACKLOG, 1024);
            sb.childHandler(serverInitializer);

            serverChannel = sb.bind().syncUninterruptibly().channel();

            cb.handler(new MyInitializer());
            clientChannel = cb.connect(serverChannel.localAddress()).syncUninterruptibly().channel();

            clientChannel.writeAndFlush(randomBufferType(clientChannel.alloc(), new byte[1024], 0, 1024));

            // We expect to get 2 exceptions (1 from BuggyChannelHandler and 1 from ExceptionHandler).
            assertTrue(serverInitializer.exceptionHandler.latch1.await(5, TimeUnit.SECONDS));

            // After we get the first exception, we should get no more, this is expected to timeout.
            assertFalse(serverInitializer.exceptionHandler.latch2.await(1, TimeUnit.SECONDS),
                "Encountered " + serverInitializer.exceptionHandler.count.get() +
                                        " exceptions when 1 was expected");
        } finally {
            if (serverChannel != null) {
                serverChannel.close().syncUninterruptibly();
            }
            if (clientChannel != null) {
                clientChannel.close().syncUninterruptibly();
            }
        }
    }

    private static class MyInitializer extends ChannelInitializer<Channel> {
        final ExceptionHandler exceptionHandler = new ExceptionHandler();
        @Override
        protected void initChannel(Channel ch) throws Exception {
            ChannelPipeline pipeline = ch.pipeline();

            pipeline.addLast(new BuggyChannelHandler());
            pipeline.addLast(exceptionHandler);
        }
    }

    private static class BuggyChannelHandler extends ChannelInboundHandlerAdapter {
        @Override
        public void channelRead(ChannelHandlerContext ctx, Object msg) throws Exception {
            ReferenceCountUtil.release(msg);
            throw new NullPointerException("I am a bug!");
        }
    }

    private static class ExceptionHandler extends ChannelInboundHandlerAdapter {
        final AtomicLong count = new AtomicLong();
        /**
         * We expect to get 1 call to {@link #exceptionCaught(ChannelHandlerContext, Throwable)}.
         */
        final CountDownLatch latch1 = new CountDownLatch(1);
        final CountDownLatch latch2 = new CountDownLatch(1);

        @Override
        public void exceptionCaught(ChannelHandlerContext ctx, Throwable cause) throws Exception {
            if (count.incrementAndGet() <= 2) {
                latch1.countDown();
            } else {
                latch2.countDown();
            }
            // This should not throw any exception.
            ctx.close();
        }
    }

Frequently Asked Questions

What is the SocketExceptionHandlingTest class?
SocketExceptionHandlingTest is a class in the netty codebase, defined in testsuite/src/main/java/io/netty/testsuite/transport/socket/SocketExceptionHandlingTest.java.
Where is SocketExceptionHandlingTest defined?
SocketExceptionHandlingTest is defined in testsuite/src/main/java/io/netty/testsuite/transport/socket/SocketExceptionHandlingTest.java at line 38.

Analyze Your Own Codebase

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

Try Supermodel Free