Home / Class/ FileRegionThrottleTest Class — netty Architecture

FileRegionThrottleTest Class — netty Architecture

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

Entity Profile

Dependency Diagram

graph TD
  446f1e15_06cc_5ba2_8909_c276a7d8f4d2["FileRegionThrottleTest"]
  b1d8c2e8_da00_2332_3992_4ef5b12182b7["FileRegionThrottleTest.java"]
  446f1e15_06cc_5ba2_8909_c276a7d8f4d2 -->|defined in| b1d8c2e8_da00_2332_3992_4ef5b12182b7
  aabb6bf8_7005_461b_4479_b398f0740b62["beforeClass()"]
  446f1e15_06cc_5ba2_8909_c276a7d8f4d2 -->|method| aabb6bf8_7005_461b_4479_b398f0740b62
  ead65abc_ca43_8a99_6231_cc7b95106346["setUp()"]
  446f1e15_06cc_5ba2_8909_c276a7d8f4d2 -->|method| ead65abc_ca43_8a99_6231_cc7b95106346
  cbd51101_cd57_e141_e8a1_54055bb77b24["tearDown()"]
  446f1e15_06cc_5ba2_8909_c276a7d8f4d2 -->|method| cbd51101_cd57_e141_e8a1_54055bb77b24
  8aa56c28_7a0e_99b9_cd3f_ce510336d931["testGlobalWriteThrottle()"]
  446f1e15_06cc_5ba2_8909_c276a7d8f4d2 -->|method| 8aa56c28_7a0e_99b9_cd3f_ce510336d931
  aa70dc56_7364_9f69_c57b_701e6d90defd["ChannelFuture()"]
  446f1e15_06cc_5ba2_8909_c276a7d8f4d2 -->|method| aa70dc56_7364_9f69_c57b_701e6d90defd

Relationship Graph

Source Code

handler/src/test/java/io/netty/handler/traffic/FileRegionThrottleTest.java lines 54–159

public class FileRegionThrottleTest {
    private static final byte[] BYTES = new byte[64 * 1024 * 4];
    private static final long WRITE_LIMIT = 64 * 1024;
    private static File tmp;
    private EventLoopGroup group;

    @BeforeAll
    public static void beforeClass() throws IOException {
        final Random r = new Random();
        for (int i = 0; i < BYTES.length; i++) {
            BYTES[i] = (byte) r.nextInt(255);
        }

        tmp = PlatformDependent.createTempFile("netty-traffic", ".tmp", null);
        tmp.deleteOnExit();
        try (FileOutputStream out = new FileOutputStream(tmp)) {
            out.write(BYTES);
            out.flush();
        } catch (IOException e) {
            throw new RuntimeException(e);
        }
    }

    @BeforeEach
    public void setUp() {
        group = new MultiThreadIoEventLoopGroup(NioIoHandler.newFactory());
    }

    @AfterEach
    public void tearDown() {
        group.shutdownGracefully();
    }

    @Disabled("This test is flaky, need more investigation")
    @Test
    public void testGlobalWriteThrottle() throws Exception {
        final CountDownLatch latch = new CountDownLatch(1);
        final GlobalTrafficShapingHandler gtsh = new GlobalTrafficShapingHandler(group, WRITE_LIMIT, 0);
        ServerBootstrap bs = new ServerBootstrap();
        bs.group(group).channel(NioServerSocketChannel.class).childHandler(new ChannelInitializer<SocketChannel>() {
            @Override
            protected void initChannel(SocketChannel ch) {
                ch.pipeline().addLast(new LineBasedFrameDecoder(Integer.MAX_VALUE));
                ch.pipeline().addLast(new MessageDecoder());
                ch.pipeline().addLast(gtsh);
            }
        });
        Channel sc = bs.bind(0).sync().channel();
        Channel cc = clientConnect(sc.localAddress(), new ReadHandler(latch)).channel();

        long start = TrafficCounter.milliSecondFromNano();
        cc.writeAndFlush(Unpooled.copiedBuffer("send-file\n", CharsetUtil.US_ASCII)).sync();
        latch.await();
        long timeTaken = TrafficCounter.milliSecondFromNano() - start;
        assertTrue(timeTaken > 3000, "Data streamed faster than expected");
        sc.close().sync();
        cc.close().sync();
    }

    private ChannelFuture clientConnect(final SocketAddress server, final ReadHandler readHandler) throws Exception {
        Bootstrap bc = new Bootstrap();
        bc.group(group).channel(NioSocketChannel.class).handler(new ChannelInitializer<SocketChannel>() {
            @Override
            protected void initChannel(SocketChannel ch) {
                ch.pipeline().addLast(readHandler);
            }
        });
        return bc.connect(server).sync();
    }

    private static final class MessageDecoder extends ChannelInboundHandlerAdapter {
        @Override
        public void channelRead(ChannelHandlerContext ctx, Object msg) throws Exception {
            if (msg instanceof ByteBuf) {
                ByteBuf buf = (ByteBuf) msg;
                String message = buf.toString(Charset.defaultCharset());
                buf.release();
                if (message.equals("send-file")) {
                    RandomAccessFile raf = new RandomAccessFile(tmp, "r");
                    ctx.channel().writeAndFlush(new DefaultFileRegion(raf.getChannel(), 0, tmp.length()));
                }

Frequently Asked Questions

What is the FileRegionThrottleTest class?
FileRegionThrottleTest is a class in the netty codebase, defined in handler/src/test/java/io/netty/handler/traffic/FileRegionThrottleTest.java.
Where is FileRegionThrottleTest defined?
FileRegionThrottleTest is defined in handler/src/test/java/io/netty/handler/traffic/FileRegionThrottleTest.java at line 54.

Analyze Your Own Codebase

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

Try Supermodel Free