Netty use Embeddedchannel for unit testing

Source: Internet
Author: User
Netty use Embeddedchannel for unit testing
For unit testing of Netty Channelhandler, Netty provides embeddedchannel embedded channel to complete this process, which is used primarily to test the legality of inbound and outbound process of data; The channel provides the following common APIs:
Writeinbound Write a inbound message to Embeddedchannel. Returns true if the data can be read from Embeddedchannel through Readinbound ();
Readinbound Read from Embeddedchannel to inbound messages. Any return traversal of the entire channelpipeline. If the read is not yet ready, this method returns null;
Writeoutbound Write an outbound message to Embeddedchannel. Returns true if the data can be read from Embeddedchannel through Readoutbound ();
Readoutbound Read the outbound message from the Embeddedchannel. Any return traversal of the entire channelpipeline. If the read is not yet ready, this method returns null;
Finish If the data can be read from the inbound or outbound, the tag Embeddedchannel is completed and returned. This will also invoke the Embeddedchannel shutdown method;
Full API See: http://netty.io/4.1/api/io/netty/channel/embedded/EmbeddedChannel.html

The following illustration shows how Channelpipeline uses Embeddedchannel:

The complete code address for the following instance: Https://gitee.com/assad/netty-test-sample/tree/master/netty-test-sample/src/main/java/junitSample
Inbound Processor TestThe following tests an inbound decoder fixedlengthframedecoder; Fixedlengthframedecoder
Decoder for testing, separating the frames read to a fixed length
public class Fixedlengthframedecoder extends Bytetomessagedecoder {
private final int framelength; Frame length
Public fixedlengthframedecoder (int framelength) {
This.framelength = Framelength;
  }
@Override
protected void decode (Channelhandlercontext ctx, bytebuf in, list<object> out) throws Exception {
Frame segmentation
while (In.readablebytes () >= framelength)
Out.add (In.readbytes (framelength));
  }
}
Test code:
@Test
public void Testfixedframedecoder () {
Embeddedchannel channel = new Embeddedchannel (new Fixedlengthframedecoder (4));
Test Inbound writes
Bytebuf buf = Unpooled.buffer ();
for (int i = 0; i < i++)
Buf.writebyte (i);
Bytebuf in = Buf.duplicate ();
The inbound writes 3 bytes, at which point Decoder caches the data and does not forward the data to the next Channelhandler
Assertfalse (Channel.writeinbound (In.readbytes (3)));
The inbound writes 7 bytes, plus the 3 bytes previously written, Decoder forwards the first 8 bytes, divides into 2 groups to forward to the next Channelhandler, and the remaining 2 bytes are still cached
Asserttrue (Channel.writeinbound (In.readbytes (7)));
Asserttrue (Channel.finish ()); Send an end signal to the channel
Test inbound Reads
From the above write process can be estimated, the first 2 times can be read to the value, the 3rd time read to a null value
BYTEBUF read = Channel.readinbound ();
Assertequals (Read,buf.readslice (4));
Read.release ();
Read = Channel.readinbound ();
Assertequals (Read,buf.readslice (4));
Read.release ();
Read = Channel.readinbound ();
Assertnull (read);
  }

Outbound Processor TestingThe following tests an inbound encoder fixedlengthframedecoder;
Fixedlengthframedecoder
Encoder for the test, the Integer of the read is absolute
public class Absintegerencoder extends Messagetomessageencoder<bytebuf> {
@Override
protected void Encode (Channelhandlercontext ctx, bytebuf in, list<object> out) throws Exception {
while (In.readablebytes () >= 4) {
int i = Math.Abs (In.readint ());
Out.add (i);
  }
  }
}
Test code:
@Test
public void Testabsintegerencoder () {
Embeddedchannel channel = new Embeddedchannel (new Absintegerencoder ());
To test outbound writes
Bytebuf buf = Unpooled.buffer ();
for (int i = 0; i < i++)
Buf.writeint (i *-1);
Asserttrue (Channel.writeoutbound (BUF));
Asserttrue (Channel.finish ());
Testing outbound Reads
for (int i = 0; i < i++)
Assertequals ((Integer) i,channel.readoutbound ());
Assertnull (Channel.readoutbound ());
  }

Exception capture TestThe processor that throws the exception, Fixedlengthframedecoder
public class Fixedlengthframedecoder extends Bytetomessagedecoder {
private final int framelength; Frame length
private final int maxframesize; Maximum frame length
Public fixedlengthframedecoder (int framelength) {
This.framelength = Framelength;
This.maxframesize = 256;
  }
Public Fixedlengthframedecoder (int framelength, int maxframesize) {
This.framelength = Framelength;
This.maxframesize = MaxFrameSize;
  }
@Override
protected void decode (Channelhandlercontext ctx, bytebuf in, list<object> out) throws Exception {

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.