I. Overview and principles
Netty unit testing, mainly on the business logic of the Channelhandler do test (after all, Bootstrap, eventloop These do not make much sense), simulation of an inbound data or outbound data, view data flow through Channelhandler What has become of this, to achieve the purpose of testing.
The Netty unit test JUNIT4 as a test framework and Embeddedchannel as a test channel. The rationale is to write inbound or outbound data to Embeddedchannel, and then check to see if anything has reached the end of the channelpipeline. In this way, you can tell if the message flowed through the Channelhandler and whether any channelhandler actions were triggered, such as:
Embeddedchannel provides the following methods for unit testing:
Writeinbound (Object ... msgs): Writes inbound messages to Embeddedchannel. Returns true if the data can be read from Embeddedchannel through the Readinbound () method.
Readinbound (): reads an inbound message from the Embeddedchannel. Anything that comes back crosses the entire channelpipeline. If there is nothing to read, NULL is returned.
Writeoutbound (Object ... msgs): Writes the outbound message to Embeddedchannel. Returns true if something can now be read from Embeddedchannel through the Readoutbound () method.
Readoutbound (): reads an outbound message from the Embeddedchannel. Anything that comes back crosses the entire channelpipeline. If there is nothing to read, NULL is returned.
Finish (): marks the Embeddedchannel as complete and returns true if there is inbound data or outbound data that can be read. This method will also invoke the close () method on the Embeddedchannel.
Second, test the inbound data
1. Write the Channelhandler we want to test into embeddedchannel for testing.
2. Writeinbound (Object ... msgs) writes data to Embeddedchannel (analog receive data).
3, Channelhandler after processing if there is return data, you can verify the data results by Readinbound (). If no data is returned, the log can be printed in the Channelhandler business logic for testing purposes.
Public classDecodertest {//1. Performing unit tests with JUnit@Test Public voidDecodertest ()throwsillegalaccessexception {bytebuf buf=Unpooled.buffer (); for(inti = 0; I < 9; i++) {buf.writebyte (i); } bytebuf buf1=buf.duplicate (); //2. Create Embeddedchannel and add a decoder (our test Channelhandler) which will be tested with 3-byte frame lengthEmbeddedchannel Embeddedchannel =NewEmbeddedchannel (NewDecoder (3)); //3. Write data to Embeddedchannel BooleanWriteinbound =Embeddedchannel.writeinbound (Buf1.retain ()); Asserttrue (Writeinbound); //4. Mark Channel as Completed status Booleanfinish =Embeddedchannel.finish (); Asserttrue (finish); //5. Read DataBytebuf Readinbound =Embeddedchannel.readinbound (); Bytebuf Readslice= Buf.readslice (3); Assertequals (Readinbound, Readslice); Readinbound.release (); Readinbound=Embeddedchannel.readinbound (); Readslice= Buf.readslice (3); Assertequals (Readinbound, Readslice); Readinbound.release (); Readinbound=Embeddedchannel.readinbound (); Readslice= Buf.readslice (3); Assertequals (Readinbound, Readslice); Readinbound.release (); //have you finished reading the data?Assertnull (Embeddedchannel.readinbound ()); //Freeing Resourcesbuf.release (); }}
Third, test the outbound data
1. Write the Channelhandler we want to test into embeddedchannel for testing.
2. Writeoutbound (Object ... msgs) writes data to Embeddedchannel (simulates sending data).
3, Channelhandler after processing if there is return data, you can verify the data results by Readoutbound (). If no data is returned, the log can be printed in the Channelhandler business logic for testing purposes.
Public classencodertest {@Test Public voidencodertest () {bytebuf buf=Unpooled.buffer (); for(inti = 1; I < 10; i++) {Buf.writeint (i*-1); } //1. Create a Embeddedchannel and install the encoder to be testedEmbeddedchannel Embeddedchannel =NewEmbeddedchannel (NewEncoder ()); //2. Write Dataasserttrue (Embeddedchannel.writeoutbound (BUF)); Asserttrue (Embeddedchannel.finish ()); //3. Read Data for(inti = 1; I < 10; i++) {Object o=Embeddedchannel.readoutbound (); System.out.println (o); } assertnull (Embeddedchannel.readoutbound ()); }}
Iv. Conclusion
By the end of this article, the basic part of Netty is almost over. Unfortunately, the company arranged for me to study the Docker technology and want to use it in our project. So Netty study will have to come to an end ~ ~ Only finished "Netty combat" half of the content, behind the codec, network protocol, case study three parts, can only be owed the ~
reference:"Netty in ACTION"
Demo Source code: Https://github.com/JMCuixy/NettyDemo/tree/master/src/main/java/org/netty/demo/unit
Netty series five (unit test).