网站建设缺乏个性,太仓网站建设找哪家,台州网站制作系统分析怎么写,义乌外贸建站序言
ChannelHandler 是Netty 应用程序的关键元素#xff0c;所以彻底地测试它们应该是你的开发过程的一个标准部分。最佳实践要求你的测试不仅要能够证明你的实现是正确的#xff0c;而且还要能够很容易地隔离那些因修改代码而突然出现的问题。这种类型的测试叫作单元测试。…序言
ChannelHandler 是Netty 应用程序的关键元素所以彻底地测试它们应该是你的开发过程的一个标准部分。最佳实践要求你的测试不仅要能够证明你的实现是正确的而且还要能够很容易地隔离那些因修改代码而突然出现的问题。这种类型的测试叫作单元测试。 单元测试应该做到应盖尽盖 虽然单元测试没有统一的定义但是大多数的从业者都有基本的共识。其基本思想是以尽可能小的区块测试你的代码并且尽可能地和其他的代码模块以及运行时的依赖如数据库和网络相隔离。如果你的应用程序能通过测试验证每个单元本身都能够正常地工作那么在出了问题时将可以更加容易地找出根本原因。
EmbeddedChannel 概述
Netty 提供了它所谓的Embedded 传输用于测试ChannelHandler。这个传输是一种特殊的Channel 实现—EmbeddedChannel—的功能这个实现提供了通过ChannelPipeline传播事件的简便方法。
这个想法是直截了当的将入站数据或者出站数据写入到EmbeddedChannel 中然后检查是否有任何东西到达了ChannelPipeline 的尾端。以这种方式你便可以确定消息是否已经被编码或者被解码过了以及是否触发了任何的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()方法
入站数据由ChannelInboundHandler 处理代表从远程节点读取的数据。出站数据由ChannelOutboundHandler 处理代表将要写到远程节点的数据。根据你要测试的ChannelHandler你将使用Inbound()或Outbound()方法对或者兼而有之。
使用EmbeddedChannel 测试ChannelHandler
JUnit 断言 org.junit.Assert 类提供了很多用于测试的静态方法。失败的断言将导致一个异常被抛出并将终止当前正在执行中的测试。导入这些断言的最高效的方式是通过一个import static 语句来实现import static org.junit.Assert.*;一旦这样做了就可以直接调用Assert 方法了assertEquals(buf.readSlice(3), read);
测试入站消息
给定一个ByteToMessageDecoder的实现给定足够的数据这个实现将 产生固定大小的帧。如果没有足够的数据可供读取它将等待下一个数据块的到来并将再次检查是否能够产生一个新的帧。
最终每个帧都会被传递给ChannelPipeline 中的下一ChannelHandler。
这是该解码器的实现
public class FixedLengthFrameDecoder extends ByteToMessageDecoder {private final int frameLength;public FixedLengthFrameDecoder(int frameLength){if (frameLength 0){throw new IllegalArgumentException(frameLength must be a positive integer: frameLength);}this.frameLength frameLength;}Overrideprotected void decode(ChannelHandlerContext ctx, ByteBuf in, ListObject out) throws Exception {if (in.readableBytes() frameLength){ByteBuf byteBuf in.readBytes(frameLength);out.add(byteBuf);}}
}现在让我们创建一个单元测试以确保这段代码将按照预期执行。正如我们前面所指出的即使是在简单的代码中单元测试也能帮助我们防止在将来代码重构时可能会导致的问题并且能在问题发生时帮助我们诊断它们。 public void testFramesDecoded(){ByteBuf buffer Unpooled.buffer();for (int i 0; i 99; i) {buffer.writeByte(i);}ByteBuf duplicate buffer.copy();FixedLengthFrameDecoder fixedLengthFrameDecoder new FixedLengthFrameDecoder(3);EmbeddedChannel embeddedChannel new EmbeddedChannel(fixedLengthFrameDecoder);assertTrue(embeddedChannel.writeInbound(duplicate.retain()));assertTrue(embeddedChannel.finish());ByteBuf read embeddedChannel.readInbound();duplicate.readBytes(3);read.release();}该testFramesDecoded()方法验证了一个包含99 个可读字节的ByteBuf 被解码为33个ByteBuf每个都包含了3 字节。需要注意的是仅通过一次对writeInbound()方法的调用ByteBuf 是如何被填充了99 个可读字节的。在此之后通过执行finish()方法将EmbeddedChannel 标记为了已完成状态。最后通过调用readInbound()方法从EmbeddedChannel 中正好读取了33个帧和一个null。
测试出站消息
测试出站消息的处理过程和刚才所看到的类似。在下面的例子中我们将会展示如何使用EmbeddedChannel 来测试一个编码器形式的ChannelOutboundHandler编码器是一种将一种消息格式转换为另一种的组件 – AbsIntegerEncoder
持有AbsIntegerEncoder 的EmbeddedChannel 将会以4 字节的负整数的形式写出站数据 编码器将从传入的ByteBuf 中读取每个负整数并将会调用Math.abs()方法来获取其绝对值 编码器将会把每个负整数的绝对值写到ChannelPipeline 中。
Testpublic void testEnc(){ByteBuf buf Unpooled.buffer();for (int i 1; i 10; i) {buf.writeInt(i * -1);}EmbeddedChannel embeddedChannel new EmbeddedChannel(new AbsIntegerEncoder());Assert.assertTrue(embeddedChannel.writeOutbound(buf));Assert.assertTrue(embeddedChannel.finish());for (int i 1; i 10; i) {Assert.assertTrue(embeddedChannel.readOutbound().equals(i));}Assert.assertNull(embeddedChannel.readOutbound());}将4 字节的负整数写到一个新的ByteBuf 中。 创建一个EmbeddedChannel并为它分配一个AbsIntegerEncoder。 调用EmbeddedChannel 上的writeOutbound()方法来写入该ByteBuf。 标记该Channel 为已完成状态。 从EmbeddedChannel 的出站端读取所有的整数并验证是否只产生了绝对值。
测试异常处理
应用程序通常需要执行比转换数据更加复杂的任务。例如你可能需要处理格式不正确的输入或者过量的数据。在下一个示例中如果所读取的字节数超出了某个特定的限制我们将会抛出TooLongFrameException。这是一种经常用来防范资源被耗尽的方法。
之前我们曾写过一个编码器FixedLengthFrameDecoder如果最大的帧大小已经被设置为3 字节。如果一个帧的大小超出了该限制那么程序将 会丢弃它的字节并抛出一个TooLongFrameException。位于ChannelPipeline 中的其他ChannelHandler 可以选择exceptionCaught()方法中处理该异常或者忽略它。 Overrideprotected void decode(ChannelHandlerContext ctx, ByteBuf in, ListObject out) throws Exception {int readableBytes in.readableBytes();if (readableBytes frameLength) {in.clear();throw new TooLongFrameException();}ByteBuf byteBuf in.readBytes(readableBytes);out.add(byteBuf);}Testpublic void testFramesDecodedMax() {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.readBytes(2)));try {channel.writeInbound(input.readBytes(4));Assert.fail();} catch (TooLongFrameException e) {// expected exception}assertTrue(channel.writeInbound(input.readBytes(3)));assertTrue(channel.finish());// Read framesByteBuf read (ByteBuf) channel.readInbound();assertEquals(buf.readSlice(2), read);read.release();read (ByteBuf) channel.readInbound();assertEquals(buf.skipBytes(4).readSlice(3), read);read.release();buf.release();}乍一看这看起来非常类似于前面代码的测试但是它有一个有趣的转折点即对TooLongFrameException的处理。这里使用的try/catch块是EmbeddedChannel的一个特殊功能。如果其中一个write方法产生了一个受检查的Exception那么它将会被包装在一个RuntimeException中并抛出。这使得可以容易地测试出一个Exception是否在处理数据的 过程中已经被处理了。
结束语
使用JUnit 这样的测试工具来进行单元测试是一种非常行之有效的方式它能保证你的代码的正确性并提高它的可维护性。为我们学习了如何使用Netty 提供的测试工具来测试自定义的ChannelHandler。