In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-04-01 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Servers >
Share
Shulou(Shulou.com)06/03 Report--
A special Channel implementation-EmbeddedChannel, which is provided by Netty specifically to improve unit testing for ChannelHandler.
The name responsibility writeInbound writes the inbound message to EmbeddedChannel. If the data can be read from the EmbeddedChannel through the readInbound method, return truereadInbound to read the inbound message from the EmbeddedChannel. Anything that returns passes through the entire ChannelPipeline. If there is nothing to read, return nullwriteOutbound to write the outbound message to EmbeddedChannel, and if you can now read something from EmbeddedChannel through readOutbound, return truereadOutbound to read the station message from EmbeddedChannel. Anything that returns passes through the entire ChannelPipeline. If there is nothing to read, return nullfinish to mark EmbeddedChannel as complete, and return true if there is inbound or outbound data that can be read. This method will also call the close method on EmbeddedChannel to test inbound message public class FixedLengthFrameDecoder extends ByteToMessageDecoder {private final int frameLength; public FixedLengthFrameDecoder (int frameLength) {if (frameLength = frameLength) {ByteBuf buf = in.readBytes (frameLength); out.add (buf);}} public class FixedLengthFrameDecoderTest {@ Test public void testFramesDecoded () {ByteBuf buf = Unpooled.buffer () For (int I = 0; I
< 9; i++) { buf.writeByte(i); } ByteBuf input = buf.duplicate(); EmbeddedChannel channel = new EmbeddedChannel(new FixedLengthFrameDecoder(3)); Assert.assertTrue(channel.writeInbound(input.retain())); Assert.assertTrue(channel.finish()); ByteBuf read = channel.readInbound(); Assert.assertEquals(buf.readSlice(3), read); read.release(); read = channel.readInbound(); Assert.assertEquals(buf.readSlice(3), read); read.release(); read = channel.readInbound(); Assert.assertEquals(buf.readSlice(3), read); read.release(); Assert.assertNull(channel.readInbound()); buf.release(); } @Test public void testFramesDecoded2() { ByteBuf buf = Unpooled.buffer(); for (int i = 0; i < 9; i++) { buf.writeByte(i); } ByteBuf input = buf.duplicate(); EmbeddedChannel channel = new EmbeddedChannel(new FixedLengthFrameDecoder(3)); Assert.assertFalse(channel.writeInbound(input.readBytes(2))); Assert.assertTrue(channel.writeInbound(input.readBytes(7))); Assert.assertTrue(channel.finish()); ByteBuf read = channel.readInbound(); Assert.assertEquals(buf.readSlice(3), read); read.release(); read = channel.readInbound(); Assert.assertEquals(buf.readSlice(3), read); read.release(); read = channel.readInbound(); Assert.assertEquals(buf.readSlice(3), read); read.release(); Assert.assertNull(channel.readInbound()); buf.release(); }}测试出站消息public class AbsIntegerEncoder extends MessageToMessageEncoder { @Override protected void encode(ChannelHandlerContext channelHandlerContext, ByteBuf in, List out) throws Exception { while (in.readableBytes() >= 4) {int value = Math.abs (in.readInt ()); out.add (value);}} public class AbsIntegerEncoderTest {@ Test public void testEncoded () {ByteBuf buf = Unpooled.buffer (); for (int I = 0; I
< 10; i++) { buf.writeInt(i * -1); } EmbeddedChannel channel = new EmbeddedChannel(new AbsIntegerEncoder()); Assert.assertTrue(channel.writeOutbound(buf)); Assert.assertTrue(channel.finish()); for (int i = 0; i < 10; i++) { Assert.assertEquals(Integer.valueOf(i), channel.readOutbound()); } Assert.assertNull(channel.readOutbound()); }}测试异常处理public class FrameChunkDecoder extends ByteToMessageDecoder { private final int maxFrameSize; public FrameChunkDecoder(int maxFrameSize) { this.maxFrameSize = maxFrameSize; } @Override protected void decode(ChannelHandlerContext ctx, ByteBuf in, List out) throws Exception { int readableBytes = in.readableBytes(); if (readableBytes >MaxFrameSize) {in.clear (); throw new TooLongFrameException ();} ByteBuf buf = in.readBytes (readableBytes); out.add (buf);}} public class FrameChunkDecoderTest {@ Test public void testFramesDecoded () {ByteBuf buf = Unpooled.buffer (); for (int I = 0; I < 9; iTunes +) {buf.writeByte (I) } ByteBuf input = buf.duplicate (); EmbeddedChannel channel = new EmbeddedChannel (new FrameChunkDecoder (3)); Assert.assertTrue (channel.writeInbound (input.readBytes (2); try {channel.writeInbound (input.readBytes (4)); Assert.fail ();} catch (TooLongFrameException e) {} Assert.assertTrue (channel.writeInbound (input.readBytes (3) Assert.assertTrue (channel.finish ()); ByteBuf read = channel.readInbound (); Assert.assertEquals (buf.readSlice (2), read); read.release (); read = channel.readInbound (); Assert.assertEquals (buf.skipBytes (4) .readSlice (3), read); read.release (); buf.release ();}}
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.