Netty 的单元测试,主要是对业务逻辑的 ChannelHandler 做测试(毕竟对 Bootstrap、EventLoop 这些做测试着实没有多大意义),模拟一次入站数据或者出站数据,查看数据流经 ChannelHandler 变成什么样了,以此达到测试的目的。

    Netty 的单元测试将Junit4作为测试框架,将 EmbeddedChannel 作为测试通道。基本原理就是:将入站数据或者出站数据写入 EmbeddedChannel 中,然后检查是否有任何东西到达了 ChannelPipeline 的尾端。以这种方式,你便可以知道消息是否流经了 ChannelHandler 以及是否触发了任何 ChannelHandler 的动作,如下图:

    EmbeddedChannel 提供了如下方法进行单元测试:

writeInbound(Object… msgs): 将入站消息写到 EmbeddedChannel 中。如果可以通过 readInbound()方法从 EmbeddedChannel 中读取数据,则返回 true。
readInbound() :从 EmbeddedChannel 中读取一个入站消息。任何返回的东西都穿越了整个 ChannelPipeline。如果没有任何可供读取的, 则返回 null。
writeOutbound(Object… msgs): 将出站消息写到EmbeddedChannel中。如果现在可以通过readOutbound()方法从 EmbeddedChannel 中读取到什么东西,则返回 true。
readOutbound(): 从 EmbeddedChannel 中读取一个出站消息。任何返回的东西都穿越了整个 ChannelPipeline。如果没有任何可供读取的,则返回 null。
finish() :将 EmbeddedChannel 标记为完成,并且如果有可被读取的入站数据或者出站数据,则返回 true。这个方法还将会调用 EmbeddedChannel 上的close()方法。

    1、将我们要测试的 ChannelHandler 写入 EmbeddedChannel 进行测试。

    2、writeInbound(Object… msgs) 将数据写入EmbeddedChannel(模拟接收数据)。

    3、ChannelHandler 处理后如果有返回数据,可以通过readInbound() 验证数据结果。如果没有返回数据,可以在 ChannelHandler 业务逻辑中,打印日志,以达到测试目的。

  1. public class DecoderTest {
  2. //1、利用Junit执行单元测试
  3. @Test
  4. public void decoderTest() throws IllegalAccessException {
  5. ByteBuf buf = Unpooled.buffer();
  6. for (int i = 0; i < 9; i++) {
  7. buf.writeByte(i);
  8. }
  9. ByteBuf buf1 = buf.duplicate();
  10. //2、创建EmbeddedChannel,并添加一个Decoder(我们的要测试 ChannelHandler) 其将以3字节帧长度被测试
  11. EmbeddedChannel embeddedChannel = new EmbeddedChannel(new Decoder(3));
  12. //3、将数据写入 EmbeddedChannel
  13. boolean writeInbound = embeddedChannel.writeInbound(buf1.retain());
  14. assertTrue(writeInbound);
  15. //4、标记 Channel 为已完成状态
  16. boolean finish = embeddedChannel.finish();
  17. assertTrue(finish);
  18. //5、读取数据
  19. ByteBuf readInbound = embeddedChannel.readInbound();
  20. ByteBuf readSlice = buf.readSlice(3);
  21. assertEquals(readInbound, readSlice);
  22. readInbound.release();
  23. readInbound = embeddedChannel.readInbound();
  24. readSlice = buf.readSlice(3);
  25. assertEquals(readInbound, readSlice);
  26. readInbound.release();
  27. readInbound = embeddedChannel.readInbound();
  28. readSlice = buf.readSlice(3);
  29. assertEquals(readInbound, readSlice);
  30. readInbound.release();
  31. //是否读取完数据了
  32. assertNull(embeddedChannel.readInbound());
  33. //释放资源
  34. buf.release();
  35. }
  36. }

    1、将我们要测试的 ChannelHandler 写入 EmbeddedChannel 进行测试。

    2、writeOutbound(Object… msgs) 将数据写入EmbeddedChannel(模拟发送数据)。

    3、ChannelHandler 处理后如果有返回数据,可以通过readOutbound() 验证数据结果。如果没有返回数据,可以在 ChannelHandler 业务逻辑中,打印日志,以达到测试目的。

  1. public class EncoderTest {
  2. @Test
  3. public void encoderTest(){
  4. ByteBuf buf = Unpooled.buffer();
  5. for (int i =1; i < 10; i++){
  6. buf.writeInt(i * -1);
  7. }
  8. //1、创建一个EmbeddedChannel 并安装要测试的Encoder
  9. EmbeddedChannel embeddedChannel = new EmbeddedChannel(new Encoder());
  10. //2、写入数据
  11. assertTrue(embeddedChannel.writeOutbound(buf));
  12. assertTrue(embeddedChannel.finish());
  13. //3、读取数据
  14. for (int i = 1; i < 10; i++){
  15. Object o = embeddedChannel.readOutbound();
  16. System.out.println(o);
  17. }
  18. assertNull(embeddedChannel.readOutbound());
  19. }
  20. }

    截止到这篇文章,Netty 的基础部分差不多就结束了。不幸的是,公司安排我去研究下 Docker 技术,想在我们项目中使用起来。所以 Netty 的学习就不得不告一段落了~~才翻完《Netty 实战》一半的内容,后面还有编解码器、网络协议、案例研究三个部分,只能先欠下了~

 

参考资料:《Netty IN ACTION》

演示源代码:https://github.com/JMCuixy/NettyDemo/tree/master/src/main/java/org/netty/demo/unit

版权声明:本文为jmcui原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。
本文链接:https://www.cnblogs.com/jmcui/p/9304508.html